PDA

View Full Version : "Snapping" during rotation and translation



JackR
05-29-2008, 01:33 PM
Hello all,

I am familiar with the basic concept between transformation matrices, but need some assistance with a specific application. I have objects loaded into a scene graph, and I have enabled translation and rotation with mouse key presses by applying a transformation matrix to the coordinates of the objects' vertices. Right now, I can rotate and translate my objects to virtually any position.

I have drawn a grid and want my objects to appear to "snap" to grid squares. In order to do this, I need to limit the movement so the rotation is only done in 90 degrees increments (90, 180, or 270 degrees of rotation), and translation is only done in grid-square size increments.

How can I go about this? I am not sure how I can make sure the transformation matrix is such that the object only translates or rotates in grid square units.

Thanks,
JackR

Dark Photon
06-02-2008, 05:00 AM
I have drawn a grid and want my objects to appear to "snap" to grid squares. In order to do this, I need to limit the movement so the rotation is only done in 90 degrees increments (90, 180, or 270 degrees of rotation), and translation is only done in grid-square size increments.

So, why don't you do exactly that?

You are already calling glRotatef, glTranslatef, etc. No?

karx11erx
06-02-2008, 05:07 AM
I think all you need to do is "snap" x,y and z passed to glRotate to the nearest whole multiple of your grip step. You simply need to do some rounding, e.g. like this:



#define GRID_INCR 90

void RotateAndSnap (float x, float y, float z)
{
float xSnap = (float) ((int) (xSnap + (GRID_INCR / 2)) / GRID_INCR);
float ySnap = (float) ((int) (ySnap + (GRID_INCR / 2)) / GRID_INCR);
float zSnap = (float) ((int) (zSnap + (GRID_INCR / 2)) / GRID_INCR);
glRotatef (xSnap, ySnap, zSnap);
}

The cast to int and addition of 45 (half of your increment) does the rounding in a simple way.

This is provided you are starting at 0,0,0 and never get some values in between.