#include "hive.h" #include "cell.h" #include "bee.h" #include "settings.h" #include <windows.h> #include <GL/gl.h> #include <math.h> float Hive::kc = 100.0; float Hive::wallcharge = 5.0; float Hive::wallfirm = 10.0; float Hive::cellfirm = 5.0; Hive::Hive( int w, int h ) { setSize( w, h ); Cell::buildDisplayLists(); } Hive::~Hive(void) { qDeleteAll( cells ); } void Hive::paint() { step(); glClearColor( 0, 0, 0, 0 ); glClear( GL_COLOR_BUFFER_BIT ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glViewport( 0, 0, width, height ); glOrtho( 0, width, 0, height, -0.01, 0.01 ); if ( settings.hiquality ) { glEnable( GL_BLEND ); glEnable( GL_LINE_SMOOTH ); glHint( GL_LINE_SMOOTH_HINT, GL_NICEST ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); } glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); for (int i = 0; i < cells.size(); ++i) { cells.at(i)->paint(); } for (int i = 0; i < cells.size(); ++i) { cells.at(i)->paintBees(); } for (int i = 0; i < bees.size(); ++i) { bees.at(i)->paint(); } } void Hive::step() { computeForces(); for (int i = 0; i < cells.size(); ++i) { cells.at(i)->step(); } for (int i = 0; i < bees.size(); ++i) { bees.at(i)->step(); } } void Hive::doEvent() { } void Hive::computeForces() { for (int i = 0; i < cells.size(); ++i) { //Electrostatic forces. //The walls. Cell* c = cells.at(i); float x = c->posX(); float y = c->posY(); if ( x < 0 ) x = 0; else if ( x > width ) x = width; if ( y < 0 ) y = 0; else if ( y > height ) y = height; c->exertForce ( c->charge() * wallcharge * kc / ( ( x * x ) + 1 ) - c->charge() * wallcharge * kc / ( (width-x) * (width-x) + 1 ), c->charge() * wallcharge * kc / ( ( y * y ) + 1 ) - c->charge() * wallcharge * kc / ( (height-y) * (height-y) + 1 ) ); x = c->posX(); y = c->posY(); float r = c->radius(); float w = width; float h = height; if ( x + r > w ) c->exertForce( wallfirm * ( w - r - x ), 0 ); else if ( x - r < 0 ) c->exertForce( wallfirm * ( r - x ), 0 ); if ( y + r > h ) c->exertForce( 0, wallfirm * ( h - r - y ) ); else if ( y - r < 0 ) c->exertForce( 0, wallfirm * ( r - y ) ); //The other cells. for (int j = i+1 ; j < cells.size(); ++j) { Cell* d = cells.at(j); float dx = d->posX() - c->posX(); float dy = d->posY() - c->posY(); if ( dx > 0 ) dx++; else dx--; if ( dy > 0 ) dy++; else dy--; float r2 = dx * dx + dy * dy; //Cheap optimization: ignore anything 200 units away. if ( r2 > 40000 ) continue; float r = sqrt( r2 ); float ux = dx / r; float uy = dy / r; float m = kc * c->charge() * d->charge() / r2; if ( r < c->radius() + d->radius() ) m += cellfirm * ( c->radius() + d->radius() - r ); d->exertForce( ux * m, uy * m ); c->exertForce( -ux * m, -uy * m ); } } } void Hive::takeBee( Bee* b ) { bees.append( b ); } void Hive::dropBee( Bee* b ) { bees.removeAll( b ); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 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... |
||
#1 | 5893 | Sam Stafford | Check in work to date on "hive". |