Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foreach (Ball ball in balls)
- {
- foreach (Ball otherBall in balls)
- {
- if (ball != otherBall)
- {
- // TODO: Put your collision handling with another ball code here
- if (ball.BoundingSphere.Intersects(otherBall.BoundingSphere))
- {
- Console.WriteLine("Ball/ball collision");
- Vector3 difference = otherBall.Position - ball.Position; // get vector between 2 ball centers
- // calculate vector components
- difference.Normalize();
- float a1 = Vector3.Dot(ball.Velocity, difference);
- float a2 = Vector3.Dot(otherBall.Velocity, difference);
- // using radius as mass, make the equations play nice
- float p = (2 * (a1 - a2)) / (ball.BoundingSphere.Radius + otherBall.BoundingSphere.Radius);
- ball.Velocity = ball.Velocity - p * otherBall.BoundingSphere.Radius * difference;
- otherBall.Velocity = otherBall.Velocity + p * ball.BoundingSphere.Radius * difference;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement