From: LMH Date: Fri, 28 Nov 2014 20:19:49 +0000 (-1000) Subject: Created a new definition for Particles. X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=bae187e901d4360415ac28c5547fa399ecbd740f;p=supertux.git Created a new definition for Particles. 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. --- diff --git a/src/object/particles.cpp b/src/object/particles.cpp index a51da08a1..6ee3ab191 100644 --- a/src/object/particles.cpp +++ b/src/object/particles.cpp @@ -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 diff --git a/src/object/particles.hpp b/src/object/particles.hpp index c98aa57cd..f9f18a099 100644 --- a/src/object/particles.hpp +++ b/src/object/particles.hpp @@ -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);