With input accumulation on, which is the default, Godot merges mouse motion into about one event per frame. A dot at every event is then spaced by how far the mouse moved that frame: in our Godot 4.7 test of a 600 px stroke at 60 fps, 5 px apart at 300 px per second and 50 px apart at 3,000. Stepping each movement by 8 px but dropping the leftover drew nothing at all when slow. Carrying the leftover gave 76 dots, every one 8 px apart, at every speed.
The code
Save it as stroke_spacing.gd. On mouse press call begin(pos), on every InputEventMouseMotion while the button is held call add(pos), and put a dot at each point they return: a decal, a particle, a stamp in an Image, whatever your brush draws.
extends RefCounted
## Even dots along a mouse or brush stroke, however fast the mouse moves.
## begin(pos) on press, add(pos) on every motion event, place a dot at every point they return.
var spacing := 8.0
var _last := Vector2.ZERO
var _carry := 0.0 # distance walked since the last dot
func begin(pos: Vector2) -> PackedVector2Array:
_last = pos
_carry = 0.0
return PackedVector2Array([pos])
func add(pos: Vector2) -> PackedVector2Array:
var out := PackedVector2Array()
var seg := pos - _last
var length := seg.length()
var d := spacing - _carry # where the next dot falls on this segment
while d <= length:
out.append(_last + seg * (d / length))
d += spacing
_carry = length - (d - spacing) # keep the leftover for the next segment
_last = pos
return out
What we measured
A straight 600 px stroke at 60 fps with one motion event per frame, at 300, 1,000 and 3,000 px per second, with 8 px spacing. Two runs with the same numbers.
- A dot at every event: 121 dots 5 px apart at 300 px/s, 37 dots 16.67 px apart at 1,000, 13 dots 50 px apart at 3,000.
- Steps of 8 px from each event, leftover dropped: at 300 px/s only the first dot, because every move was shorter than 8 px. At 3,000 px/s 73 dots, gaps up to 10 px, the last one 2 px short of the end.
- The script: 76 dots, every gap 8.00 px, the last dot on the end of the stroke, at all three speeds.
Why it happens
The mouse does not report every pixel it passes. A fast flick covers 50 px between two events and a slow drag only a few. Spacing by distance instead of by event fixes both, but only if the distance already walked since the last dot is kept. Without it, every short move starts again from zero and never reaches the next dot.
What to change
- Between two events the script follows a straight line, so a curve becomes short straight pieces, one per frame.
- For a brush whose dots should touch, set spacing from the brush size, for example a quarter of it.
- Painting from a @tool script: make the painted nodes stay after saving.
One emitter for a whole level of painted points: ambient particles from emission points.
Paint particles in the editor: VFX Painter Kit for Godot 4 lets you paint ambient particles onto your level in the editor, with each layer as one emitter and one draw call, and 12 presets from fireflies to ground mist.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: VFX Painter Kit for Godot 4 lets you paint ambient particles onto your level in the editor, with each layer as one emitter and one draw call, and 12 presets from fireflies to ground mist.
More how-tos for VFX Painter Kit for Godot 4: Ambient particles in many places from one emitter · Nodes your editor tool adds that stay in the scene
More how-tos: UI and HUD
- A screen fade that finishes while the game is paused or slowed
- A hand of cards that always fits, without spreading out when it is small
- Edge markers that stay on the rim and point the true way
- A wrapped Label that is as wide as its text, up to a limit
- Big numbers with thousands separators, minus signs and all
- Show a 0 to 1 ratio on a progress bar, and fill a ring or gauge from the angle you want
- Blink or fade a tinted HUD element without losing its color
- Spin a HUD ring, radar or icon in place instead of around its corner
- Make buttons clickable again under a full-screen fade, transition or post-process ColorRect
- 9-slice window: corners that do not stretch
- Visual novel background: fill the screen, crossfade scenes
- Coins that fly to the HUD counter
- Floating damage numbers in 2D
- An icon over a 3D character's head, without the ghost behind the camera
- Drag a box to select 3D units, in any direction
- Radar blips that turn with the player
- Pick the radial menu slot the mouse is pointing at
- Show a percent that never says 0% or 100% too early
- Type dialogue at the same letters per second on every line and every frame rate
- Move a speedometer or gauge needle smoothly without cutting through the bottom
- A countdown that never shows 0 while time is left
- A coin counter that rolls up and lands on the real total