# Thread: 2D Ball Collision (velocity vector calculation)

1. ## 2D Ball Collision (velocity vector calculation)

Hey,

I'm working on a project for one of my classes - while the part I'm stuck on isn't centric to the project, it is indeed a part, which is why I'm telling you about it initially.

In said project (it's in C), I have to simulate a collision of 2 balls in 2D space. Now assume all the collision detection and such has already been done (it is), I'm having trouble calculating the new velocity vectors (I'm pretty crappy at math, haha).

I have 2 versions where I attempt to calculate the exidence vectors.
Code:
```
// Calculate the ball deltaX and deltaY
float dX = balls[i].X - balls[j].X;
float dY = balls[i].Y - balls[j].Y;

// Normalize each delta
float normalDX = dX / sqrt(pow(dX, 2) + pow(dY, 2));
float normalDY = dY / sqrt(pow(dX, 2) + pow(dY, 2));

// Calculate dot product and get each magnitude
float magnitude_I = (balls[i].velocityX * normalDX) + (balls[i].velocityY * normalDY);
float magnitude_J = (balls[j].velocityX * normalDX) + (balls[j].velocityY * normalDY);

// Apply initial magnitudes to opposite balls
// (Collision is perfectly elastic)
float I_final_mag = magnitude_J;
float J_final_mag = magnitude_I;

// Apply velocities to X/Y components of each ball
balls[i].velocityX += (I_final_mag - magnitude_I) * dX;
balls[i].velocityY += (I_final_mag - magnitude_I) * dY;

balls[j].velocityX += (J_final_mag - magnitude_J) * dX;
balls[j].velocityY += (J_final_mag - magnitude_J) * dY;```
Code:
```float dX = balls[i].X - balls[j].X;
float dY = balls[i].Y - balls[j].Y;

// Normalize each delta
float normalDX = dX / sqrt(pow(dX, 2) + pow(dY, 2));
float normalDY = dY / sqrt(pow(dX, 2) + pow(dY, 2));

// Calculate dot product and get each magnitude
float magnitude_I = (balls[i].velocityX * normalDX) + (balls[i].velocityY * normalDY);
float magnitude_J = (balls[j].velocityX * normalDX) + (balls[j].velocityY * normalDY);

// Get the X and Y components of resultant magnitudes for the change in velocity
float newDY_I = sin(magnitude_I);
float newDX_I = cos(magnitude_I);
float newDY_J = sin(magnitude_J);
float newDX_J = cos(magnitude_J);

// Apply the changes in velocity to the balls
balls[i].velocityX += newDX_I;
balls[i].velocityY += newDY_I;
balls[j].velocityX += newDX_J;
balls[j].velocityY += newDY_J;```
Where:
- I/J are just counters, representing different balls
- velocityX/velocityY are the X/Y components of the velocity
- X/Y are the locations

All the examples I've found of calculating the new vector are in OO or Java which are blackboxing the vector math.

Both of these solutions yield crappy results, I'm not sure where the issue is, I'm not sure if it's something I'm doing wrong in the implementation, or something wrong with my thought process, but something is definitely wrong.

My thought process for this is:
1. Get vector between 2 ball centers
2. Normalize vector
3. Take dot product of that and the velocity vector
4. Apply resultant to X/Y components of velocity
- WHere 3/4 must happen for both balls.

Maybe somebody sees something that's obviously wrong and can point me in the right direction or something? Not looking for answers here, just help.

TIA.

2. ( Just noticed this thread, sorry )

Let's see, the math for sphere elastic collision( without rotational forces ) are:
Let Norm1 be a unit reflection of Sphere1's velocity vector onto the collision plane
Let Norm2 be a unit reflection of Sphere2's velocity vector onto the collision plane

Let Vel1 be the velocity vector of Sphere1
Let Vel2 be the velocity vector of Sphere2

Let Mass1 be the mass of Sphere1
Let Mass2 be the mass of Sphere2

Let P1, P2 be the momentums(scalars) of Sphere1, Sphere2 respectively ( Momentum = Mass * Velocity )
P1 = Mass1 * |Vel1|
P2 = Mass2 * |Vel2|

Let A1 be the acceleration of Sphere1 on the time of collision
Let A2 be the acceleration of Sphere2 on the time f collision
Let Time be the amount of time both sphere's retain contact
( Force = Mass * acceleration && Force = Momentum / Time <=> acceleration = Momentum / (Mass * Time) )
A1 = P2 / (Mass1 * Time)
A2 = P1 / (Mass2 * Time)

Then newVel1 = Vel1 + A1 * Time = Vel1 + (P2 / Mass1) * Norm1
And newVel2 = Vel2 + A2 * Time = Vel2 + (P1 / Mass2) * Norm2
Hopefully I didn't confuse you.
Also hopefully, I didn't do anything wrong... ^_^

Popular pages Recent additions