1
\$\begingroup\$

I'm writing a really basic collision detection system (AABB vs AABB, AABB vs Circle, Circle vs Circle), and I've just got to the point of writing the actual loop which iterates the objects and checks for collisions.

My question is when do I apply the velocity to each object? Do I move all objects by vel * dt before I even start colliding? Do I apply the velocity as I iterate? If so, do I apply it to both objects I'm testing, or just the first, and then apply the velocity to the second when I check collisions against that one?

Also presumably I should do the whole collision check recursively (as the act of responding to one collision might cause another), but that obviously effects how and when the velocity is applied.

My suspicion is that I should move all the objects by their velocity before I begin, and then recursively detect and respond to collisions until nothing is colliding (or until some loop limit).

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The way I would go about it (did it in a few small games) would be to first move all objects, then detect and possibly correct collisions, and then draw.

If you collide as you move the objects, you will end up with inconsistent states: if A has been moved and B has yet to be moved, you might detect and collision between A and B to does not actually exist once B has been moved.

If you correct collisions first, you might have an issue: You are, effectively, correcting collision that were drawn one frame ago, meaning that the player might actually see objects overlapping during one frame before disappearing.

If you move (apply velocity), then collide and correct, and finally draw, you make sure that no overlapping/invalid state is drawn to the screen.

Hope it helps :)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .