Hi,
I’m trying to recreate the famous Scrabble game, and right now i’m stuck with trying to snap my tile to the grid. The grid is a static image and tiles are individual objects. I want to be able to move tiles with mouse/finger (the game is supposed to be for smartphones) but I always get some misalignment due to borders and floating points. Any way to easily snap to a grid ? I’m using godot 4.3
Have a great day
For snapping logic this what I do:
First of all I divide mouse position by tile size and floor it and multiply by the tile size. If the center of object is also (0,0) then add half of the tile size.
snap_position = floor(mouse/tile_size) * title_size + tile_size/2
It would be more helpful if you say your logic or how misaligned it or share picture.
I’m using a picture as my background grid
It’s a 15 by 15 grid but what’s bothering the computation are lines.
The red dot is my cursor and as you can see, the tile is not really lined up, but still snaps to a grid roughly good. Slots seem to be a bit too large
How did you changed the arrow? Ensure it is not the problem. If your grid cell size doesn’t match with tile size it also can create issues. Also check the objects are in the origin. Can you give the code of this logic to further inspection.
I’ve managed to line up the tiles with the grid, however I can’t get a reliable position from the mouse cursor position. Even though I’m using the same vector to compute tile position and mouse “grid position”, tiles are lined up and mouse is not.
var grid_pos := (get_global_mouse_position()) - board.global_position
print_debug("%s" % (grid_pos.snapped(board.snapping_vector) / board.snapping_vector))
Right now, i have this, where the division is to get the grid position of the tile. The center of the grid is supposed to be (7, 7)
, but I get a wrong result.
not sure about your specfic problem. The center of the screen is (0,0)
My grid sprite is not centered, I think it will make for easier computation later on