Time for action - creating explosions

  1. Add the RandomDirection() helper method to the ExplosionManager class:
    public Vector2 randomDirection(float scale)
    {
    Vector2 direction;
    do
    {
    direction = new Vector2(
    rand.Next(0, 101) - 50,
    rand.Next(0, 101) - 50);
    } while (direction.Length() == 0);
    direction.Normalize();
    direction *= scale;
    return direction;
    }
    
  2. Add the AddExplosion() method to the ExplosionManager class:
    public void AddExplosion(Vector2 location, Vector2 momentum)
    {
    Vector2 pieceLocation = location -
    new Vector2(pieceRectangles[0].Width/2,
    pieceRectangles[0].Height/2);
    int pieces = rand.Next(minPieceCount, maxPieceCount + 1);
    for (int x = 0; x < pieces; x++)
    {
    ExplosionParticles.Add(new Particle(
    pieceLocation,
    texture,
    pieceRectangles[rand.Next(0,pieceRectangles.Count)],
    randomDirection(pieceSpeedScale) + momentum,
    Vector2.Zero,
    explosionMaxSpeed,
    durationCount,
    initialColor,
    finalColor));
    }
    int points = rand.Next(minPointCount, maxPointCount + 1);
    for (int x = 0; x < points; x++)
    {
    ExplosionParticles.Add(new Particle(
    location,
    texture,
    pointRectangle,
    randomDirection((float)rand.Next(
    pointSpeedMin, pointSpeedMax)) + momentum,
    Vector2.Zero,
    explosionMaxSpeed,
    durationCount,
    initialColor,
    finalColor));
    }
    }
    

What just happened?

The RandomDirection() helper method generates random X and Y coordinates each between -50 and 50, and uses them to create a vector representing a direction. Because it is technically possible that the resulting vector will be a zero vector (0, 0) that cannot be normalized (and would represent no direction at all), we enclose the generation code in a do...while loop, which simply checks the generated vector for a length other than zero before continuing. Once an appropriate vector is generated, it is normalized and multiplied by the scale passed to the method and returned.

When an explosion is added, the AddExplosion() method is passed a vector pointing to the center point of the explosion. Since we draw sprites as rectangles that start in the upper left corner of the sprite, we need to compensate for the size of the larger explosion pieces and keep them centered on the explosion point. The pieceLocation vector is calculated by subtracting half of the height and width of the rectangle for the larger pieces from the center point of the explosion.

Next, a random number of pieces are generated, each added as a new particle to the ExplosionParticles list. The first four parameters of the constructor for the Particle class are the same as for any of our other sprites: location, texture, initial frame, and velocity. In the case of velocity, the randomDirection() method is used to generate a random vector at the speed of pieceSpeedScale to move the piece slowly away from the center point of the explosion. To this random vector, we add the vector passed to the AddExplosion() method (momentum) that represents the portion of the velocity of the original object that is exploding, that the explosion should retain. Without a small amount of momentum, the explosion associated with a moving object would stop dead at the point of the explosion. By adding the momentum to the explosion we can create a drifting effect, where the entire explosion moves in the same direction as the original object.

Our particles will not have any built-in acceleration in this case, so Vector2.Zero is passed for the acceleration parameter. The remaining parameters of the Particle constructor pass in the named variables representing the duration of the explosion and the starting and ending tint colors for the sprite.

After each of the pieces is generated, an identical process is executed to generate the point sprites for the explosion, with the only difference being that the point sprites move at a higher speed away from the center of the explosion, with a randomization factor to make them look more realistic.

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

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