O'Reilly logo

XNA 4.0 Game Development by Example Beginner's Guide by Kurt Jaegers

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - bouncing asteroids part 1

  1. Add the BounceAsteroids() method to the AsteroidManager class:
    private void BounceAsteroids(Sprite asteroid1, Sprite asteroid2)
    {
    {
    Vector2 cOfMass = (asteroid1.Velocity +
    asteroid2.Velocity) / 2;
    Vector2 normal1 = asteroid2.Center - asteroid1.Center;
    normal1.Normalize();
    Vector2 normal2 = asteroid1.Center - asteroid2.Center;
    normal2.Normalize();
    asteroid1.Velocity -= cOfMass;
    asteroid1.Velocity =
    Vector2.Reflect(asteroid1.Velocity, normal1);
    asteroid1.Velocity += cOfMass;
    asteroid2.Velocity -= cOfMass;
    asteroid2.Velocity =
    Vector2.Reflect(asteroid2.Velocity, normal2);
    asteroid2.Velocity += cOfMass;
    }
    }
    

What just happened?

We begin by calculating the velocity of the center of mass for the collision, represented ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required