I've been working on incorporating circle to circle collision for a pool game, aiming to make the balls bounce apart correctly upon impact. Despite trying various tutorials and scouring multiple questions on stackoverflow, I haven't found a solution that works for me.
Specifically, my question is: how can I implement circle to circle collision that functions effectively in these scenarios? 1: A moving ball colliding with a stationary ball. 2: A moving ball colliding with another moving ball
One of the tutorials I experimented with can be found here.
The current code I'm using somewhat works but has noticeable glitches:
function newCollide(ball1, ball2)
{
a = ball1.position.x - ball2.position.x;
b = ball1.position.z - ball2.position.z;
ab = Math.sqrt(((a * a) + (b * b)));
if(ab <= 1.1)
{
console.log("collision");
ball2.speedX = ball1.speedX;
ball2.speedZ = ball1.speedZ;
ball1.speedX *= 0.3;
ball1.speedZ *= 0.3;
ball1.position.x += ball1.speedX;
ball1.position.z += ball1.speedZ;
ball2.position.x += ball2.speedX;
ball2.position.z += ball2.speedZ;
}
}
Any assistance would be greatly appreciated.