3
\$\begingroup\$

There's many articles on the internet focusing on how to implement movement, collision detection, or collision resolution. What I'm wondering is how to put them together, specifically in what order to perform them.

Should one iterate through all physics objects and do movement, collision detection and resolution for each (approach A), or perform movement for all objects, then detect all collisions, and finally resolve all collisions (approach B), or some combination thereof? Is there an approach that is universally superior? If not, what are the pros and cons for each approach (including ones I haven't listed)?

Approach A:

for (all objects)
    object.move
    object.detect_collisions
    object.resolve_collisions

Approach B:

for (all objects)
    object.move

for (all objects)
    object.detect_collisions

for (all objects)
    object.resolve_collisions
\$\endgroup\$
5
\$\begingroup\$

Approach B is what you want.

With Approach A here's a possible problem:

//You have two balls
//Ball A moves
//Ball A is now colliding with Ball B
//Ball A and B get forces applied

When in reality Balls A and B are very close but have the exact same velocity!

What should happen (and what you would get with Approach B) is:

//Ball A Moves
//Ball B Moves
//No collisions detected
//No forces applied
//Both balls continue moving as they did 
\$\endgroup\$
  • \$\begingroup\$ Are there any cases at all where Approach A is better than B? I've made games with only mobile to static collisions (player characters and stage), so the problem you mentioned didn't apply. Is B still better in those situations? \$\endgroup\$ – Wingblade Jul 8 '15 at 20:00
  • \$\begingroup\$ Approach A is much faster. 1 iteration vs 3. If you're not aiming for accuracy or your game is just much smaller and slower it could be a nice optimization. That said if you're not sure I would always start with B. \$\endgroup\$ – Honeybunch Jul 8 '15 at 20:38
  • \$\begingroup\$ Only doing 1 iteration was the reason why I used Approach A in those games, it is faster. However aslong as the number of objects is small there should be little difference. Thank you for your answer! \$\endgroup\$ – Wingblade Jul 8 '15 at 20:55
  • \$\begingroup\$ The iteration is the absolute lowest cost in this: it merely loops through all objects. The number of operations is the same in both options. Say you have 100 objects. Option A 1 forloop, 100x movement logic, 100x99 collisionchecks and up to 100 collisionresolves. Option B 3 forloops and the same number of moves, checks and potentially less collisions. So in the end option B is probably much efficient if it just saves one collision due to the ball example A provided. \$\endgroup\$ – Felsir Jul 13 '16 at 12:58
1
\$\begingroup\$

In general, approach B is actually faster since it can be cached and vectoried more efficiently using Structs of Arrays (SoA) / Arrays of Structs of Arrays (AoSoA), which you probably should be using anyway.

\$\endgroup\$
  • \$\begingroup\$ Actually option B is the better approach in general. It gives the correct result and is in most cases fastest. \$\endgroup\$ – Felsir Jul 13 '16 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.