/// <summary> /// InitializeParticle is overridden to add the appearance of wind. /// </summary> /// <param name="p">the particle to set up</param> /// <param name="where">where the particle should be placed</param> protected override void InitializeParticle(Particles p, Vector2 where, Vector2 direction, Vector2 shipPosition, float rotationShip, bool random) { base.InitializeParticle(p, where, direction, shipPosition, rotationShip, random); // the base is mostly good, but we want to simulate a little bit of wind // heading to the right. p.Acceleration.X += Synthesis.Game1.RandomBetween(10, 50); }
protected override void InitializeParticle(Particles p, Vector2 where, Vector2 direction, Vector2 shipPosition, float rotationShip, bool random) { base.InitializeParticle(p, where, direction, shipPosition, rotationShip, random); // 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 = p.Lifetime and vt = 0. p.Acceleration = -p.Velocity / p.Lifetime; }
/// <summary> /// InitializeParticle randomizes some properties for a particle, then /// calls initialize on it. It 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. /// </summary> /// <param name="p">the particle to initialize</param> /// <param name="where">the position on the screen that the particle should be /// </param> protected virtual void InitializeParticle(Particles p, Vector2 where, Vector2 direction2, Vector2 shipPosition, float rotationShip, bool random) { // first, call PickRandomDirection to figure out which way the particle // will be moving. velocity and acceleration's values will come from this. Vector2 direction; if (random == false) { direction = direction2; } else { direction = PickRandomDirection(); } // pick some random values for our particle float velocity = Synthesis.Game1.RandomBetween(minInitialSpeed, maxInitialSpeed); float acceleration = Synthesis.Game1.RandomBetween(minAcceleration, maxAcceleration); float lifetime = Synthesis.Game1.RandomBetween(minLifetime, maxLifetime); float scale = Synthesis.Game1.RandomBetween(minScale, maxScale); float rotationSpeed = Synthesis.Game1.RandomBetween(minRotationSpeed, maxRotationSpeed); // then initialize it with those random values. initialize will save those, // and make sure it is marked as active. p.Initialize(where, velocity * direction, acceleration * direction, lifetime, scale, rotationSpeed, color, shipPosition, rotationShip); }
/// <summary> /// override the base class's Initialize to do some additional work; we want to /// call InitializeConstants to let subclasses set the constants that we'll use. /// /// also, the particle array and freeParticles queue are set up here. /// </summary> public override void Initialize() { InitializeConstants(); // calculate the total number of particles we will ever need, using the // max number of effects and the max number of particles per effect. // once these particles are allocated, they will be reused, so that // we don't put any pressure on the garbage collector. particles = new Particles[howManyEffects * maxNumParticles]; freeParticles = new Queue<Particles>(howManyEffects * maxNumParticles); for (int i = 0; i < particles.Length; i++) { particles[i] = new Particles(); freeParticles.Enqueue(particles[i]); } base.Initialize(); }