I'm making a PCG game where I have points scattered or projected onto the surface of a cube, which is centered on origin with a "radius" of 1.
That surface is a grid and I want to associate each point to the center of each proper cell's grid.
After detecting which face the point is by taking max(x,y,z) I used that formula:
halfTileSize * floor (3dpositions * gridDivision)/gridDivision;
[EDIT: everything above work, everything below is useless, the problem was outside influence from obsolete code]
But it doesn't round the point to the correct grid cell on the surface. There is a problem where front and back don't return the same result, or it skips a row with some condition, and it also rounds to the wrong nearby cells.
I tried to replace the floor with:
3dpositions.xyz < 0 ? ceil(3dpositions * gridDivision) : floor(3dpositions * gridDivision)
That didn't work either. It seems to work fine on a 2d planes, which I used to conceptualize the formula, but in 3D I'm at loss as to why it wouldn't work, since I basically reduce the problem to 2D anyway...
Any help to create a formula that would round the points properly into the right cell?