#include "bee.h" #include "cell.h" #include "hive.h" #include "settings.h" #include <math.h> #include <windows.h> #include <GL/gl.h> Bee::Bee( Hive* h ) { hive = h; curr = targ = 0x0; angle = angle_v = angle_v_target = angle_a_max = 0; p_x = p_y = v_x = v_y = v_x_target = v_y_target = 0; p_x = p_y = -10; h->takeBee( this ); } Bee::~Bee(void) { } void Bee::orbit( Cell* c ) { curr = c; targ = 0x0; c->addBee( this ); hive->dropBee( this ); float dx = p_x - c->posX(); float dy = p_y - c->posY(); float ra = atan2( dy, dx ); angle = ra * 57.295827908797774375395898255342; v_x -= c->velX(); v_y -= c->velY(); ra += 1.570795; float tux = cos( ra ); float tuy = sin( ra ); float v = v_x * tux + v_y * tuy; angle_v = v * 57.295827908797774375395898255342 / ( c->radius() + BEE_ORBIT_RAD ); } void Bee::leave() { if ( !curr ) return; p_x = curr->posX(); p_y = curr->posY(); float r = curr->radius() + BEE_ORBIT_RAD; float a = angle * 0.017453292519943295769236907684886; p_x += r * cos( a ); p_y += r * sin( a ); v_x = curr->velX(); v_y = curr->velY(); a += 1.570795; float v = angle_v * r * 0.017453292519943295769236907684886; v_x += v * cos( a ); v_y += v * sin( a ); curr->remBee( this ); hive->takeBee( this ); curr = 0x0; } void Bee::setTargetCell( Cell* c ) { if ( curr ) leave(); targ = c; } void Bee::paint() { glPushMatrix(); if ( curr ) { glRotatef( angle, 0, 0, 1 ); glTranslatef( curr->radius() + BEE_ORBIT_RAD, 0, 0 ); } else { if ( targ && settings.vectors ) { glLineWidth( 1 ); glColor4f( 0, 0, 1, 0.5 ); glBegin( GL_LINES ); glVertex2f( p_x, p_y ); glVertex2f( targ->posX(), targ->posY() ); glEnd(); } glTranslatef( p_x, p_y, 0 ); if ( targ && settings.vectors ) { glBegin( GL_LINES ); glColor3f( 1, 0, 0 ); glVertex2f( 0, 0 ); glVertex2f( v_x * 10, v_y * 10 ); glColor3f( 0, 1, 0 ); glVertex2f( 0, 0 ); glVertex2f( v_x_target * 10, v_y_target * 10 ); glEnd(); } } glColor3f( 1, 1, 1 ); glPointSize( 3 ); glBegin( GL_POINTS ); glVertex2f( 0, 0 ); glEnd(); glPopMatrix(); } void Bee::step() { if ( curr ) { if ( angle_v < angle_v_target ) { angle_v += angle_a_max; if ( angle_v > angle_v_target ) angle_v = angle_v_target; } else if ( angle_v > angle_v_target ) { angle_v -= angle_a_max; if ( angle_v < angle_v_target ) angle_v = angle_v_target; } angle += angle_v; if ( angle > 360 ) angle -= 360; if ( angle < 0 ) angle += 360; return; } if ( targ ) { float t_x = targ->posX(); float t_y = targ->posY(); float dx = t_x - p_x; float dy = t_y - p_y; float hyp = sqrt( dx * dx + dy * dy ); float pa = atan2( dy, dx ); float r = targ->radius() + BEE_ORBIT_RAD; if ( r + 1.5 >= hyp ) { orbit( targ ); if ( !targ ) step(); return; } float a = asin( r / hyp ); float va = pa - a; v_x_target = BEE_MAX_V * cos( va ); v_y_target = BEE_MAX_V * sin( va ); } if ( v_x == v_x_target && v_y == v_y_target ) goto done; float a_x_m = v_x_target - v_x; float a_y_m = v_y_target - v_y; float am2 = a_x_m * a_x_m + a_y_m * a_y_m; if ( am2 <= BEE_MAX_A * BEE_MAX_A ) { v_x = v_x_target; v_y = v_y_target; goto done; } float am = sqrt( am2 ); v_x += BEE_MAX_A * a_x_m / am; v_y += BEE_MAX_A * a_y_m / am; done: p_x += v_x; p_y += v_y; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#3 | 5899 | Sam Stafford |
Pushed a little more information into Settings and made graphics quality configurable. Want to see if that makes it run better on old machines... |
||
#2 | 5898 | Sam Stafford |
Added a quick and dirty settings file, and with it, a bit of configurability. Still tweaking the numbers on maximum velocity and acceleration for the bees. |
||
#1 | 5893 | Sam Stafford | Check in work to date on "hive". |