#include #include #include #include "simulation.h" static int _width, _height; static int _dt; static Particle *_draggedParticle = NULL; static int _radius; static int _dragX, _dragY; static int _mouseX, _mouseY; void resize(int width, int height) { _width = width; _height = height; } void Sim_Init(int width, int height, int radius, int dt) { resize(width, height); _radius = radius; _dt = dt; } void Sim_Quit() { _draggedParticle = NULL; } void Sim_Resize(int width, int height) { resize(width, height); } void Sim_Randomize(Particle *particles, int count) { int i; time_t t; srand((unsigned)time(&t)); for(i = 0; i < count; i++) { Particle *p = &particles[i]; p->x = (rand() % _width) - _radius; p->y = (rand() % _height) - _radius; p->angle = rand() % 360; p->xVel = ((double)(rand() % 100) / 1000 + 0.05) * (rand() % 2 ? -1 : 1); p->yVel = ((double)(rand() % 100) / 1000 + 0.05) * (rand() % 2 ? -1 : 1); switch(rand() % 3) { case 0: p->r = 0xFF; p->g = 0; p->b = 0; break; case 1: p->r = 0.44 * 0xFF; p->g = 0.87 * 0xFF; p->b = 0.76 * 0xFF; break; case 2: p->r = 0xFF; p->g = 0xFF; p->b = 0xFF; break; } p->a = rand() % 0xFF; } } int Sim_Update(Particle *particles, int count, double deltaTime) { int collided = 0; int i, j; double stepTime = deltaTime * 1000 / _dt; for(i = 0; i < _dt; i++) { for(j = 0; j < count; j++) { Particle *p = &particles[j]; if(_draggedParticle == p) { p->x = _mouseX + _dragX; p->y = _mouseY + _dragY; } else { if(p->x < 0) { p->x = 0; p->xVel *= -1; collided = 1; } if(p->x > _width - (_radius * 2)) { p->x = _width - (_radius * 2); p->xVel *= -1; collided = 1; } if(p->y < 0) { p->y = 0; p->yVel *= -1; collided = 1; } if(p->y > _height - (_radius * 2)) { p->y = _height - (_radius * 2); p->yVel *= -1; collided = 1; } p->x += p->xVel * stepTime; p->y += p->yVel * stepTime; } p->angle += 0.1 * stepTime; p->a -= 0.1 * stepTime; if(p->a <= 0) { p->a = 0xFF; } } } return collided; } void Sim_UpdateDrag(int x, int y) { _mouseX = x; _mouseY = y; } void Sim_BeginDrag(Particle *particles, int count, int x, int y) { int i; Sim_UpdateDrag(x, y); for(i = 0; i < count; i++) { Particle *p = &particles[i]; int radiusSquared = _radius * _radius; double distSquared = pow(p->x - x + _radius, 2) + pow(p->y - y + _radius, 2); if(distSquared < radiusSquared) { _draggedParticle = p; _dragX = p->x - x; _dragY = p->y - y; break; } } } void Sim_EndDrag() { if(_draggedParticle != NULL) { _draggedParticle->xVel = ((double)(rand() % 100) / 100 + 0.1) * (rand() % 2 ? -1 : 1); _draggedParticle->yVel = ((double)(rand() % 100) / 100 + 0.1) * (rand() % 2 ? -1 : 1); _draggedParticle = NULL; } }