I want to align an object position to grid.
For instance, if my object position is (102, 93) and
my grid size is 10 the aligned position (multiple) must be (100, 90).
My program does it a lot of times.
Currently i’m using this:
inline int Align(int value, int size)
{
return (value - (value % size));
}
Then, i do this when the object position is changed:
this->m_x = Align(new_x, GRID_SIZE);
this->m_y = Align(new_y, GRID_SIZE);
My questions are:
Is this a good way to align a object to grid?
The % operator is fast (i use it a lot)?
If i use float intead of int what i can do to align the position?
Another way of doing the same thing using integer math is like this:
If your size is a power of 2, you can use bitmasking (e.g. for size 16):