← Pixel Dreamer Studio

Explosion damage that reaches big enemies

Let the damage fall off with the distance to the edge of each target's collision shape, not to its centre.

Damage that falls off with the distance from the blast to the target's centre treats a big enemy as if it were a point in its middle, so a boss whose side is inside the blast can get nothing. In our Godot 4.7 test a 5 m blast found a 2.5 m radius boss, a tank and a tower with its sphere query, but their centres were 6.50, 6.58 and 6.28 m away, so damage from the centre distance was 0 for all three. Measuring to the nearest point of each collision shape, which matched the physics engine to the millimetre, gave them 20.0, 4.3 and 19.7 damage out of 100.

The code

Save it as blast_falloff.gd and load it with const Blast := preload("res://blast_falloff.gd"). For each body the blast found, call Blast.damage(body, blast_position, 5.0, 100.0) instead of using the distance to body.global_position. It reads the body's CollisionShape3D children and supports sphere, box, capsule and cylinder shapes that are not scaled; other shapes count from their centre. Keep finding the bodies with a sphere query and a wall check, as in explosion damage through walls.

extends RefCounted
## Explosion damage that falls off with the distance from the blast to the
## nearest point of each target's collision shapes, not to the target's centre,
## so a big enemy with its side inside the blast still gets hurt.
##   const Blast := preload("res://blast_falloff.gd")
##   var dmg := Blast.damage(body, blast_position, 5.0, 100.0)
## Works with Sphere, Box, Capsule and Cylinder shapes that are not scaled.
## Other shapes count from their centre.


static func damage(body: Node3D, blast: Vector3, radius: float, full: float) -> float:
	var d := surface_distance(body, blast)
	if d >= radius:
		return 0.0
	return full * (1.0 - d / radius)


static func surface_distance(body: Node3D, point: Vector3) -> float:
	var best := INF
	for child in body.get_children():
		var cs := child as CollisionShape3D
		if cs == null or cs.disabled or cs.shape == null:
			continue
		var p := cs.global_transform.affine_inverse() * point  # the blast in the shape's own space
		var s = cs.shape
		var d := p.length()
		if s is SphereShape3D:
			d = p.length() - s.radius
		elif s is BoxShape3D:
			var q: Vector3 = p.abs() - s.size * 0.5
			d = Vector3(maxf(q.x, 0.0), maxf(q.y, 0.0), maxf(q.z, 0.0)).length() + minf(maxf(q.x, maxf(q.y, q.z)), 0.0)
		elif s is CapsuleShape3D:
			var half: float = maxf(s.height * 0.5 - s.radius, 0.0)
			d = Vector3(p.x, p.y - clampf(p.y, -half, half), p.z).length() - s.radius
		elif s is CylinderShape3D:
			var side: float = Vector2(p.x, p.z).length() - s.radius
			var top: float = absf(p.y) - s.height * 0.5
			d = Vector2(maxf(side, 0.0), maxf(top, 0.0)).length() + minf(maxf(side, top), 0.0)
		best = minf(best, maxf(d, 0.0))
	return best

What we measured

A blast at the origin with a 5 m radius and 100 damage falling off in a straight line to 0 at the edge, and five StaticBody3D targets. For each one we also asked the physics engine for the smallest sphere around the blast that touches it, by halving the radius 30 times. Two runs gave the same numbers.

Why it happens

A sphere query finds every body whose collision shape overlaps the blast, but global_position is only the body's origin. For a soldier the difference is the width of a capsule; for a boss, a vehicle or a building it can be several metres, enough to put the centre outside the blast while the side is well inside. The script moves the blast into each shape's own space and works out the distance to its surface, the same way for a turned box as for a straight one. For a body made of several shapes, the script uses the closest one.

What to change

What the blasts look like: Modern Battlefield VFX for Godot 4 has mortar, landmine and fuel depot blasts to play where explode() deals the damage.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Modern Battlefield VFX for Godot 4 has mortar, landmine and fuel depot blasts to play where explode() deals the damage.

More how-tos for Modern Battlefield VFX for Godot 4: Explosion effect in one burst · 3D debris particles not rotating · 3D camera shake that lands back on center

More how-tos: Hazards, traps and attacks