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.
- Plain random points, already spread evenly over the area with sqrt(): 6.82 pairs too close per layout on average, 99.5% of layouts with at least one, and the closest pair 0.012 m apart: two objects in the same spot.
- The script: 0 pairs too close in all 200 layouts, 20.0 points placed on average, and the closest pair 0.8 m apart, the minimum.
- Asked for 60 points in the same circle, the script placed 51: there was no room for more at 0.8 m.
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
min_distance: about the width of one object, so they touch at most.radius: the size of the area.tries_per_point: more tries pack the circle a little fuller.
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
- Hover a 3D object inside a SubViewport
- A turned building that covers the cells it stands on
- An inventory that never loses an item and never overfills a stack
- A JSON save that loads back exactly what you saved
- Fire that spreads one cell per step, in every direction
- A pooled enemy that comes back as fresh as a new one
- An enemy that stops at the dead player and finds the respawned one
- Poison, burn and bleed that keep ticking when applied again
- XP, qi and gold that keep growing without going negative
- Count slot line wins with wilds in front
- Random directions spread evenly over a sphere or a cone
- Count a lap only when the car really went round
- Make a seed repeat: daily runs, replays and roguelike seeds
- Offline progress that counts the time the game was closed
- A chance roll without the long bad-luck streaks
- Check crafting recipes in any order, with the right counts
- Make HP and mana regen the same at any frame rate
- Damage over time with the right number of ticks
- Spawn enemies where the camera cannot see them
- Keep a chasing horde from stacking into one enemy
- Make the fish bite after the same wait at any frame rate
- Snap buildings to the right grid cell on both sides of the origin
- Make a cooking timer fire once, on time
- Pick from a weighted loot table with the right chances
- Level up as many times as the XP covers
- Walk a patrol guard to each point without overshooting
- Remove items from an Array while checking them, without skipping any
- Count and take crafting items from every stack
- Play random sounds and events without obvious repeats
- Pick the growth image so the ripe one shows exactly when the crop can be picked
- Turn a day counter into season, day and year without a day 0
- An enemy that dies once, however many hits land
- A fair shuffle you can replay
- A ghost that drives the lap at real speed
- A vision cone that does not flicker at the edge
- Move a group of units without a pile-up
- A sprinkler that waters a round patch
- A slot reel that always lands forward on the result
- A crowd of NPCs that never emote or bob in step
- A song position that moves on every frame
- Nodes your editor tool adds that stay in the scene