← Pixel Dreamer Studio

Scatter spikes, roots or trees in a circle without overlaps

Try random spots and keep only the ones far enough from every point placed so far.

Random points do not avoid each other. In our Godot 4.7 test, 20 random points in a circle with a 4 m radius had 6.82 pairs closer than 0.8 m on average, and 199 of 200 layouts had at least one. Try up to 30 random spots per point and keep the first one at least the minimum distance from all the others: 0 pairs too close, and all 20 points still fit.

The code

Call scatter(count) and spawn your spike, root or flower at each point, relative to the center of the effect. Set rng.seed to get the same layout every time. It returns fewer points than you asked for when the circle is full, so spawn from the returned array, not from count.

extends Node
## Scatter points in a circle that keep a minimum distance from each other:
## spikes, roots, flowers or trees that should not grow into each other.

@export var radius := 4.0
@export var min_distance := 0.8
@export var tries_per_point := 30

var rng := RandomNumberGenerator.new()


func scatter(count: int) -> Array[Vector3]:
	var pts: Array[Vector3] = []
	for n in count:
		for t in tries_per_point:
			var a := rng.randf() * TAU
			var r := radius * sqrt(rng.randf())  # sqrt: even over the area, not bunched in the middle
			var p := Vector3(cos(a) * r, 0.0, sin(a) * r)
			var ok := true
			for q in pts:
				if p.distance_to(q) < min_distance:
					ok = false
					break
			if ok:
				pts.append(p)
				break
	return pts  # fewer than count when the circle is full

What we measured

200 layouts of 20 points in a circle with a 4 m radius, minimum distance 0.8 m, with a fixed seed. Two runs with the same numbers.

Why it happens

Each random point is picked on its own, so nothing stops two from landing almost on top of each other, and 20 points make 190 pairs that can. Checking the distance to every point already placed, and trying again when it is too close, keeps them apart. Finding a free spot gets harder as the circle fills up, which is why the tries are limited.

What to change

Why sqrt() for even spreading: random points clump in the middle of a circle.

Ready-made druid effects: Nature & Druid VFX for Godot 4 has 31 presets, including Entangle Roots, green and toxic Spore Clouds, Thorn Rings and healing flowers, 9 of them also in a PSX version.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Nature & Druid VFX for Godot 4 has 31 ready-made druid effects, including Entangle Roots, Thorn Rings, green and toxic Spore Clouds and healing flowers, 9 of them also in a PSX version.

More how-tos for Nature & Druid VFX for Godot 4: Slows and roots that overlap · Growing one cloud or aura zone grows all of them

More how-tos: Game systems