← Pixel Dreamer Studio

Random directions spread evenly over a sphere or a cone

Pick an even height from -1 to 1 and an even angle around, instead of normalizing a random vector or picking two random angles.

Pick z = randf_range(-1, 1) and a = randf_range(0, TAU), then use Vector3(r * cos(a), r * sin(a), z) with r = sqrt(1 - z * z). That covers the sphere evenly. In our Godot 4.7 test of 100,000 directions, Vector3(randf_range(-1, 1), randf_range(-1, 1), randf_range(-1, 1)).normalized() put 35.8% within 20 degrees of the 8 corners of a cube, where an even spread gives 24.1%. A random yaw and pitch put 22.1% within 20 degrees of straight up or down instead of 6.0%. The even height and angle gave 24.2% and 6.1%.

The code

Save it as random_dir.gd. on_sphere() gives a direction anywhere, in_cone() one within max_angle_deg of a forward direction, for a fountain, a spray or shotgun pellets. Multiply the result by a speed to launch debris or sparks, and pass your own RandomNumberGenerator to repeat a seed.

extends RefCounted
## Random directions spread evenly over a whole sphere or inside a cone:
## firework bursts, flying debris, shotgun pellets.


static func on_sphere(rng: RandomNumberGenerator = null) -> Vector3:
	var z := _rand(rng, -1.0, 1.0)  # an even height gives an even spread over the surface
	var a := _rand(rng, 0.0, TAU)
	var r := sqrt(1.0 - z * z)
	return Vector3(r * cos(a), r * sin(a), z)


static func in_cone(forward: Vector3, max_angle_deg: float, rng: RandomNumberGenerator = null) -> Vector3:
	var z := _rand(rng, cos(deg_to_rad(max_angle_deg)), 1.0)
	var a := _rand(rng, 0.0, TAU)
	var r := sqrt(1.0 - z * z)
	var f := forward.normalized()
	var up := Vector3.UP if absf(f.y) < 0.99 else Vector3.RIGHT  # looking_at() fails when up is parallel
	return Basis.looking_at(-f, up) * Vector3(r * cos(a), r * sin(a), z)  # +Z of this basis points along forward


static func _rand(rng: RandomNumberGenerator, from: float, to: float) -> float:
	return rng.randf_range(from, to) if rng else randf_range(from, to)

What we measured

100,000 directions each way, from the same seed. Near means within 20 degrees. With an even spread that is 24.1% for the 8 corners of a cube, 18.1% for the 6 axis directions and 6.0% for straight up or down. Two runs gave the same numbers.

Why it happens

A cube reaches further out at its corners than a sphere, so a random point in it, pushed out to the sphere, ends up near a corner more often. Two random angles cover the sphere like lines of longitude, which all meet at the poles: each step of pitch gets the same share of directions, but near the top and bottom it covers much less of the surface. On a sphere every band of the same height has the same area, which is why an even height with an even angle around comes out even.

What to change

Bursts that are ready to play: Festival & Fireworks VFX for Godot 4 has rockets that burst by themselves, among them Peony, Willow Gold, Ring Cyan and Heart Pink, plus fountains, sparklers and confetti. One-shots start with play().

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Festival & Fireworks VFX for Godot 4 has rockets that burst by themselves, Peony, Willow Gold, Ring Cyan and more, plus fountains and sparklers.

More how-tos for Festival & Fireworks VFX for Godot 4: Fireworks without sub-emitters · Confetti particles with random colors · Big particle effects that stay on screen

More how-tos: Game systems