Time for action – bouncing asteroids – part 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 by the cOfMass
vector variable. This value is determined by adding the velocities of the two colliding...