13
\$\begingroup\$

I have recently had an issue where sometimes a collision would not be detected. Someone told me that maybe I had an tunneling issue.

In the context of game development using physics, what is tunneling?


This is a low-ball question; I realized when suggesting someone that maybe they had that issue that we did not have a clear explanation around about what the issue was, so this question is meant to help users explain things with fewer words.

\$\endgroup\$
2
30
\$\begingroup\$

What is tunneling and why it happens

In video games, physic objects will have an specific position which is updated by the physics engine each frame based on its velocity and any forces applied to it.

enter image description here

Motion depicted with very low frame rate to draw attention to the fact that it is not continuous. Note that although the depiction is 2D, this is not specific to 2D.


When the physics engine computes the new position, it must check for collisions and respond to them. For example, if the new position results in a collision with a wall, the physics engine must compute a collision response (e.g. bouncing) so that the object does not end up stuck in the wall.

enter image description here

Collision depicted. With same low frame rate as before.


However, if the object is moving too fast (or if the frame rate extremely low), the physics engine may compute the new position to be on the other side of the wall. As if the object had, somehow, gone through it. We refer to this as tunneling (as if the object had made a tunnel under or through the wall and ended at the other side).

enter image description here

Tunneling depicted. Frame rate is much lower than before.


Solutions

Solution to the problem include checking for collisions at multiple steps along the motion of the object…

enter image description here

Multiple checks per frame depicted.


… Or checking against a shape that extends from the old position of the object to its new position. This is known as a "sweep collision detection", and it is integral part of a "continuous physics simulation".

enter image description here

Single shape using to check sweeping motion depicted.


Some other ways to mitigate the problem include:

  • Enforcing a maximum speed for objects (e.g. terminal velocity).
  • Making thicker walls. Thick geometry is also good to hide some shadow mapping problems, so these go hand-in-hand.
  • And more physics frames, of course.

Optimizations are beyond the scope of this answer.

\$\endgroup\$
4
  • 10
    \$\begingroup\$ Excellent use of animations here! Super clear. 🤩 \$\endgroup\$
    – DMGregory
    May 17 at 14:40
  • \$\begingroup\$ One other mitigation that might be worth adding is "thicker" colliders. A common place to get tunneling is with (non-convex) mesh colliders - where the surface is effectively infinitely thin, so it's very easy to skip over. Using convex hull colliders, or primitives like boxes/spheres/capsules that have an easier-to-evaluate "inside" volume lets the physics engine detect when something's dropped into the middle of the collision, even if it skipped past the skin. \$\endgroup\$
    – DMGregory
    May 17 at 15:23
  • \$\begingroup\$ Is another solution to compute the point of impact (not too hard for simple motion of simple objects) and determine whether it happens within the next frame? I implemented something like that in a game decades ago, but no idea if it's a useful technique generally. I hadn't thought of the sweep collision detection method... \$\endgroup\$ May 17 at 22:48
  • \$\begingroup\$ Computing the point of impact is essentially a version of the sweep approach described above. \$\endgroup\$
    – DMGregory
    May 20 at 17:53

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.