Created a new definition for Particles.
authorLMH <lmh.0013@gmail.com>
Fri, 28 Nov 2014 20:19:49 +0000 (10:19 -1000)
committerLMH <lmh.0013@gmail.com>
Fri, 28 Nov 2014 20:39:09 +0000 (10:39 -1000)
Previously, the parameters used to define particles were not very intuitive. Specifically, the angles and initial velocity were very muddled. It appeared that the angle parameters were being used to randomize both angle and velocity, but in some bizarre manner.  The new definition allows the bounds for both the actual angle of trajectory and magnitude of the initial velocity to be defined, and a random value between those bounds is what is used for each particle.

In a nutshell: the new definition uses trigonometry in a physically correct and intuitive manner.

Since the old definition is called in a manner that makes particles which look good, it has not yet been replaced. However, these calls should probably be reworked under the new definition to produce the same behavior before removing the old definition.

src/object/particles.cpp
src/object/particles.hpp

index a51da08..6ee3ab1 100644 (file)
@@ -24,6 +24,7 @@
 #include "supertux/sector.hpp"
 #include "video/drawing_context.hpp"
 
+//TODO: remove this function in favor of the one below
 Particles::Particles(const Vector& epicenter, int min_angle, int max_angle,
                      const Vector& initial_velocity, const Vector& acceleration, int number,
                      Color color_, int size_, float life_time, int drawing_layer_) :
@@ -61,6 +62,41 @@ Particles::Particles(const Vector& epicenter, int min_angle, int max_angle,
   }
 }
 
+Particles::Particles(const Vector& epicenter, int min_angle, int max_angle,
+                     const float min_initial_velocity, const float max_initial_velocity,
+                     const Vector& acceleration, int number, Color color_,
+                     int size_, float life_time, int drawing_layer_) :
+
+  accel(acceleration),
+  timer(),
+  live_forever(),
+  color(color_),
+  size(size_),
+  drawing_layer(drawing_layer_),
+  particles()
+{
+  if(life_time == 0) {
+    live_forever = true;
+  } else {
+    live_forever = false;
+    timer.start(life_time);
+  }
+
+  // create particles
+  for(int p = 0; p < number; p++)
+  {
+    Particle* particle = new Particle;
+    particle->pos = epicenter;
+
+    float velocity = graphicsRandom.rand(min_initial_velocity, max_initial_velocity);
+    float angle = graphicsRandom.rand(min_angle, max_angle) * (M_PI / 180);  // convert to radians
+    particle->vel.x = (cos(angle)) * velocity;
+    particle->vel.y = (sin(angle)) * velocity;
+
+    particles.push_back(particle);
+  }
+}
+
 Particles::~Particles()
 {
   // free particles
index c98aa57..f9f18a0 100644 (file)
@@ -29,6 +29,10 @@ public:
             const Vector& initial_velocity, const Vector& acceleration,
             int number, Color color, int size, float life_time,
             int drawing_layer);
+  Particles(const Vector& epicenter, int min_angle, int max_angle,
+            const float min_initial_velocity, const float max_initial_velocity,
+            const Vector& acceleration, int number, Color color,
+            int size, float life_time, int drawing_layer);
   ~Particles();
 
   virtual void update(float elapsed_time);