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;

Where: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;

- 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:- WHere 3/4 must happen for both balls.

- Get vector between 2 ball centers
- Normalize vector
- Take dot product of that and the velocity vector
- Apply resultant to X/Y components of velocity

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.