← Pixel Dreamer Studio

Make the explosion as big as its damage

Measure the drawn pixels in every frame of the effect and scale it so the biggest frame reaches the damage radius.

Explosion frames rarely fill their texture: there is empty space around the fireball so it can grow. Scale a 64 px explosion to a 100 px blast radius by its texture size (radius 32) and the biggest frame in our Godot 4.7 test was drawn only 61 to 63 px out, so enemies at 70, 90 and 99 px took damage outside the flash. Measuring the first frame instead went the other way: the last frame went past the edge of the screen. The script below finds the farthest drawn pixel in all frames and scales by that: 97 to 99 px, for an AnimatedSprite2D or a Sprite2D.

The code

Save it as fx_fit.gd and load it with const FxFit := preload("res://fx_fit.gd"). When you create the explosion, before it is shown, call FxFit.fit_radius($Boom, BLAST_RADIUS) with the same radius your damage code uses. The sprite should be centred, which is the default. It reads every pixel of every frame (3.3 ms for 12 frames of 64 by 64 in our test), so call it once per effect, or once per scene you load and reuse the scale.

extends RefCounted
## Scale an explosion (or any round effect) so what is drawn matches a radius in pixels,
## measured from the frames themselves, not from the size of the texture.
##   const FxFit := preload("res://fx_fit.gd")
##   FxFit.fit_radius($Boom, BLAST_RADIUS)   # a centred Sprite2D or AnimatedSprite2D
## It reads every pixel of every frame: call it once when the effect is made, not every frame.


static func visible_radius(tex: Texture2D, alpha := 0.1) -> float:
	## Distance from the middle of the texture to the farthest pixel that is drawn.
	if tex == null:
		return 0.0
	var img := tex.get_image()
	if img == null:
		return 0.0
	if img.is_compressed():
		img.decompress()
	var mid := Vector2(img.get_width(), img.get_height()) * 0.5
	var r := 0.0
	for y in img.get_height():
		for x in img.get_width():
			if img.get_pixel(x, y).a > alpha:
				r = maxf(r, (Vector2(x + 0.5, y + 0.5) - mid).length())  # to the pixel's centre
	return r


static func fit_radius(node: Node2D, radius: float, alpha := 0.1) -> float:
	## Returns the largest drawn radius found in the frames, in texture pixels.
	var r := 0.0
	if node is AnimatedSprite2D:
		var frames: SpriteFrames = node.sprite_frames
		for i in frames.get_frame_count(node.animation):
			r = maxf(r, visible_radius(frames.get_frame_texture(node.animation, i), alpha))
	elif node is Sprite2D:
		r = visible_radius(node.texture, alpha)
	if r > 0.0:
		node.scale = Vector2.ONE * (radius / r)
	return r

What we measured

An explosion of 12 frames, 64 by 64 px, a white disc growing from radius 6 to 20.3 px, and a damage radius of 100 px. We showed the biggest frame and read, on the rendered screen, how far the drawn pixels reached from the centre in 8 directions. Two runs gave the same numbers.

The last 1 to 3 px are the steps of a round shape made of square pixels.

Why it happens

Scale multiplies the whole texture, empty pixels included. An explosion drawn with room to grow fills only part of its texture on its biggest frame, so dividing the radius by half the texture width makes the drawn part smaller than the damage by the same share: 20 of 32 px here, about 63%. The first frame is the smallest, so measuring it makes every later frame too big. The farthest drawn pixel over all frames is the one number that matches what the player sees at the moment the explosion is largest.

What to change

Ready-made pixel explosions: Pixel VFX Vol 2 Explosions & Smoke has 12 pixel art explosion, fire and smoke effects in 3 colors (fire, blue, green), 64x64 and Godot 4 ready.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: Pixel VFX Vol 2 Explosions & Smoke has 12 pixel art explosion, fire and smoke effects in 3 colors (fire, blue, green), 64x64 and Godot 4 ready.

More how-tos: Fire, smoke and explosions