|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| Rotating a group of points around a pivot |
|
![]() markookram Member since: 1/3/2008 From: Maribor, Slovenia |
||||
|
|
||||
| So simple and yet I'm stumped. I have four points (with x, y, z coordinates) that make up a rectangle. I want to rotate the rectangle around it's own center. How do I do that? |
||||
|
||||
![]() Jan-Lieuwe Member since: 11/30/2004 From: Harderwijk, Netherlands |
||||
|
|
||||
| Your coordinates should be relative to the center of the rectangle, placed at the origin (so the 4 points are local coordinates, the pivot of the rectangle - its center in your case - is located at 0,0,0). Transform each point by using a 4x4 matrix which contains the transformation steps you want to apply to the rectangle, for example: * rotation around the z axis * followed by a rotation around the y axis * followed by a rotation around the x axis * followed by a translation |
||||
|
||||
![]() Buckshag Member since: 5/30/2006 From: Rotterdam, Netherlands |
||||
|
|
||||
| You have to apply a pivot transformation first. This would translate your vertices so that its center of rotation will be the pivot. Assuming you only have a translational pivot point, this goes like: transformMatrix = TranslationMatrix( -pivotPoint ) * RotationMatrix( rotation ) * Translationmatrix( pivotPoint ) * TranslationMatrix( translation ) Where pivotPoint is the center of rotation, rotation is the rotation of your quad and translation its position. Of course you can optimize this, but it shows the idea. If you have a 10x10 quad with the lower left corner at 0,0,0 and upper right at 10, 10, 0 then your pivotPoint would be 5,5,0 to make it rotate around the center. Hope that helps :) Edit: of course I'm assuming you cannot modify the vertex data here. You can also just translate all vertices so that 0,0,0 is the center of rotation. This is also basically what the transform matrix with pivot support does. |
||||
|
||||
![]() markookram Member since: 1/3/2008 From: Maribor, Slovenia |
||||
|
|
||||
| Could you write me a practical example of this please? |
||||
|
||||
![]() jollyjeffers Moderator - DirectX and XNA Member since: 3/16/2000 From: London, United Kingdom |
||||
|
|
||||
| Moved to 'Maths & Physics' for any further discussion... Jack Hoxley [ Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ] |
||||
|
||||
![]() yosh64 Member since: 7/11/2007 |
||||
|
|
||||
| hey Basically you define a new coordinate system for the group of points. You would place the origin of the coordniate system at the point of the pivot. Now incase you don't know, basically a coordinate system defines which way/direction is up/down, which way is left/right, and which way is forwards/backwards. You will need to have some knowledge of matrices and such also, as coordinate systems are represented with a 4x4 matrix, and are combined (added together) via matrix multiplication. I won't bother going any further into things incase you already know. So what you do is instead of changing/rotating the points, you just change/rotate each axis of your coordinate system and draw the points relative to it. I hope this makes some sense. edit I quickly put together some pesudo code, I hope it is okay :).
render()
{
// push current modelview matrix onto stack
// note, I assume the modelview matrix is already set as expected, whether it be in
// world space, or transformed into eyespace by the camera or whatever
glPushMatrix();
// note, if this was a class then you would probably have this matrix defined inside it, and have
// functions to alter it, but I just do these things here to make the example easier to read
CMatrix rectMatrix;
rectMatrix.location = pivot.position; // set origin to position of pivot
rectMatrix.rotate(1, 0, 0, 45.0f); // rotate 45 degrees around X axis
// note, the group of points should be defined in object space
glMultMatrix(rectMatrix.matrix); // multiply/combine with current opengl modelview matrix
OnDrawGroupOfPoints(); // draw group of points
// restore previous modelview matrix from stack
glPopMatrix();
}
cyas [Edited by - yosh64 on January 28, 2008 5:57:44 PM] |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|