# Rotating a group of points around a pivot

This topic is 4010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
Could you write me a practical example of this please?

##### Share on other sites
Moved to 'Maths & Physics' for any further discussion...

##### Share on other sites
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]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634103
• Total Posts
3015539
×