0
\$\begingroup\$

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.

EDIT:

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
\$\endgroup\$
6
  • \$\begingroup\$ This isn't nearly enough information to help you. What engine and programming language are you using? What does your current code look like? \$\endgroup\$
    – Kevin
    Commented Dec 2, 2020 at 23:59
  • 1
    \$\begingroup\$ The code you've shown is what we typically use for this situation. Using this code, an x position between -800 and 800 maps to column 0, -2400 to -800 maps to column -1, and 800 to 2400 maps to column +1. How does this differ from what you need? \$\endgroup\$
    – DMGregory
    Commented Dec 3, 2020 at 2:31
  • \$\begingroup\$ @DMGregory I get my player to walk to the top left and instead of spawing the appropriate chunk it does nothing At grid position (-0,0), it just spawns the chunk at (0,0). I get (-0,0) instead of (-1,0) \$\endgroup\$ Commented Dec 3, 2020 at 3:08
  • \$\begingroup\$ Well, did you try drawing your algo with pen and paper? From the description it looks right, you might have a bug elsewhere. \$\endgroup\$
    – Kromster
    Commented Dec 3, 2020 at 9:33
  • \$\begingroup\$ @Kromster I'll investigate. \$\endgroup\$ Commented Dec 3, 2020 at 9:45

1 Answer 1

1
\$\begingroup\$

It's common for negative zero to behave in unintuitive ways. Complicating things, I wasn't able to find an official doc detailing what sort of behavior a programmer should expect from Godot regarding negative zero.

So the best suggestion I can offer is to special case -0 to behave as needed:

func calculate_grid_position():
    var tempX = int(round(position.x/ 1600)
    if tempX == -0:
        tempX = -1
    var tempY = int(round(position.y/ 1600)
    if tempY == -0:
        tempY = -1
    var grid_position = Vector2(tempX, tempY)
    return grid_position

It would be a good idea not to hard coded the cell size (int this case 1600), but without more knowledge about your code, I can't specifically tell you how to fix that other than suggesting you to move those values into a variable or a constant.

As an end note, the desired mapping as described in the question & comments seems a bit unusual. I generally wouldn't expect -0 to arise under most situations. When I have needed to handle it as a special case, I've either converted it to 0 or treat it as an error / exception.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I agree -0 can act in unintuitive ways but I suspect -0 should become 0 and not -1 in this case. Otherwise the grid cell for 0 would include 0 ≤ x < 800 instead of -800 ≤ x < 800. \$\endgroup\$
    – amitp
    Commented Dec 4, 2020 at 23:07
  • \$\begingroup\$ @amitp I agree that the mapping is unusual. My read was that OP specifically identified the the code given worked except that -0 needed to map to -1. But it probably bears mentioning - I'll add an edit & would gladly update further if OP provides additional info. \$\endgroup\$
    – Pikalek
    Commented Dec 4, 2020 at 23:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .