I try to get elastic collision of two particles calculated, that means the velocity of both particles after collision. It is based on this:

The issue is, that the results are not correct. In an elastic collision, when the mass of both particles is the same, the magnitudes of velocities should swap to conserve impulse.
That means vbnew should equal va and vice versa.
However, results I get look like this:
Magnitude of va (before coll) and vanew (after coll) 151.18777 132.975
Magnitude of vb (before coll) and vbnew (after coll) 94.70846 118.93311

I am not sure, if the whole function is wrong, or if it is just something in it, I have to address.

This is the function I use:

//Particle A
//A1 position at collision
//va - original velocity
//vanew - velocity after collision
//ma - Mass of particle
//Particle B
//B1 position at collision
//vb - original velocity
//vbnew - velocity after collision
//mb - Mass of particle
PVector vecx = PVector.sub(A1, B1);
vecx.normalize();
PVector vecv1 = va.copy();
float x1 = PVector.dot(vecx, vecv1);
PVector vecv1x = new PVector();
PVector.mult(vecx,x1,vecv1x);
PVector vecv1y = PVector.sub(vecv1, vecv1x);
PVector vecx2 = vecx.mult(-1);
PVector vecv2 = vb.copy();
float x2 = vecx2.dot(vecv2);
PVector vecv2x = new PVector();
PVector.mult(vecx2,x2,vecv2x);
PVector vecv2y = PVector.sub(vecv2, vecv2x);
float consta1 = (ma - mb)/(ma+mb);
float consta2 = 2* mb / (ma+mb);
float constb1 = 2 * ma / (ma+mb);
float constb2 = (mb - ma)/(ma+mb);
PVector vanew = new PVector();
PVector.mult(vecv1x,consta1,vanew);
PVector inta1 = new PVector();
PVector.mult(vecv2x,consta2,inta1);
vanew.add(inta1);
vanew.add(vecv1y);
PVector vbnew = new PVector();
PVector.mult(vecv1x, constb1,vbnew);
PVector intb1 = new PVector();
PVector.mult(vecv2x,constb2,intb1);
vbnew.add(intb1);
vbnew.add(vecv2y);

function doBounce( r1, r2 ) {
var f = 0;
var dx = (r2.x - r1.x + 1.5) % 1 - 0.5;
var dy = (r2.y - r1.y + 1.5) % 1 - 0.5;
var d2 = dx*dx+dy*dy;
if( d2 < sq(r1.radius+r2.radius) ) {
// bump
var d = sqrt(d2);
dx /= d;
dy /= d;
var dvx = r2.vx - r1.vx;
var dvy = r2.vy - r1.vy;
var vn = dx * dvx + dy * dvy;
if( vn < 0 ) {
f = 2 * vn / (r1.invMass + r2.invMass);
r1.vx += f * r1.invMass * dx;
r1.vy += f * r1.invMass * dy;
r2.vx -= f * r2.invMass * dx;
r2.vy -= f * r2.invMass * dy;
}
}
return -f;
}

The dx and dy look weird because my world wraps. They are just the position of one object minus the other. vn is the velocity normal (perpendicular) to the motion of the two objects. It is only the normal velocity that changes.

In physics simulations, itâ€™s usually easier to work with inverse mass (1.0 / mass) since then you can set it to 0 to create an object with infinite mass.

Thank you for the information. I translated your code into 3D and it works but it gives exactly the same results as my current code. I still donâ€™t get a full swap of velocity magnitudes as I would expect, but maybe I am just thinking wrong, that I would need to see that for an elastic collision in 3D.

I agree though that the vector calculation is sometimes cumbersome. Every time I have to track every vector step by step to make sure, it keeps its value throughout the calculations.

Are you doing pure elastic collisions without friction or air resistance?

If you put one ball at the origin with 0 velocity and smack it head-on with another of the same mass, does the incoming one stop and give all the energy to the other? Because thatâ€™s what should happen with that function and does appear to happen in the game I linked above.

Since the two collision functions give the same result, if there is indeed a problem, I would assume itâ€™s elsewhere. When you resolve the collision, are you computing its exact time so that you know the two objects are not inter-penetrating and youâ€™re not somehow triggering an immediate second collision? Thatâ€™s a problem more with non-event-based collision systems where objects penetrate, you collide them, but because they are still overlapping, they collide again the next frame causing incorrect vibrations. Your event system would presumably not have that problem.

I did so, but there is little to no energy transfer. There is nothing interfering with the calculation or additonal input.
I just took the data from the program and recalculated by hand and it gives the same result in that sense, that not one of the particles stops:

Just to make sure you have the basics working, try setting the invMass to 1.0, the radius to 0.5, put r1 at (0,0,0) with 0 velocity and r2 at (-0.999,0,0) with velocity (1,0,0). Then test with r2 at (0,0.999,0) with (0,-1,0) velocity and then again along z. In each case, r2â€™s velocity should result in 0 and r1 should have r2â€™s initial velocity.

Thanks for your patience
Yes, that works. And it also works for (0.499, 0.499,0) as r1 with velocity(-1,-1,0) and (0.3,0.3,0.3) as r1 with velocity (-1,-1,-1).

Where it breaks down is, when any of x,y,z of velocity has the opposite sign. Furthermore, location and the velocity have to be aligned. (0.3,0.9,0.1) as location works but only when the velocities change in sync to (-1,-3,-0.33). As soon as any of these velocities have a different ratio between x,y and z as the location the results are not correct anymore.

Could it have to be with the fact, that one has to identify a plane both trajectories are located in, which is given in 2D?

(dx, dy, dz) is the vector from one ball center to the other and then when divided by d it becomes a unit vector. (dvx, dvy, dvz) is the relative velocity of r2 with respect to r1. When you take the dot product of those vectors, vn gives the amount of velocity perpendicular to the plane of contact between the two balls. It is only that perpendicular velocity that changes. Any sideways velocity of either ball (parallel to the plane of contact) is unaffected by the collision. So, the only time you should see a perfect swap of their velocities is when you have a perfect head-on collision with no relatively sideways velocity.

For instance, with radius of 0.5, put r1 at the origin with 0 velocity. Put r2 at (1, 0, 0) and a velocity of (-1, anyY, anyZ). After the collision, r1 should have a velocity of (-1, 0, 0) and r2 should have (0, anyY, anyZ). Since (dx, dy, dz) was (1, 0, 0), it is only along that vector that the velocities change.

If youâ€™re not getting that result, double-check that you have all the x,y,z suffixes and +/- signs exactly correct.

That works as well. Get the correct coordinates, which you did predict.
But based on your explanations, maybe the results I receive are correct and the only â€śissueâ€ť is, that it is not a head-on collision and therefore it looks weird.
For your example, I receive now as velocity magnitudes
before r1v=0 und r2v=10.1 and after r1v=1 and r2v=10.05
That looks odd, however, coordinates are exactly as you describe.

r2 traded the x-compoent of its velocity with r1. r2v went from (-1, 10, -1) with a magnitude of sqrt(102) = 10.0995 to (0, 10, -1) with a magnitude of sqrt(101) = 10.0499. Looks right to me.

Have you visualized it yet? Iâ€™ll bet it looks right when you see it.