.snapped() issue in a grid

A user posted a slightly inverted issue last august with no replies.

I’m using a 32 by 32 grid and ive got a:

Node2d

sprite2d

area2d

  • collision shape 2d

Scene that is able to be dragged and dropped onto the grid after being instantiated.

The position of all of those nodes are set to 0,0 and the offset is not set to centered. The scale of all child nodes is also set to 1,1

Code:

Const TILE_SIZE: Vector2 = Vector2(32,32)

func _process():

if is_dragging_build_object == true:

build_object.global_position = get_mouse_global_position()

Then I have an unhandled input event left click set is_dragging_build object to false and

build_object.global_position = get_mouse_global_position.snapped(TILE_SIZE).

The dragging of the object works, the 0,0 top left corner of the object is aligned with the 0,0 tip of the cursor. However my issue is that the .snapped() properly snaps the object to tiles based on the center (in this case the 16,16 point) of the object. So which ever tile the center 16,16 point of the tile is in is the tile that the object snaps too. Not whatever tile the cursor 0,0 point is at or whatever tile the objects 0,0 point is at.

I’ve tried adding an offset like so:

.snapped(TILE_SIZE - TILE_SIZE/2.0)

and

.snapped(TILE_SIZE) - TILE_SIZE/2.0

and

14 other variations of such

The object still snaps to whatever tile the center of the object not its origin is in when the .snapped function is called.

Maybe I am misunderstanding the .snapped() function a little bit. Either way would somebody please help me get the .snapped() to snap to the tile that the 0,0 point of the get_global_mouse_position() is in or the 0,0 point of the object instead of wherever the center of the object is? Thank you!

Several hours later I discovered that the snapped function snaps to the nearest intersection of the grid! Creating the appearance that the dragged objects halfway point was determining the tile that the object snaps to. For anyone reading I was able to set the offset to the global mouse position like so to ensure that the object snaps to the correct tile in the grid:

build_object.global_position = (get_global_mouse_position() - TILE_SIZE/2.0).snapped(TILE_SIZE)

Not sure if stylistically this is the best way but it works!

So you issues was that when you place down an object, it gets placed in the center of the 4x4 tile grid instead of inside the tiles? That’s the exact problem I’m having.

Tried your solution and received

Error at (52, 40): Invalid operands “Vector2” and “float” for “-” operator.

probably got this because I’m trying to do math with a float and Vector2, is that supposed to be a subtraction sign after get_global_mouse_position()

Yes, @dailm tile size is of type Vector2, as you can see in the original code.

Const TILE_SIZE: Vector2 = Vector2(32,32)

Whereas in your case you probably stored it as a float, which you can’t logically subtract from a Vector2, hence the error.

Oh, I didn’t even have the tile size set as a variable. But adding that line solved my error.

Probably something wrong on my end though because it needs to be exactly inside the tile or else it’s going to snap on the sides of the tiles. Maybe I can get away with that by making my placing tiles slightly smaller (they’re 64, 64 just like the grid) and validate its placement to make sure users place it in the correct spot within the tile.