Advertisement
Guest User

Ball-on-Ball action

a guest
Dec 4th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. foreach (Ball ball in balls)
  2.             {
  3.                 foreach (Ball otherBall in balls)
  4.                 {
  5.                     if (ball != otherBall)
  6.                     {
  7.                         // TODO: Put your collision handling with another ball code here
  8.                         if (ball.BoundingSphere.Intersects(otherBall.BoundingSphere))
  9.                         {
  10.                             Console.WriteLine("Ball/ball collision");
  11.  
  12.                             Vector3 difference = otherBall.Position - ball.Position;    // get vector between 2 ball centers                            
  13.  
  14.                             // calculate vector components
  15.                             difference.Normalize();
  16.                             float a1 = Vector3.Dot(ball.Velocity, difference);
  17.                             float a2 = Vector3.Dot(otherBall.Velocity, difference);
  18.  
  19.                             // using radius as mass, make the equations play nice
  20.                             float p = (2 * (a1 - a2)) / (ball.BoundingSphere.Radius + otherBall.BoundingSphere.Radius);
  21.  
  22.                             ball.Velocity = ball.Velocity - p * otherBall.BoundingSphere.Radius * difference;
  23.                             otherBall.Velocity = otherBall.Velocity + p * ball.BoundingSphere.Radius * difference;
  24.                         }
  25.                     }
  26.                 }
  27.             }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement