2d ball collision code problem XNA, over accelerated balls and stick together sometimes. help please? [closed]
- by Sivan
public static void Collision(Ball ball1, Ball ball2)
{
Vector3 x = new Vector3((ball1.BallPosition.X - ball2.BallPosition.X), (ball1.BallPosition.Y - ball2.BallPosition.Y), 0);
x.Normalize();
Vector3 v1 = new Vector3(ball1.Speed, 0);
float x1 = Vector3.Dot(x, v1);
Vector3 v1x = x * x1;
Vector3 v1y = v1 - v1x;
x = -x;
Vector3 v2 = new Vector3(ball2.Speed, 0);
float x2 = Vector3.Dot(x, v2);
Vector3 v2x = x * x2;
Vector3 v2y = v2 - v2x;
float m1 = 12, m2 = 4;
float combinedMass = m1 + m2;
Vector3 newVelA = (v1x * ((m1 - m2) / combinedMass)) + (v2x * ((2f * m2) / combinedMass)) + v1y;
Vector3 newVelB = (v1x * ((2f * m1) / combinedMass)) + (v2x * ((m2 - m1) / combinedMass)) + v2y;
ball1.Speed = new Vector2(newVelA.X, newVelA.Y);
ball2.Speed = new Vector2(newVelB.X,newVelB.Y );
}