#include <math.h> #include "bee.h" #include "cell.h" Cell::Cell(void) { x = 100; y = 100; rad = 10; m = 3.14 * 10 * 10; coul = 1; fx = fy = ax = ay = vx = vy = 0; } Cell::~Cell(void) { } void Cell::paint() { glPushMatrix(); glTranslatef( x, y, 0 ); glColor3f( 1, 1, 1 ); glLineWidth( rad / 10 ); glScalef( rad, rad, 1 ); glCallList( CIRCLE_LIST ); glPopMatrix(); } void Cell::paintBees() { glPushMatrix(); glTranslatef( x, y, 0 ); for (int i = 0; i < bees.size(); ++i) { bees.at(i)->paint(); } glPopMatrix(); } void Cell::addBee( Bee* b ) { bees.append( b ); } void Cell::remBee( Bee* b ) { bees.removeAll( b ); } void Cell::exertForce( float ex, float ey ) { fx += ex; fy += ey; } #define MIN_V 0.5 #define MIN_V2 0.25 #define COE_F 0.05 bool beeAngleLessThan( const Bee* b, const Bee* c ) { return b->degree() < c->degree(); } void Cell::step() { if ( !m ) return; ax = fx / m; ay = fy / m; vx += ax; vy += ay; //Fudge terminal velocity. if ( vx < -10 ) vx = -10; else if ( vx > 10 ) vx = 10; if ( vy < -10 ) vy = -10; else if ( vy > 10 ) vy = 10; //Appy friction. float v2 = vx * vx + vy * vy; if ( v2 > MIN_V2 ) { //Unit vector pointing toward velocity. float va = sqrt( v2 ); float ux = vx / va; float uy = vy / va; //Acceleration backward is MUf * g. float mu = min( COE_F, va - MIN_V ); vx -= mu * ux; vy -= mu * uy; } x += vx; y += vy; fx = fy = 0; if ( bees.size() == 0 ) return; //57.3 = 360/2pi float bmav = 57.3 * BEE_MAX_V / ( rad + BEE_ORBIT_RAD ); float bmaa = 57.3 * BEE_MAX_A / ( rad + BEE_ORBIT_RAD ); bmav *= 0.5; if ( bees.size() == 1 ) { bees.at(0)->setTargetSpin( bmav, bmaa ); bees.at(0)->step(); return; } float dist = 360 / bees.size(); float av, d; qSort( bees.begin(), bees.end(), beeAngleLessThan ); for (int i = 0; i < bees.size()-1; ++i) { d = bees.at(i+1)->degree() - bees.at(i)->degree(); av = bmav; if ( d < dist ) { av *= ( 0.5 * d / dist + 0.5 ); } bees.at(i)->setTargetSpin( av, bmaa ); } d = bees.at(0)->degree() - bees.at(bees.size()-1)->degree() + 360; av = bmav; if ( d < dist ) { av *= ( 0.5 * d / dist + 0.5 ); } bees.at(bees.size()-1)->setTargetSpin( av, bmaa ); for (int i = 0; i < bees.size(); ++i) { bees.at(i)->step(); } } uint Cell::CIRCLE_LIST; void Cell::buildDisplayLists() { CIRCLE_LIST = glGenLists( 1 ); glNewList( CIRCLE_LIST, GL_COMPILE ); glBegin( GL_LINE_LOOP ); for ( float r = 0 ; r < 6.3 ; r += 0.2 ) { glVertex2f( cos( r ), sin( r ) ); } glEnd(); glBegin( GL_LINE_LOOP ); for ( float r = -0.1 ; r < 6.3 ; r += 0.2 ) { glVertex2f( cos( r ), sin( r ) ); } glEnd(); glEndList(); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#5 | 5907 | Sam Stafford | Rollback change 5901. | ||
#4 | 5901 | Sam Stafford | Extra-low-quality setting for testing. | ||
#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". |