🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

A grid system that is offset from zero

Started by
6 comments, last by Alberth 4 years, 6 months ago

Hello

I am having real issues with creating a local grid system based on some anchor.I define my grid span as having a size (say 10 by 10) and a centre point on the XZ plane ( x, 0, z). My grid sizes vary and are not necessarily 1 by 1. So everything uses floats.Then i draw out my grid and i get problems due to imprecision and i do not see any way in which to solve this.

This forum is buggy as all hell when posting code, so i had to post it here:

https://pastebin.com/8dtLNpwN

This is what happens if i move the center position of my grid:

https://i.imgur.com/tQoRI3c.gif

It seems the imprecision causes the for loop to some times not do enough iterations, some times 1 too many or 1 too few. I can't figure out how to solve this, has any one made a grid system based on a local position ? Because it seems largely impossible ???

Advertisement

Can't you just hardcode 10 or use an int variable in the for statement comparison?

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

No because it isn't always 10. Nor is the offset always the same. Its up to the user. Wouldn't be a very flexible system if it was hard coded.

You should always be cautious when comparing floats to integers. Due to float imprecision the number 10 for example might end up represented as 10.00001, which will then fail your less-than test. Rounding the number or using an int to begin with as fleabay suggested prevents this.

Prototype said:

You should always be cautious when comparing floats to integers. Due to float imprecision the number 10 for example might end up represented as 10.00001, which will then fail your less-than test. Rounding the number or using an int to begin with as fleabay suggested prevents this.

I don't see how i can use only integers though. Since that would mean my grid cannot be localised it would always be axis aligned to the world which i am trying to avoid...

Forget about world coordinates, forget about scale, forget around orientation. Focus on the grid itself. Your grid has a integer number of cells in each direction, so use integers for the grid size.

int num_rows, num_columns;
for (int row = 0; row < num_rows; ++row) {
  for (column = 0; column < num_columns; ++column) {
     var pos_in_3d = transform_to_3d_world_coordinates(row, column);
     Gizmos.DrawWireCube(pos_in_3d, new Vector3(_gridSize, 0, _gridSize));
  }
}
     

Indeed, as 'a light breeze' says, define the grid in simple integers, and map grid coordinates to world coordinates for drawing (where the mapping handles scale, origin and orientation). For click positions etc, first do the reverse conversion (from world back to (real) grid coordinates), and then find the grid cell that was accessed.

This topic is closed to new replies.

Advertisement