See all glossary terms

Tunneling

Tunneling is when physics entities move fast enough that they jump through walls or collision areas. It's a common problem in game development.
Normally, the physics engine checks for collisions in each physics update using the object's final position. If the object moves too fast and collision shapes are small enough, it can pass through a wall without triggering a collision. One frame, the object is on one side of the wall, and the next frame, it's on the other side. For the physics engine, it never collided with the wall.
For most games and entities, this is not a problem. But it can be a big issue for fast-moving objects like bullets. For this reason, it's common to use ray-casts or shape-casts to detect collisions for fast-moving objects, which do not have the tunneling problem as they check for collisions along a projected line.

Continuous Collision Detection

Tunneling can also be solved with Continuous Collision Detection (CCD). This feature checks for collisions along the path of an object's movement rather than just at its final position. Under the hood, the feature may use ray-casting or shape-casting on top of your entity's collision shape.
CCD costs performance and isn't necessary in most cases, so it's usually off by default in game engines. We can enable it for specific physics bodies that need it. The feature is available in Godot for physics body nodes (not areas). You can enable it in code for a given body like this:
extends CharacterBody2D

func _ready():
    var resource_id := get_rid()
    PhysicsServer2D.body_set_continuous_collision_detection_mode(ressource_id, PhysicsServer2D.CCD_MODE_CAST_RAY)