mathematics – How do I determine the player’s grid position?

My original method for doing this involved taking the player’s position, dividing it by the grid cell size, and rounding the result. This works for most cases except when I move my player up above y-axis zero or to the left of x-axis 0. I use the grids to load chunks of procedurally generated tiles into my scene.

it keeps resulting in -0 being a component of the position vectors, which throws my system off. I fixed that by converting each component to integers, but that doesn’t solve the greater problem.

There must be a more effective method for this available.


I am using Godot Engine and GDscript for the game I am creating.

This is my code:

    func calculate_grid_position():
    var grid_position = Vector2(int(round(position.x/ 1600)),
                                int(round(position.y/ 1600)))
    return grid_position