← Pixel Dreamer Studio

A ball that hits the net however hard it is kicked

Turn on continuous collision detection for anything that moves more than about half its own size plus the wall's in one physics tick.

A RigidBody3D moves in steps, one per physics tick, so a fast ball can land past a thin wall, or so far into it that it is pushed out the other side. In our Godot 4.7 test a football, 22 cm across, shot at a 5 cm thick wall went through 3 times in 10 at 10 m/s, 6 times at 20 m/s and 9 times at 80 m/s, with the default settings of a new project. With continuous_cd on, it went through 0 times at every speed. Doubling the physics ticks to 120 still let 6 in 10 through at 40 m/s.

The code

Save it as fast_body.gd and call preload("res://fast_body.gd").prepare($Ball, 40.0, 0.05) once when the ball is made, with the fastest it will ever go in m/s and the thinnest wall, post or net it must not pass, in metres. It turns continuous_cd on when that speed is high enough for a wall to be missed. You can also just tick Continuous CD on the RigidBody3D in the Inspector.

extends RefCounted
## Stops fast RigidBody3D balls and bullets from passing through thin walls,
## goal posts and nets. A body moves in steps, one per physics tick. If a step
## jumps over the wall, or lands with the body's centre already past the
## middle of the wall, the body comes out on the far side.
##   FastBody.prepare($Ball, 40.0, 0.05)   # top speed in m/s, thinnest wall in m
## Turns on continuous collision detection when the body is fast enough for
## that to happen, with a 25% margin.


static func safe_speed(body_size: float, wall_thickness: float, ticks := Engine.physics_ticks_per_second) -> float:
	return (body_size + wall_thickness) * 0.5 * ticks * 0.75  # one step = half the body + half the wall, less 25%


static func prepare(body: RigidBody3D, top_speed: float, wall_thickness: float) -> bool:
	var size := _size(body)
	body.continuous_cd = top_speed >= safe_speed(size, wall_thickness)
	return body.continuous_cd


static func _size(body: RigidBody3D) -> float:  # the smallest width of the body's first shape
	for c in body.get_children():
		if c is CollisionShape3D and c.shape != null:
			var s: Shape3D = c.shape
			if s is SphereShape3D:
				return s.radius * 2.0
			if s is CapsuleShape3D:
				return s.radius * 2.0
			if s is BoxShape3D:
				var b: Vector3 = s.size
				return minf(b.x, minf(b.y, b.z))
	return 0.1

What we measured

A RigidBody3D sphere of radius 0.11 m and 0.43 kg with no gravity, sent straight at a 5 cm thick StaticBody3D wall 5 m away, at 60 physics ticks. Each shot was 10 balls side by side, each starting a tenth of a physics step further back, so where the steps happened to land did not decide the result. Two runs gave the same counts.

Why it happens

Each tick the engine moves the ball by its speed divided by the tick rate, then checks what it overlaps. At 20 m/s and 60 ticks that is 33 cm per step, more than the ball and the wall together, so some steps start in front of the wall and end behind it. Slower than that, a step can still end with the ball's centre past the middle of the wall, and pushing it out the nearest way then means out the far side, which fits the balls lost at 8 and 10 m/s. Continuous collision detection checks the whole path of the step, not just where it ends.

What to change

What the shot looks like: Sports & Ball Game VFX for Godot 4 has 32 ready-made sports effects in Stylized and Retro PSX looks, including charge shots in 3 levels, kick impacts, ball trails and a goal net ripple.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Sports & Ball Game VFX for Godot 4 has 32 ready-made sports effects in Stylized and Retro PSX looks, including charge shots in 3 levels, kick impacts, ball trails and a goal net ripple.

More how-tos: Projectiles and aiming