#Custom units coordinate system

1 messages · Page 1 of 1 (latest)

prime bane
#

Script-wise, how do people go around making a system for coordinates for grid-based object placement? Normally, the objects have the global_position values, which correspond to the pixel units. But if I want to have a grid-based position, it should retrieve the position that is based on the size of the object in pixels (see the attached image).

I also want to be able to retrieve objects at said position, but have no idea how would I do that without some kind of a collision object that exists only when the code wants to check position - seems like not the best solution.

tired axle
#

Generally i just use a TileMapLayer for that.
It has local_to_map() and map_to_local() which translates from Vector2i to Vector2.

But i think the implementation is just a bit of math.
map_pos.x = floori(position.x / cell_size.x)

So if the position.x is 0 to cell_size.x. The result will always be 0 (since floori() removes the decimal part and rounds down).
If position.x was 24 and cell_size.x was 16, it would result in 1.5, which rounds down to just 1.