Timeline for How to do grid systems with floats given precision causes problems
Current License: CC BY-SA 4.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Dec 15, 2019 at 23:47 | vote | accept | WDUK | ||
Dec 12, 2019 at 13:16 | comment | added | Filip Milovanović |
@WDUK: The other thing you can do in addition to the above is: when calculating the number of tiles, take some small threshold value (e.g. 0.00001, but experiment a bit), and if abs(round(x) - x) < threshold use round(x), otherwise do floor(x). The effect is that the grid size will snap to the nearest grid line if very, very close to it, which will get you a more consistent behavior for different grid sizes. 2/2
|
|
Dec 12, 2019 at 13:16 | comment | added | Filip Milovanović | @WDUK: I probably didn't express myself in the best way. Currently, it looks like you are creating the grid anew whenever the centerpoint moves; what I meant by "once" is to ignore the centerpoint and just create the grid as if it were at (0, 0), and then apply a transform to that to get the final result (I used "once" because the creation part doesn't need to be done on every update, but only when the grid size changes, or just on load/init if it doesn't change). This will make it consistent and avoid the flickering when you move it around. 1/2 | |
Dec 12, 2019 at 1:15 | comment | added | WDUK | @FilipMilovanović am i not already doing it once with the for loops? Unless i misunderstand what you mean here? And if i apply floor (see second set of images using floor) it's also some times a bit off. For me i find its when i move the center, which causes the width/length to recalculate and theres where the imprecision is kicking off. | |
Dec 12, 2019 at 0:42 | comment | added | Filip Milovanović | @WDUK: I would just calculate the number of tiles once in each direction, by doing the division, then rounding to the nearest integer (or you can do floor). You can then construct the tiles with the origin at (0, 0), as sort of a local-space model of the grid; then and as a final step (or in each frame) multiply that (each vertex) with a any transform matrix you want (translation, rotation, scale, affine transforms of any kind) - or if it's only about translating the center point, just add the translation vector to everything. | |
Dec 12, 2019 at 0:30 | comment | added | Errorsatz | Epsilon can be as small as 1.401298E-45f, so it may be worth using a larger value. For most purposes, you could go up to 0.001f without issue. | |
Dec 12, 2019 at 0:06 | comment | added | WDUK | Hi see edit, your first assumption is correct, width/size of 2.5 should still round to 2 since i've not yet reached the 3rd tile. But your suggestion of floor to int does not work (as per edit) because it now casts 2 to 1 in some situations where the float is very close. Epsilon does not seem to fix it. | |
Dec 11, 2019 at 23:52 | history | answered | Errorsatz | CC BY-SA 4.0 |