-3
I’m trying to implement the equations for the two-circle collision response in a program of mine, but all of them either made the ball speed incredibly high or only works 50% of the time (in the other 50 balls just wanted to go further towards each other instead of colliding) someone can say what is wrong or simply pass an implementation of the equations for me to adapt to my program?
the code I’m using (which works 50% of the time):
var v1 = Math.hypot(vx1, vy1);
var v2 = Math.hypot(vx2, vy2);
var theta1 = v1 === 0 ? 0 : vy1 >= 0 ? Math.acos(vx1 / v1) : 3 * Math.PI / 2 + Math.asin(vx1 / v1);
var theta2 = v2 === 0 ? 0 : vy2 >= 0 ? Math.acos(vx2 / v2) : 3 * Math.PI / 2 + Math.asin(vx2 / v2);
var phi = y1 - y2 >= 0 ? Math.acos((x1 - x2) / v2) : 3 * Math.PI / 2 + Math.asin((x1 - x2) / v2);
var termo1 = (v1 * Math.cos(theta1 - phi) * (m1 - m2) + 2 * m2 * v2 * Math.cos(theta2 - phi)) / (m1 + m2);
proximo_vx1 = termo1 * Math.cos(phi) + v1 * Math.sin(theta1 - phi) * Math.cos(phi + Math.PI / 2);
proximo_vy1 = termo1 * Math.sin(phi) + v1 * Math.sin(theta1 - phi) * Math.sin(phi + Math.PI / 2);
the first equation is the one used above (50% of the times), the second equation is the one that accelerates the balls too
screenshot of Wikipedia (https://en.wikipedia.org/wiki/Elastic_collision, section on collisions between sieves):
Please reduce your question to a Javascript code issue. For example, why a certain expression (simpler than the ones you asked) that should result in a certain value is giving another value that was not expected. There will probably be an answer here on the site.
– Piovezan