Time for action – bouncing Asteroids – part 1
Add the
BounceAsteroids()
method to theAsteroidManager
class:Private Sub BounceAsteroids(asteroid1 As Sprite, asteroid2 As Sprite) Dim cOfMass As Vector2 = (asteroid1.Velocity + asteroid2.Velocity) / 2 Dim normal1 As Vector2 = asteroid2.Center - asteroid1.Center normal1.Normalize() Dim normal2 As Vector2 = 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 End Sub
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 objects, and then dividing by the total mass...