FrankK
FrankK

Reputation: 511

Three.js circle to circle collision

I'm trying to implement circle to circle colission for a pool game. i'm trying to make the balls bounce apart in the right way when they collide. i've tried several tutorials and read multiple questions on stackoverflow but none really helped me.

my question: how can i implement circle to circle collision that will work in these two cases: 1: a moving ball colliding with a non moving ball. 2: a moving ball colliding with a moving ball

one of the tutorials i've tried:

tutorial

this is the code i currently have, it is in some way working but is extremely glitchy:

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 help is appreciated.

Upvotes: 0

Views: 196

Answers (1)

Lutz Lehmann
Lutz Lehmann

Reputation: 25992

In a collision of equal, non-rotating disks the disks just simply exchange their velocity vectors.

In your test on collision, you should also check that the original movement is towards each other to avoid spurious oscillations.

The position does not change during collision, you should remove the position update entirely.

Upvotes: 1

Related Questions