SpaceFace
SpaceFace

Reputation: 1552

Collision detection and bouncing

I'm playing around a bit and trying to get some things to work for a later project, but I have a problem that I can't seem to find a solution to.

Right now I have working and accurate collision detection, but the problem is creating the physics. The physics are simple right now, an object has a vector for it's direction and speed, when there is a collision the vector is inverted.

The problem is that when objects collide they get shoved into each other and end up inverting the vector forever causing strange results. E.g. magically floating upwards, or appearing to ignore the physics entirely.

Could anyone help me out?

EDIT: Detection is done by separating axis theorem and I didn't put code because all that really goes on is there is a loop to check for collisions, when one is found the vectors are inverted. Like I said, the actual detection works fine, I think my problem is as Sibbo said with moving the objects so they don't collide anymore.

Upvotes: 1

Views: 1268

Answers (4)

G. Bach
G. Bach

Reputation: 3909

I recently wrote the physics for a billard game for a university project (so while this possibly isn't code that's up to industry standards, it does what it's supposed to); what we ended up doing to prevent endless loops of calculating the effects of an identical collision was this: we stored all collisions in a java.util.List and whenever the next collision was being calculated, we checked whether both objects taking part in the collision were already part of the last collision that had been calculated; if that was the case, that object-pair was ignored.

This has an up- and a downside: upside is, you don't introduce inaccuracies by using a (minimal) offset movement so the objects won't touch anymore. Downside is, you can't have two collisions between two identical objects one after the other (for example, you can't have [(ball_1,ball_2); (ball_1, ball_2); ...], you can only have something like [(ball_1, ball_2); (ball_1, table); (ball_1, ball_2); ... ] as your list of collisions).

Upvotes: 1

Peter Lawrey
Peter Lawrey

Reputation: 533790

Each object should have a size and when they bounce off the edges of each other they bounce off a line which bi-secs the line between them. Are you assuming all the objects have the same mass?

Upvotes: 0

cdnza
cdnza

Reputation: 67

Move the objects slightly apart (outside the threshold of collision) as well as inverting the velocity vector. Assuming you are using a vector for position as well, just alter its values such that the objects are no longer considered to be colliding.

Upvotes: 1

Sibbo
Sibbo

Reputation: 3914

I don't know your implementation, but when you detect a collision, you should move the objects so that they don't touch anymore. That will prevent your program to enter an endless-collision-loop

Upvotes: 1

Related Questions