Skip to content

Commit

Permalink
Particle engine improved (See #99)
Browse files Browse the repository at this point in the history
- Instead of particle number and burst size, period given in constructor.
- In Thruster, SpeedRange(), BeamAngle() method implemented.
- Keys added for thruster test.
- Period(), Position(), and Angle() methods added to ParticleManager.
- AddParticles() method removed, since it's related with burst size.
  • Loading branch information
abekkine committed May 12, 2018
1 parent 485b4c2 commit 9753112
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 21 deletions.
4 changes: 2 additions & 2 deletions testing/particle-test/BigBang.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class Star : public Particle {

class BigBang : public ParticleManager {
public:
BigBang(int n, int m)
: ParticleManager(n, m)
BigBang(double period)
: ParticleManager(period)
{}
~BigBang() {}
void AddParticle() {
Expand Down
41 changes: 30 additions & 11 deletions testing/particle-test/ParticleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@

#include "Particle.h"

#include <assert.h>
#include <time.h>

#include <vector>

#define MAX_PARTICLES 1000

class ParticleManager {
public:
ParticleManager(int numParticles, int numBurst)
: kMaxParticles(numParticles)
, kBurstSize(numBurst)
ParticleManager(double period)
: kMaxParticles(MAX_PARTICLES)
, period_(period)
, remainder_time_(0.0)
{}
virtual ~ParticleManager() {}
virtual void Init() {
srand48(clock());
}
void Period(double period) {
period_ = period;
}
void Position(double x, double y) {
x_ = x;
y_ = y;
}
void Angle(double a) {
angle_ = a;
}
virtual void AddParticle() = 0;
void Render() {
glPushMatrix();
Expand All @@ -30,15 +44,19 @@ class ParticleManager {

glPopMatrix();
}
void AddParticles() {
for (int i=0; i<kBurstSize; ++i) {
AddParticle();
}
}
virtual void Update(double time_step) {

if (particles.size() < (kMaxParticles-kBurstSize)) {
AddParticles();
assert( period_ > 0.0 && "Period cannot be equal to or lesser than 0.0");

if (period_ > 0.0) {

int count = (time_step + remainder_time_) / period_;
remainder_time_ = fmod(time_step + remainder_time_, period_);
for (int i=0; i<count; ++i) {
if (particles.size() < kMaxParticles) {
AddParticle();
}
}
}

for (auto iP = particles.begin(); iP != particles.end(); ++iP) {
Expand All @@ -54,11 +72,12 @@ class ParticleManager {

protected:
const int kMaxParticles;
const int kBurstSize;
double period_;
double x_;
double y_;
double angle_;
std::vector<Particle *> particles;
double remainder_time_;
};

#endif // PARTICLE_MANAGER_H_
Expand Down
25 changes: 19 additions & 6 deletions testing/particle-test/Thruster.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,26 @@ class Smoke : public Particle {

class Thruster : public ParticleManager {
public:
Thruster(int n, int m) : ParticleManager(n, m) {
x_ = 10.0;
y_ = 10.0;
angle_ = 90.0;
Thruster(double period) : ParticleManager(period) {
Position(10.0, 10.0);
Angle(90.0);
min_speed_ = 5.0;
max_speed_ = 15.0;
beam_angle_ = 45.0 * M_PI / 180.0;
}
~Thruster() {}
void SpeedRange(double min, double max) {
min_speed_ = min;
max_speed_ = max;
}
void BeamAngle(double alpha) {
beam_angle_ = alpha * M_PI / 180.0;
}
void AddParticle() {
Smoke *p;
p = new Smoke(1.0);
double a = (drand48() * M_PI / 6.0) - (M_PI / 12.0);
double s = drand48() * 10.0 + 5.0;
double a = (drand48() * beam_angle_) - 0.5 * beam_angle_;
double s = drand48() * (max_speed_ - min_speed_) + min_speed_;
p->vx = s * cos(a);
p->vy = s * sin(a);
p->color[0] = 1.0;
Expand All @@ -37,6 +46,10 @@ class Thruster : public ParticleManager {

particles.push_back(p);
}
private:
double min_speed_;
double max_speed_;
double beam_angle_;
};

#endif // THRUSTER_H_
Expand Down
21 changes: 19 additions & 2 deletions testing/particle-test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ void display(void) {
void keyboard(unsigned char key, int x, int y) {

switch(key) {
case '1':
thruster->Period(0.05);
thruster->BeamAngle(45.0);
thruster->SpeedRange(10.0, 15.0);
break;
case '2':
thruster->Period(0.02);
thruster->BeamAngle(35.0);
thruster->SpeedRange(10.0, 15.0);
break;
case '3':
thruster->Period(0.001);
thruster->BeamAngle(25.0);
thruster->SpeedRange(10.0, 15.0);
break;
case 27:
exit(0);
break;
Expand Down Expand Up @@ -55,10 +70,12 @@ void init() {

init_gfx();

bang = new BigBang(2000, 5);
// Emit one particle each second.
bang = new BigBang(1.0);
bang->Init();

thruster = new Thruster(500, 5);
// Emit one particle each 0.01 second, or with 100.0 Hz.
thruster = new Thruster(0.02);
thruster->Init();
}

Expand Down

0 comments on commit 9753112

Please sign in to comment.