← Pixel Dreamer Studio

A turned building that covers the cells it stands on

Turning a building a quarter turn swaps its width and depth, and the model has to turn about its middle.

A building turned a quarter turn is as wide as it was deep, so a footprint that keeps the old width and depth marks the wrong cells. In our Godot 4.7 test a 2 by 3 building turned 90 or 270 degrees had 2 of its 6 cells wrong. Its model, with its pivot in a corner, turned about that corner and covered 0% of its own cells at 90, 180 and 270 degrees. Swapping the size on odd turns, and putting the model's middle at the middle of the cells, gave the right cells and 100% cover at every turn.

The code

Save it as footprint.gd and load it with const Footprint := preload("res://footprint.gd"). Mark the cells with Footprint.cells(origin, Vector2i(2, 3), turns), where origin is the cell under the building's corner and turns is 0 to 3. Place the model with building.position = Footprint.centre(origin, Vector2i(2, 3), turns, cell_size) and building.rotation.y = -turns * PI / 2. The model's pivot must be in the middle of its base: if it is in a corner, move the mesh inside the building scene so the middle sits on the scene's origin.

extends RefCounted
## Grid cells a building covers after it is turned, and where to put its model.
## A 2 x 3 building turned 90 degrees covers 3 x 2 cells, and a model whose
## pivot is in a corner swings out of its cells when it turns about that corner.
##   var cells := Footprint.cells(origin, Vector2i(2, 3), turns)     # turns = 0..3 quarter turns
##   building.position = Footprint.centre(origin, Vector2i(2, 3), turns, cell_size)
##   building.rotation.y = -turns * PI / 2
## The model's pivot must be in the middle of its base (move the mesh inside
## the scene so it is).


static func size_after(size: Vector2i, turns: int) -> Vector2i:
	return size if posmod(turns, 2) == 0 else Vector2i(size.y, size.x)


static func cells(origin: Vector2i, size: Vector2i, turns: int) -> Array[Vector2i]:
	var s := size_after(size, turns)
	var out: Array[Vector2i] = []
	for y in s.y:
		for x in s.x:
			out.append(origin + Vector2i(x, y))
	return out


static func centre(origin: Vector2i, size: Vector2i, turns: int, cell := 1.0) -> Vector3:
	var s := size_after(size, turns)
	return Vector3((origin.x + s.x * 0.5) * cell, 0.0, (origin.y + s.y * 0.5) * cell)

What we measured

A building 2 cells wide and 3 deep at cell (4, 4), 1 m cells, its model a 2 x 3 m box. We compared the cells marked and the area the model covers after each quarter turn. Two runs gave the same results.

Why it happens

The grid only knows how many cells across and down. A quarter turn swaps those two numbers, a half turn does not. A model turns about its pivot. With the pivot in a corner, turning moves the rest of the building around that corner and into the cells next door, a different side at each turn. With the pivot in the middle, placed at the middle of the cells, the building turns in place.

What to change

Effects for placing, upgrading and demolishing: City Builder & Construction VFX has 31 presets for placing, upgrading and demolishing buildings, including foundation, chimney smoke, coins, zones and 8 PSX versions.

Tested in Godot 4.7 with the Compatibility renderer.

Ready-made: City Builder & Construction VFX has 31 presets for placing, upgrading and demolishing buildings, including foundation, chimney smoke, coins, zones and 8 PSX versions.

More how-tos for City Builder & Construction VFX: Fire or dust over a whole building

More how-tos: Game systems