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.
- Not turned: both ways right, the model on 100% of its cells.
- 90 degrees: the footprint becomes 3 x 2. Keeping 2 x 3 marked 2 of 6 cells wrong. The corner-pivot model swung to x 1 to 4, covering 0% of its cells at x 4 to 7. The script: 100%.
- 180 degrees: the size is 2 x 3 again, so the cells were right, but the corner-pivot model sat at x 2 to 4 and z 1 to 4, again 0%. The script: 100%.
- 270 degrees: 2 of 6 cells wrong with the old size, the corner-pivot model 0%, the script 100%.
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
- Snapping to the wrong cell left of or above the origin: floor, not int.
- Dust that should cover the whole building: emission box extents.
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
- Hover a 3D object inside a SubViewport
- 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
- Scatter spikes, roots or trees in a circle without overlaps
- 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