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, ...
Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.