Search code examples
javaalgorithmgeometrycollision-detectionphysics

Glancing collision between circles


I am modelling objects in a game prototype as circles. Each object has a mass, radius, velocity, and location. No two objects have the same mass and radius.

I have to following code which is supposed to calculate an elastic collision between them, but I've noticed that the results are always as if the circles had hit each other dead-on.

private void handleCollision(JumpObject a, JumpObject b) {
        double newVelAX = (a.getVelocity().x * (a.getMass() - b.getMass()) + (2 * b.getMass() * b.getVelocity().x)) 
                / (a.getMass() + b.getMass());
        double newVelAY = (a.getVelocity().y * (a.getMass() - b.getMass()) + (2 * b.getMass() * b.getVelocity().y)) 
                / (a.getMass() + b.getMass());
        double newVelBX = (b.getVelocity().x * (b.getMass() - a.getMass()) + (2 * a.getMass() * a.getVelocity().x)) 
                / (a.getMass() + b.getMass());
        double newVelBY = (b.getVelocity().y * (b.getMass() - a.getMass()) + (2 * a.getMass() * a.getVelocity().y)) 
                / (a.getMass() + b.getMass());

        a.getVelocity().setLocation(newVelAX, newVelAY);
        b.getVelocity().setLocation(newVelBX, newVelBY);
}

I need to deal with the case where one of the circles hits another at a glancing angle (I can do a diagram later if it's needed), such as when a small circle just barely nicks the edge of a larger circle as it goes by.

How do I modify my collision algorithm to account for where one circle hits the other?


Solution

  • Please see the references.

    circle-circle collision

    Ball to Ball Collision - Detection and Handling

    http://gamedev.tutsplus.com/tutorials/implementation/when-worlds-collide-simulating-circle-circle-collisions/

    http://processing.org/examples/circlecollision.html