Godot Version
4.3 `
Question
I’m making a 3d dungeon generator
I have premade tiles that I instantiate, place and rotate to build a network of corridors.
The generation works, and now I want to determine what grid positions are occupied by my tiles. Using a set size value for each tile using the script at the bottom of this post.
To debug this I paste a cube on all my occupied grid points to see if they line up with the tile in the scene.
But I want them to line up no matter the rotation. And I can’t figure it out.
At -90 they line up,
0 all gridpoints are shifted +1 in z
90 gridpoints +1 in z and +1 in x
-180 they shift +1 in x
func get_tile_coverage_by_transform(tile_instance: Node3D, tile_size: Vector2) -> Array:
# Get the global transform of the tile
var tile_global_transform = tile_instance.global_transform #Represents the tile's position, rotation, and scale in the world.
print("tile global transform:", tile_global_transform)
var tile_global_origin = tile_global_transform.origin # The tile's position in world coordinates.
print("tile_global_origin:", tile_global_origin)
var tile_global_basis = tile_global_transform.basis
print("tile_global_basis:", tile_global_basis)
for x in range(0, int(tile_size.x)):
for z in range(0, int(tile_size.y)):
var local_position = Vector3(x, 0, z)
var rotated_position = tile_global_basis * local_position
var world_position = tile_global_origin + rotated_position
var snapped_position = world_position.snapped(Vector3(1, 1, 1)) #<-gridsize
covered_positions.append(snapped_position)
return covered_positions