[Supertux-Commit] r6261 - trunk/supertux/src/object
mathnerd314 at cummiskey.dreamhost.com
mathnerd314 at cummiskey.dreamhost.com
Mon Jan 18 15:27:55 PST 2010
Author: mathnerd314
Date: 2010-01-18 15:27:55 -0800 (Mon, 18 Jan 2010)
New Revision: 6261
Modified:
trunk/supertux/src/object/particlesystem.cpp
trunk/supertux/src/object/particlesystem.hpp
trunk/supertux/src/object/snow_particle_system.cpp
trunk/supertux/src/object/snow_particle_system.hpp
Log:
Snow ADSR (bug 221)
This has the same ideas as the patch but is implemented slightly differently. Tweaks to various constants/timings welcome.
Modified: trunk/supertux/src/object/particlesystem.cpp
===================================================================
--- trunk/supertux/src/object/particlesystem.cpp 2010-01-18 22:41:35 UTC (rev 6260)
+++ trunk/supertux/src/object/particlesystem.cpp 2010-01-18 23:27:55 UTC (rev 6261)
@@ -66,7 +66,7 @@
//if(pos.x > virtual_width) pos.x -= virtual_width;
//if(pos.y > virtual_height) pos.y -= virtual_height;
- context.draw_surface(particle->texture, pos, z_pos);
+ context.draw_surface(particle->texture, pos, particle->angle, Color(1.0f, 1.0f, 1.0f), Blend(), z_pos);
}
context.pop_transform();
Modified: trunk/supertux/src/object/particlesystem.hpp
===================================================================
--- trunk/supertux/src/object/particlesystem.hpp 2010-01-18 22:41:35 UTC (rev 6260)
+++ trunk/supertux/src/object/particlesystem.hpp 2010-01-18 23:27:55 UTC (rev 6261)
@@ -55,6 +55,7 @@
public:
Particle() :
pos(),
+ angle(),
texture()
{}
@@ -62,6 +63,8 @@
{}
Vector pos;
+ // angle at which to draw particle
+ float angle;
SurfacePtr texture;
private:
Modified: trunk/supertux/src/object/snow_particle_system.cpp
===================================================================
--- trunk/supertux/src/object/snow_particle_system.cpp 2010-01-18 22:41:35 UTC (rev 6260)
+++ trunk/supertux/src/object/snow_particle_system.cpp 2010-01-18 23:27:55 UTC (rev 6261)
@@ -22,7 +22,22 @@
#include "supertux/globals.hpp"
#include "video/drawing_context.hpp"
-SnowParticleSystem::SnowParticleSystem()
+// TODO: tweak values
+namespace SNOW {
+static const float SPIN_SPEED = 60.0f;
+static const float WIND_SPEED = 30.0f; // max speed of wind will be randf(WIND_SPEED) * randf(STATE_LENGTH)
+static const float STATE_LENGTH = 5.0f;
+static const float DECAY_RATIO = 0.2f; // ratio of attack speed to decay speed
+static const float EPSILON = 0.5f; //velocity changes by up to this much each tick
+static const float WOBBLE_DECAY = 0.99f; //wobble decays exponentially by this much each tick
+static const float WOBBLE_FACTOR = 4 * .005f; //wobble approaches drift_speed by this much each tick
+}
+
+SnowParticleSystem::SnowParticleSystem() :
+ state(RELEASING),
+ timer(),
+ gust_onset(0),
+ gust_current_velocity(0)
{
snowimages[0] = Surface::create("images/objects/particles/snow2.png");
snowimages[1] = Surface::create("images/objects/particles/snow1.png");
@@ -30,6 +45,8 @@
virtual_width = SCREEN_WIDTH * 2;
+ timer.start(.01);
+
// create some random snowflakes
size_t snowflakecount = size_t(virtual_width/10.0);
for(size_t i=0; i<snowflakecount; ++i) {
@@ -39,14 +56,19 @@
particle->pos.x = systemRandom.randf(virtual_width);
particle->pos.y = systemRandom.randf(SCREEN_HEIGHT);
particle->anchorx = particle->pos.x + (systemRandom.randf(-0.5, 0.5) * 16);
+ // drift will change with wind gusts
particle->drift_speed = systemRandom.randf(-0.5, 0.5) * 0.3;
particle->wobble = 0.0;
particle->texture = snowimages[snowsize];
+ particle->flake_size = powf(snowsize+3,4); // since it ranges from 0 to 2
- particle->speed = 1 + (2 - snowsize)/2 + systemRandom.randf(1.8);
- particle->speed *= 20; // gravity
+ particle->speed = 2 * (1 + (2 - snowsize)/2 + systemRandom.randf(1.8)) * 10; // gravity
+ // Spinning
+ particle->angle = systemRandom.randf(360.0);
+ particle->spin_speed = systemRandom.randf(-SNOW::SPIN_SPEED,SNOW::SPIN_SPEED);
+
particles.push_back(particle);
}
}
@@ -63,19 +85,60 @@
void SnowParticleSystem::update(float elapsed_time)
{
+ // Simple ADSR wind gusts
+
+ if (timer.check()) {
+ // Change state
+ state = (State) ((state + 1) % MAX_STATE);
+
+ if(state == RESTING) {
+ // stop wind
+ gust_current_velocity = 0;
+ // new wind strength
+ gust_onset = systemRandom.randf(-SNOW::WIND_SPEED, SNOW::WIND_SPEED);
+ }
+ timer.start(systemRandom.randf(SNOW::STATE_LENGTH));
+ }
+
+ // Update velocities
+ switch(state) {
+ case ATTACKING:
+ gust_current_velocity += gust_onset * elapsed_time;
+ break;
+ case DECAYING:
+ gust_current_velocity -= gust_onset * elapsed_time * SNOW::DECAY_RATIO;
+ break;
+ case RELEASING:
+ // uses current time/velocity instead of constants
+ gust_current_velocity -= gust_current_velocity * elapsed_time / timer.get_timeleft();
+ break;
+ case SUSTAINING:
+ case RESTING:
+ //do nothing
+ break;
+ default:
+ assert(false);
+ }
+
std::vector<Particle*>::iterator i;
for(i = particles.begin(); i != particles.end(); ++i) {
SnowParticle* particle = (SnowParticle*) *i;
float anchor_delta;
+ // Falling
particle->pos.y += particle->speed * elapsed_time;
- particle->pos.x += particle->wobble * elapsed_time /* * particle->speed * 0.125*/;
-
+ // Drifting (speed approaches wind at a rate dependent on flake size)
+ particle->drift_speed += (gust_current_velocity - particle->drift_speed) / particle->flake_size + systemRandom.randf(-SNOW::EPSILON,SNOW::EPSILON);
+ particle->anchorx += particle->drift_speed * elapsed_time;
+ // Wobbling (particle approaches anchorx)
+ particle->pos.x += particle->wobble * elapsed_time;
anchor_delta = (particle->anchorx - particle->pos.x);
- particle->wobble += (4 * anchor_delta * 0.05) + systemRandom.randf(-0.5, 0.5);
- particle->wobble *= 0.99f;
- particle->anchorx += particle->drift_speed * elapsed_time;
+ particle->wobble += (SNOW::WOBBLE_FACTOR * anchor_delta) + systemRandom.randf(-SNOW::EPSILON, SNOW::EPSILON);
+ particle->wobble *= SNOW::WOBBLE_DECAY;
+ // Spinning
+ particle->angle += particle->spin_speed * elapsed_time;
+ particle->angle = fmodf(particle->angle, 360.0);
}
}
Modified: trunk/supertux/src/object/snow_particle_system.hpp
===================================================================
--- trunk/supertux/src/object/snow_particle_system.hpp 2010-01-18 22:41:35 UTC (rev 6260)
+++ trunk/supertux/src/object/snow_particle_system.hpp 2010-01-18 23:27:55 UTC (rev 6261)
@@ -18,6 +18,7 @@
#define HEADER_SUPERTUX_OBJECT_SNOW_PARTICLE_SYSTEM_HPP
#include "object/particlesystem.hpp"
+#include "supertux/timer.hpp"
class SnowParticleSystem : public ParticleSystem
{
@@ -41,14 +42,44 @@
float anchorx;
float drift_speed;
+ // Turning speed
+ float spin_speed;
+
+ // for inertia
+ unsigned int flake_size;
+
SnowParticle() :
speed(),
wobble(),
anchorx(),
- drift_speed()
+ drift_speed(),
+ spin_speed(),
+ flake_size()
{}
};
+ // Wind is simulated in discrete "gusts"
+
+ // Gust state
+ enum State {
+ ATTACKING,
+ DECAYING,
+ SUSTAINING,
+ RELEASING,
+ RESTING,
+ MAX_STATE
+ };
+ State state;
+
+
+ // Gust state delay timer
+ Timer timer;
+
+ // Peak magnitude of gust is gust_onset * randf(5)
+ float gust_onset,
+ // Current blowing velocity of gust
+ gust_current_velocity;
+
SurfacePtr snowimages[3];
private:
More information about the Supertux-Commit
mailing list