Skip to main content
Tweeted twitter.com/StackSoftEng/status/1206363475485872128
added 131 characters in body
Source Link
WDUK
  • 2.1k
  • 3
  • 15
  • 24

When casting to int i get one less tile:

enter image description here

But i've seen plenty of applications use grid positions based on some local reference point, there must be some trick to get this to work properly that i am not aware of with floats... does any one know?

But i've seen plenty of applications use grid positions based on some local reference point, there must be some trick to get this to work properly that i am not aware of with floats... does any one know?

When casting to int i get one less tile:

enter image description here

But i've seen plenty of applications use grid positions based on some local reference point, there must be some trick to get this to work properly that i am not aware of with floats... does any one know?

Tagging with Unity, since the Mathf library is specific to that engine.
Link
added 97 characters in body
Source Link
WDUK
  • 2.1k
  • 3
  • 15
  • 24

Ok i'veI've been trying to create a grid system that can use any grid size and start at any given anchor point rather than always at 0,0.

Bounds Span : 1f2f by 1f2f 
Grid Size : 1f by 1f
Center point : 3.97f , 2.7f  (the red dot)

Notice my for loop now runs one extra time some times, i have tried casting and flooring to int, both don't fix the problem, and thats because float imprecision is the cause, for example i've had 2.000000000f cast to int as 1.. so it still didn't work. if i cast the result of the division because it would be off by 1 in my for loops, see here:

enter image description here

Ok i've been trying to create a grid system that can use any grid size and start at any given anchor point rather than always at 0,0.

Bounds Span : 1f by 1f 
Grid Size : 1f by 1f
Center point : 3.97f , 2.7f  (the red dot)

Notice my for loop now runs one extra time some times, i have tried casting and flooring to int, both don't fix the problem, and thats because float imprecision is the cause, for example i've had 2.000000000f cast to int as 1.. so it still didn't work.

I've been trying to create a grid system that can use any grid size and start at any given anchor point rather than always at 0,0.

Bounds Span : 2f by 2f 
Grid Size : 1f by 1f
Center point : 3.97f , 2.7f  (the red dot)

Notice my for loop now runs one extra time some times, i have tried casting and flooring to int, both don't fix the problem, and thats because float imprecision is the cause, for example i've had 2.000000000f cast to int as 1.. so it still didn't work if i cast the result of the division because it would be off by 1 in my for loops, see here:

enter image description here

Source Link
WDUK
  • 2.1k
  • 3
  • 15
  • 24
Loading