+1 vote

I have a grid-map node and I want the player to move grid-based, think about Ultima and old 3d dungeon crawler games' movement.
In 2d we have worldtomap function for tilemap but there is no similar func for 3d grid-map.
heres a img so ye can understand it better.
left key pressed

what i need is a way to get the world position ( vector3) of the tile that the player is on and the adjacent tiles.
Or a better way of doing this kind of movement.

I can share my project files if needed.

in Engine by (16 points)

1 Answer

+2 votes
Best answer

Weird, TileMap provides them but not its 3D counterpart, apparently.

Anyway, you can get the conversion between world and grid-space with these formulas (a bit simplified), assuming your world node is centered at (0,0,0) with a scale of (1,1,1):

func world_to_grid(pos):
    var v = pos / tile_size
    return Vector3(floor(v.x), floor(v.y), floor(v.z))


func grid_to_world(pos):
    var v = pos * tile_size
    # This second calculation is to center the position on the tile
    return v + tile_size/2.0

If your game is fully grid-based, you can create your own grid_position variable in your objects using a script for your game logic, and convert to world coordinates when you want to change the position of the node. So to advance 1 cell forward, you would do grid_position.z += 1, then spatial.set_translation(grid_to_world(grid_position)) to update the node.

by (27,962 points)
selected by

And, if your game is with full grid logic, you can create all the game in a matrix with the logic working over it and reconstruct a GridMap for the visual part based on that matrix (the old way) and just use the map-to-world for movement simulations/animations

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.