Snap to position?

Blitz3D Forums/Blitz3D Beginners Area/Snap to position?

tdman(Posted 2008) [#1]
Hi, say I have some points (evenly spaced) on screen [-16, -12, -8, -4, 0, 4, 8, 12, 16] and I have an entity that is feely movable... how can I snap the position of the entity to the nearest number once the user has finished moving the joystick? So for example, if the entity was moved to 10.5 how do I get this to measure the difference between entity position and the two nearest numbers I have specified, and then position the entity to the nearest one? There has to be a tidy way of doing this, without having to manually check everything each time the joystick is moved! Thanks!


Floyd(Posted 2008) [#2]
4.0 * Int( x / 4.0 ) is x rounded to a multiple of 4.

The idea is that Int() rounds to integer, i.e. a multiple of 1.

So you scale x down so the gap is 1, round it, then scale back up to multiple of 4.


tdman(Posted 2008) [#3]
Worked perfectly, thanks! And the whole thing done in 6 lines... I had over 30 just to do part of it before, thanks for pointing out the efficient way :)