2

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?

user1118321
  • 25,567
  • 4
  • 55
  • 86
Mar
  • 7,765
  • 9
  • 48
  • 82

1 Answers1

4

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

Community
  • 1
  • 1
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • These are good references. The third one, at gamedev.tutsplus, actually reproduces exactly the problem I'm having. If you watch their last demo running, you'll notice that glancing collisions do the same thing as direct ones. Processing's circle collision code seems to have the stuff I need. – Mar Aug 20 '13 at 04:25
  • My final solution involved importing a `Vector2D` class to handle some of the math. I can figure out the vector math, but doing all the trig as well was getting me mixed up. – Mar Aug 25 '13 at 04:53
  • @MartinCarney Looks like the problem with the simulation at gamedev.tutsplus has been fixed. – Brian McCutchon Nov 23 '14 at 04:54