Time for action - updating and drawing particles

  1. Add an Update() method to the Particle class:
    public override void Update(GameTime gameTime)
    {
    if (IsActive)
    {
    velocity += acceleration;
    if (velocity.Length() > maxSpeed)
    {
    velocity.Normalize();
    velocity *= maxSpeed;
    }
    TintColor = Color.Lerp(
    initialColor,
    finalColor,
    DurationProgress);
    remainingDuration--;
    base.Update(gameTime);
    }
    }
    
  2. Add a Draw() method to the Particle class:
    public override void Draw(SpriteBatch spriteBatch)
    {
    if (IsActive)
    {
    base.Draw(spriteBatch);
    }
    }
    

What just happened?

Both the Update() and the Draw() methods override the Sprite class' methods of the same name because we need to alter the behaviour associated with them. Both methods will only execute any code if the IsActive property returns true. In fact, for the Draw() method, this is the only reason we override the method to prevent drawing a particle that has expired.

In the Update() method, we add the acceleration vector to the velocity vector, and then check to see if the length of the velocity vector has exceeded the maximum speed at which the particle is allowed to move. If it has, the velocity vector is normalized and then multiplied by the maximum speed, resulting in a vector pointing in the same direction at the maximum length.

The TintColor property will be reset in each frame using the Lerp() method of the Color class. Lerp returns a color between the two colors provided as parameters, that is scaled towards one color or the other based on the value of the third parameter, that ranges from zero to one. A zero value would return the first color parameter, while a one would return the second. Values between zero and one return a mixing of the two colors along a linear scale.

Tip

Lerping

The term "Lerp" is a rough acronym for "linear interpolation", a method to progress between two known points based on a reference value. For example, if you were to lerp between 1 and 10, with a control of 50%, the result would be 5 (halfway between the initial and final values). The MathHelper class contains a Lerp() method that can be used for numerical lerping, and several XNA structures, including vectors, contain their own Lerp() methods as well.

In both the Update() and the Draw() methods, the base method of the sprite class is called as the last task each method performs before exiting.

Particle explosions

Now that we have the Particle class available to us, we can turn our attention to building particle-based explosions. In the SpriteSheet.png file, beginning at location (0, 100) are three frames of roughly circular, partially transparent spattering that we will use as the basis for our explosion effects.

When a new explosion is generated, we will create a random number of these images and an additional number of individual small dot-shaped particles to represent the explosion. These particles will be moved slowly away from their starting point in random directions, providing a gradually expanding explosion effect.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.134.118.95