// InitializeParticle is overridden to add the appearance of wind. protected override void InitializeParticle(Particle particle, Vector2 where) { base.InitializeParticle(particle, where); // The base is mostly good, but we want to simulate a little bit of wind heading to the right. particle.Acceleration.X += Utils.RandomBetween(10, 50); }
protected override void InitializeParticle(Particle particle, Vector2 where) { base.InitializeParticle(particle, where); // The base works fine except for acceleration. Explosions move outwards, // then slow down and stop because of air resistance. Let's change acceleration // so that when the particle is at max lifetime, the velocity will be zero. // // We'll use the equation vt = v0 + (a0 * t). If you're not familar with this, // it's one of the basic kinematics equations for constant acceleration, and // basically says: velocity at time t = initial velocity + acceleration * t. // // We'll solve the equation for a0, using t = particle.Lifetime and vt = 0. particle.Acceleration = -particle.Velocity / particle.Lifetime; }
// Randomizes some properties for a particle, then calls Initialize on it. // This can be overriden by subclasses if they want to modify the way particles // are created. For example, SmokePlumeParticleSystem overrides this function // make all particles accelerate to the right, simulating wind. protected virtual void InitializeParticle(Particle particle, Vector2 where) { // First, call PickRandomDirection to figure out which way the particle // will be moving. Velocity and acceleration values will come from this. Vector2 direction = PickRandomDirection(); // Pick some random values for our particle. float velocity = Utils.RandomBetween(minInitialSpeed, maxInitialSpeed); float acceleration = Utils.RandomBetween(minAcceleration, maxAcceleration); float lifetime = Utils.RandomBetween(minLifetime, maxLifetime); float scale = Utils.RandomBetween(minScale, maxScale); float rotationSpeed = Utils.RandomBetween(minRotationSpeed, maxRotationSpeed); // Then initialize the particle with these random values. particle.Initialize(where, velocity * direction, acceleration * direction, lifetime, scale, rotationSpeed); }