int() cuts toward zero, so int(-0.5 / 2.0) is 0, the same cell as +0.5. Every cell left of or behind the origin comes out one off, and the cells along the zero lines become twice as big. Use floori(x / cell_size) for the cell and put the building at (cell + 0.5) * cell_size.
The code
Raycast from the mouse to the ground as usual and give the hit position to cell_of(). Use the Vector2i it returns as the key for what is built where, and center_of() for where the building goes. Set origin if your grid does not start at (0, 0, 0).
extends Node
## Grid cells for building placement that are right on both sides of the origin.
## cell_of() is the cell under a world point (from your mouse ray), center_of() where to put the building.
@export var cell_size := 2.0 # meters
@export var origin := Vector3.ZERO # the corner where cell (0, 0) starts
func cell_of(world: Vector3) -> Vector2i:
var p := (world - origin) / cell_size
return Vector2i(floori(p.x), floori(p.z)) # floori: -0.5 is cell -1, not 0
func center_of(cell: Vector2i) -> Vector3:
return origin + Vector3((cell.x + 0.5) * cell_size, 0.0, (cell.y + 0.5) * cell_size)
What we measured
Cells 2 m wide and a mouse point every 5 cm over 12 × 12 m around the origin: 57,600 points. Two runs with the same numbers.
int(x / 2): the building ended up away from the mouse for 75.0% of the points, every point left of or behind the origin. 16 m² of mouse positions all built in the cell next to the origin, 4 cells' worth.int(-0.5 / 2)gave 0,floori(-0.5 / 2)gave -1.snappedf(x, 2)as the building's center: the building was always under the mouse, but its edges were off the grid lines 100% of the time, so each building covered parts of 4 cells.- The script: building under the mouse for 100% of the points, edges on the grid lines 100%, one cell per building, and the cell next to the origin got exactly 4 m² of mouse positions.
Why it happens
int() drops the fraction, which rounds toward zero: 1.5 becomes 1, but -1.5 becomes -1, not -2. So on the negative side every point lands one cell too close to the origin, and cells -1 and 0 merge into one. snapped() rounds to the nearest grid point, and a grid point is the corner of four cells, not a cell. floori() always rounds down, so both sides of the origin work the same way.
What to change
cell_size: the width of one cell in meters. For buildings bigger than one cell, find the cell under the mouse, then mark that cell and the ones next to it as taken.origin: move the whole grid, for example to line it up with your terrain.
Effects that fill the whole building: particles that cover a building.
Ready-made city builder effects: City Builder & Construction VFX for Godot 4 has 31 presets, including foundation, upgrade, demolish, 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
- A turned building that covers the cells it stands on
- 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
- 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