4
\$\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\$

2 Answers 2

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\$
4
  • \$\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
    Commented Jul 8, 2015 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
    Commented Jul 8, 2015 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
    Commented Jul 8, 2015 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
    Commented Jul 13, 2016 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\$
1
  • \$\begingroup\$ Actually option B is the better approach in general. It gives the correct result and is in most cases fastest. \$\endgroup\$
    – Felsir
    Commented Jul 13, 2016 at 13:50

You must log in to answer this question.

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