Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Problems with combining Rotations, Translations and pivot points


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Zarl   Members   -  Reputation: 100

Like
0Likes
Like

Posted 30 March 2011 - 10:05 AM

Hi!

I am currently writing a game in C++ using OpenGL. I have implemented a Quaternion class to handle 6DOF movement and it worked great until the day I realised that I need a pivot point systmem in wich each of my models would rotate around a pivot point. If I were using glRotate I would write the translation for each separate mesh like this:
// add pivot offset
glTranslatef( pivotVec.x, pivotVec.y, pivotVec.z );

// rotate
glRotate( orientation.x, 1, 0, 0 );
glRotate( orientation.y, 0, 1, 0 );
glRotate( orientation.z, 0, 0, 1 );

// remove pivot offset
glTranslatef( -pivotVec.x, -pivotVec.y, -pivotVec.z );

// translate to position
glTranslatef( position.x, position.y, position.z );

It seems so simple, the matrix derived from the quaternion would be sent to OpenGL with a glMultMatrix call. The thing that confuses me is that to translate and rotate correctly, I had to place the glTranslate command before the glMultMatrix command. I have re-read chapter 3 in red book a few times and I am still convinced that something is wrong as it clearly states that if you what to translate an object globaly with the rotation applied at the translated local origin, you should call glRotate first. I have also tried to make the best out of thing to try and achieve my pivot point effect with my current system, but no matter what I do, just could not come up with a way to achieve that effect. I am not dumb so I realised that if any one would be kind enough to help me, he/she would probably want some source code of my quaternion class and my transformation code, so here it is:

Quaternion to Matrix:
void Quaternion::CreateMatrix(float *Matrix)
{
	if(!Matrix) 
		return;

	Matrix[0] = 1.0f - 2.0f * (y * y + z * z);
	Matrix[1] = 2.0f * (x * y + z * w);
	Matrix[2] = 2.0f * (x * z - y * w);
	Matrix[3] = 0.0f;
	
	Matrix[4] = 2.0f * (x * y - z * w);
	Matrix[5] = 1.0f - 2.0f * ( x * x + z * z );
	Matrix[6] = 2.0f * (z * y + x * w);
	Matrix[7] = 0.0f;

	Matrix[8] = 2.0f * (x * z + y * w);
	Matrix[9] = 2.0f * (y * z - x * w);
	Matrix[10] = 1.0f - 2.0f * (x * x + y * y );
	Matrix[11] = 0.0f;

	Matrix[12] = 0;
	Matrix[13] = 0;
	Matrix[14] = 0;
	Matrix[15] = 1.0f;
}

Transformation code:

	//create our local object matrix
	glPushMatrix();

	//translate
	glTranslatef(pos.x, pos.y , pos.z);

	//convert the orientation quaternion into a rotation matrix for OpenGL use
	float * rotMatrix;
	rotMatrix = new float[16];
	ori.CreateMatrix(rotMatrix);
	glMultMatrixf(rotMatrix);
	delete [] rotMatrix;

	//draw stuff
	// Drawing code goes here...

	glPopMatrix();

All replies will be greatly appreciated!

Sponsor:

#2 haegarr   Crossbones+   -  Reputation: 5590

Like
1Likes
Like

Posted 30 March 2011 - 10:35 AM

If you compute a transformation matrix M from a rotation R and a translation T in the following order (using column vectors as is usual for OpenGL)
M := T * R
and apply it to a vertex position v
v' := M * v = ( T * R ) * v = T * ( R * v )
then it appears as that R is applied on the original v, and T is applied on the rotated v (i.e. on R * v).

In OpenGL, the equivalent sequence of commands looks like
glLoadIdentity
glTranslate
glRotate
glVertex
Notice that the commands from top to bottom have the same order as the formula terms from left to right. The command sequence is headed by loading a identity matrix, because glTranslate (as well as glRotate and glScale and glMultMatrix) is multiplicative, so in fact the formula
v' := I * T * R * v
is computed.

Now, the point 0 counts ever to the rotation axis, because 0 is the one point that cannot be changed by multiplication with a pure rotation matrix
R * 0 == 0
and such an invariance is a criteria for the rotation axis. Hence, at the moment when you apply the rotation, you must ensure that any point you want the rotation axis pass through, is shifted to 0. The translation to accomplish this must obviously be on the right of R to be already "active" when R is applied (see the explanation above). Let us assume that A is the translation that describes the point through which the axis passes. Then
R * A-1
will do it (notice that the inverse A is used; this is equivalent to a negated translation).

Now, after rotation you don't want the pivot point to rest at 0. You want it back where it was formerly. Hence undo the translation. This must be done obviously left of R because you want to translate the already rotated vertex back. Hence in summary
A * R * A-1
is the rotation around a pivot point.


Together with the object translation T (i.e. applied on the pivot rotated vertex) you'll have
T * A * R * A-1

That said, I assume that the correct order of your example would be

// translate to position
glTranslatef( position.x, position.y, position.z );

// add pivot offset
glTranslatef( pivotVec.x, pivotVec.y, pivotVec.z );

// rotate
glRotate( orientation.x, 1, 0, 0 );
glRotate( orientation.y, 0, 1, 0 );
glRotate( orientation.z, 0, 0, 1 );

// remove pivot offset
glTranslatef( -pivotVec.x, -pivotVec.y, -pivotVec.z );
or alternatively with a glMultMatrix (from the quaternion) instead of the glRotate invocations.

#3 Zarl   Members   -  Reputation: 100

Like
0Likes
Like

Posted 30 March 2011 - 12:05 PM

Thanks a lot for answering my question so quickly!

I tried your approach and it was quite good! One problem is still left and that is that objects transformed by the following code gets a bit offset depending on how they are oriented. An object oriented with the global axes have perfect pivot point offset in the direction I wanted the offset to be in. But objects with other orientations are moved along the global axes at the first glTranslate and are rotated along the local axes during the second glTranslate call. I wonder if it is possible to make it so that this offset is always moved along the local axes all the time, perhaps with a quaternion multiplication on the pivotOffset vector?

	glTranslatef(pos.x, pos.y , pos.z);

	glTranslatef(pivotOffset.x, pivotOffset.y, pivotOffset.z);

	float * rotMatrix;
	rotMatrix = new float[16];
	ori.CreateMatrix(rotMatrix);
	glMultMatrixf(rotMatrix);
	delete [] rotMatrix;

	glTranslatef(-pivotOffset.x, -pivotOffset.y, -pivotOffset.z);


EDIT:
I realise that multiplying the pivotOffset vector with the orientation quaternion before the later of the glTranslate calls won´t work as it would just neutralise the pivot effect I am after, is there any way I could achieve the effect described earlier in this thread?

#4 haegarr   Crossbones+   -  Reputation: 5590

Like
0Likes
Like

Posted 30 March 2011 - 01:04 PM

I tried your approach and it was quite good! One problem is still left and that is that objects transformed by the following code gets a bit offset depending on how they are oriented. An object oriented with the global axes have perfect pivot point offset in the direction I wanted the offset to be in. But objects with other orientations are moved along the global axes at the first glTranslate and are rotated along the local axes during the second glTranslate call. I wonder if it is possible to make it so that this offset is always moved along the local axes all the time, perhaps with a quaternion multiplication on the pivotOffset vector?

A transformation is ever to be interpreted w.r.t. the space that is active at the moment when the transformation is applied. So each transformation can be understood as changing the space. See for example the A-1 transformation we've used above: Its sense was to change the space into one where the pivot point is at 0, so that the rotation could be applied as desired.

Because the pivot point translation is applied first, its co-ordinates are given in the same space in which the vertices are given (usually called the "model space" or "local space"). At this moment the model space and the global space (which is the usual name of the space that is active after you've applied a (not to say the last) transformation) are coincident, because you haven't applied any transformation yet.

This concept works for other situations, too. If your pivot point isn't given in the correct space, you need to transform first into the space where the pivot point is defined in.

Unfortunately I haven't understood what you want to achieve. The A .. A-1 translations don't change the rotation axis in any way, and the rotation doesn't change the pivot point in any way (all this when seen from the global space). How do objects have "other orientations"?

#5 Zarl   Members   -  Reputation: 100

Like
0Likes
Like

Posted 30 March 2011 - 01:57 PM

Unfortunately I haven't understood what you want to achieve.



Just to make it clear, I want my mesh class (from which the transformation code comes) to transform each object by the position vector in global co-ordinates, I also want it to rotate the object using a quaternion, and that rotation should occur around a point specified by a vector in relation to the position specified by the position variable. I think a image would explain this much better:

http://img690.images...ttoachieve.jpg/

Do you see what I mean now?

#6 haegarr   Crossbones+   -  Reputation: 5590

Like
0Likes
Like

Posted 31 March 2011 - 01:41 AM

From the image I would say that you want to use the pivot point not only as pivot point but as the origin of the model. This is because the right figure shows that the pivot point comes to rest at the co-ordinates of position.

The pivot point is at 0 before and after the rotation R. It should be at the position defined by T. So far we have used
T * A * R * A-1
and hence the 0 after the rotation was translated by T * A and that is obviously in general not equal to T. So drop the translation by A
T * R * A-1
to let the pivot point and the origin coincident for T.

However, from the textual description the statement "rotation should occur around a point specified by a vector in relation to the position specified by the position variable" means IMHO something different. It means that the center of rotation is the sum of position and pivot. That would not match the right figure in the image, so I assume it is wrong!?

#7 Zarl   Members   -  Reputation: 100

Like
0Likes
Like

Posted 31 March 2011 - 09:12 AM

Oh! I get it now! I have been a bit confused as you can probably tell and you have made my day by making me realising that I was in fact asking for something I did not want. What I want is what I drew in that image, and I have achieved that thanks to your algebra, showing that what I really wanted is T * R * A-1 and nothing else. So I thank you from the bottom of heart for helping me sole my problem!





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS