0 votes

I'm attempting to make libraries for strategy games/puzzle games, most of which require a click and drag path for players to use. However, every attempt I've made of making each point bound to a grid has failed. Hopefully I'm overthinking this, because I was unable to find any documentation on procedural grid snapping online.
Most attempts I've made usually revealed one bug that lead to another. I can upload example code if needed.

the general idea.

Godot version 3.4.1 stable
in Engine by (14 points)

1 Answer

0 votes

Grid snapping boils down to using the Vector2.snapped() function, or floor(x / step) * step on each coordinate, where step is how big a cell of your grid is. Then if you want the result to be centered, add a half-cell to it.

For example, to snap coordinates you would get with var pos = get_local_mouse_position(), you would do pos = pos.snapped(Vector2(32,32)) if your cell size is 32x32 pixels, add pos += Vector2(16,16) to center it, and apply the resulting position to the point of your line.

See https://docs.godotengine.org/en/stable/classes/class_vector2.html#class-vector2-method-snapped

Alternatively if you use TileMap you can also use world_to_map() to get tile coordinates https://docs.godotengine.org/en/stable/classes/class_tilemap.html#class-tilemap-method-world-to-map

by (29,034 points)

Oh yes, basic snapping is well and fine. Flooring, centering, and moving back is easy. The hardest part of this is trying to make it not have diagonals, in a sort of zig zag pattern. I'd imagine one would have to continuously split between two points until no diagonals remain, but how I've done it has yet to work flawlessly.

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 Frequently asked questions and 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 [email protected] with your username.