← Pixel Dreamer Studio

Stretch a bolt sprite from the caster to the target

Put the start of the art on the node's origin, turn it by the direction from A to B, and scale it to the distance, all in world space.

Two things go wrong when a lightning bolt, beam or rope sprite is stretched from one point to another. from.angle_to(to) is the angle between the two positions as seen from the corner of the screen, not the direction from one to the other: in our Godot 4.7 test it missed the target by up to 264 px. And a Sprite2D is centred by default, so after scaling it to the distance, half of it sits behind the caster: 150 px behind and 150 px short of the target. The script below starts the art on the caster, turns it by the direction from A to B and scales it to the distance in world space: 0 px off, also under a scaled and turned parent and for art drawn top to bottom.

The code

Save it as beam_span.gd and load it with const Beam := preload("res://beam_span.gd"). Call Beam.span($Bolt, $Caster.global_position, target.global_position) every frame the bolt is shown, so it follows both ends. The art is expected to run left to right. For art drawn top to bottom pass true as the fourth value, and the fifth makes the bolt thicker or thinner.

extends RefCounted
## Stretch a sprite (a lightning bolt, a beam, a rope) from one point to another, in world space.
## The art is drawn left to right; set vertical = true for art drawn top to bottom.
##   const Beam := preload("res://beam_span.gd")
##   func _process(_delta):
##       Beam.span($Bolt, $Caster.global_position, target.global_position)


static func span(sprite: Sprite2D, from: Vector2, to: Vector2, vertical := false, thickness := 1.0) -> void:
	var size := sprite.get_rect().size
	var length := size.y if vertical else size.x
	if length <= 0.0:
		return  # no texture yet
	sprite.centered = false
	# the start of the art sits on the node's origin, centred across its width
	sprite.offset = Vector2(-size.x * 0.5, 0.0) if vertical else Vector2(0.0, -size.y * 0.5)
	sprite.global_position = from
	var angle := (to - from).angle()
	sprite.global_rotation = angle - PI * 0.5 if vertical else angle
	var stretch := from.distance_to(to) / length
	sprite.global_scale = Vector2(thickness, stretch) if vertical else Vector2(stretch, thickness)

What we measured

A 64 by 16 px sprite stretched from a caster at (200, 300) to 4 targets in different directions, 150 to 300 px away. We worked out where the two ends of the drawn sprite landed and how far they were from the caster and the target, then drew two of them and read the screen. Two runs gave the same numbers.

Why it happens

Vector2.angle_to() treats both points as directions from (0, 0) and gives the angle between those two directions. The direction from one point to another is (to - from).angle(), which is what angle_to_point() gives in Godot 4. A centred sprite grows from its middle when you scale it, so to start at the caster the art's left edge has to be on the origin: centered = false and an offset of half the height up. Position, rotation and scale are relative to the parent, so a parent that is scaled or turned changes all three. Setting global_position, global_rotation and global_scale avoids that.

What to change

Ready-made pixel spells: Pixel VFX Vol 3 Elemental Magic has 12 pixel art spell effects (ice, lightning, wind, poison, holy, water, earth) in 3 color sets, 64x64 and Godot 4 ready.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Pixel VFX Vol 3 Elemental Magic has 12 pixel art spell effects (ice, lightning, wind, poison, holy, water, earth) in 3 color sets, 64x64 and Godot 4 ready.

More how-tos: Auras, rings, beams and weather