# OpenGL Rotating submodel

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

## Recommended Posts

I load an OBJ model into OpenGL. The model is a car. It consists of 5 submodels (groups) : frame, wheel1, wheel2, wheel3, wheel4. I use a rotation matrix to rotate the entire object around the 0 point (0,0,0). For each coordinate in the model I multiple with a rotation matrix, depending on the input. I made the functions so that the submodels can rotate around their midpoint (origin). The rotation of models around the point (0,0,0) or around their midpoint/origin works fine. However, I want to rotate the wheels forward, while the model/car is rotating around its origin. I thought to use rotation matrix rY. This is fine if the model is in position of Image A. However in Image B: the rotation matrix the Z coordinate does not change. So the positions aren't right anymore, if the model matrix is rotated. (See Image B.) I'm guessing I should use all 3 rotation matrixes somehow. The rotation matrix would depend on the 3 angles of rotation of the original model, but I dont know how. How would you rotate the wheels forward? (Could I also use a standard function of OpenGL?)

##### Share on other sites
Are you performing all of your rotations as 3x3 matrices? In 3d graphics transformation matrices are typically represented as 4x4 matrices, so that each matrix can store a rotation and a translation. If you only have 3x3 matrices I think that might be a problem for you, because you don't always want a rotation about an object's midpoint.

To do this properly you'd want to first rotate the wheel forward around its own midpoint, translate it to the proper position in the car, and then rotate it again around the center of the car.

Quote:
 (Could I also use a standard function of OpenGL?)

You can do this with OpenGL fixed function transformations. (These are depreciated, and you should learn to handle all of your matrix rotations yourself for forward compatibility, but they still work for now.) However you have to apply the transformations in reverse order due to the way the matrix multiplication stack works.

Assuming that your wheels rotate around the X axis by 45 degrees, and that the wheels are located at (+/- 1, 0, +/- 1) XYZ coordinates. Lets say you want to rotate the car by 30 degrees around the Y axis (up vector). This is what it would look like:

glLoadIdentity() //Reset everythingglRotatef(30,0,1,0)//Rotate the carDraw the Car frameFor Each Wheel(  glPushMatrix() //Save the original rotation matrix  glTranslatef(+/-1, 0, +/-1) //Move the wheel into position  glRotatef(45,1,0,0)//Rotate your wheel around its midpoint  Draw a wheel  glPopMatrix() //Load the original rotation matrix}

[I think I've got the rotations in the right order, but it has been a while since I used the fixed function equations, so someone can correct me if I've misspoke]

##### Share on other sites
The matrix is 3x3 (or 3xPolygonSize). I've the matrix stored in this way

It now works when I rotate the model around one axis (X,Y or Z) including the wheels. So Yaw, Roll or Pitch the object:
When using Yaw, Roll or Pitch the model goes back to its original position.
(inside: not drawn)

However when I do 2 or 3 rotations on the total model (for example coordinates*Ry and coordinates*Rz) it doesn't work. In other words, with for example Yaw and Pitch together the model doesn't get back in its original position.

I stored the total rotation of the angles, and wanted to rotate back with it.

So the problem is to get it back in it's original positions, when the model itself is rotated using 2 or 3 rotation matrixes.
I thought to do the rotations undone. (substracted), but this seems to work only in the first case.

I found out that if I do this in the opposite order that it was done, I get back into the intial position.

so:
pitch -92
yaw -92

However, if I do it in a different order it gets rotated, why is this?
Should I keep a list of each rotation that is done, or the order that the rotations of the total model is done?

[Edited by - walkaways on March 11, 2010 12:11:33 PM]

##### Share on other sites
Yes the order of rotations is very important, and if you change the order you will get different behavior.

You should take a look at the OpenGL Red Book, it will help you learn some fundamental concepts of 3d graphics, which should help you understand all of the questions you are asking.

There is a free online version (it is quite old, but the transformation concepts are still the same). Specifically in your case read through chapter 3: "Viewing"

Red Book Online Version

##### Share on other sites
I'll look into the book.
Because the order of rotation is important, should I store all rotations of the model in an array?
This would make it easy to reverse them all.

For example

orignal state -> pitch 30* -> roll 30* -> pitch 40* -> roll 30*

To get back to the original matrix position, one would go trough an array that contains this rotation in opposite direction, right?

curent_state -> roll -30* -> pitch -40* -> roll -30* -> pitch -30* -> original state

So to rotate the wheels in this situation from the original state:

roll -30* - pitch -40* - roll -30* - pitch -30* -> original state
rotate wheels
original state + rotated wheels -> pitch 30* - roll 30* - pitch 40* - roll 30*

Is it the 'right' way to keep an array of all rotations you've done with the model?

##### Share on other sites
For what reason do you feel the need to reverse your rotations? If you needed to get a matrix that "undid" all of your operations, then you would need to find the inverse of the original rotation matrix. You can do this by remembering all of the operations in reverse order, or there are other techniques for inverting a matrix that you can use. However I can't see why you would need to do this in your situation.

If you want to undo all your rotations (to draw another object for example), just reload the identity matrix, or better yet just use glPushMatrix/glPopMatrix (more appropriate for hierarchies).

For example if you have two cars you want to draw in different orientations:

//Do anything else in your scene you want.

glPushMatrix() //Store the current matrix on the stack
doCarRotations()
drawCar()
glPopMatrix() //we throw away the current matrix, and are back to what we had when we called pushmatrix
glPushMatrix() //save the matrix again
doCar2Rotations()
drawCar2()
glPopMatrix()

##### Share on other sites
I want to rotate the matrix back to the original position, so that I can rotate the wheels forward.

If I have the car in the following position:

I could Roll, Yaw or Pitch it around the midpoint, but neither of these will cause the wheel to roll forward.

If I rotate back to the original state, its easy to use Roll to move the wheel forward. Then I could rotate back to position in picture 1 and draw screen again.

Should I do it differently?

Side note: Im applying rotation matrixes directly to the original matrix, so the original matrix is not stored anywhere.

//xMatrix = ( cos( Theta * (PI/180)) * Matrix)   + (sin(Theta * (PI/180)))*Matrix[i+2];// zMatrix[i+2] = (-sin( Theta * (PI/180)) * Matrix)   + (cos(Theta * (PI/180)))*Matrix[i+2];

##### Share on other sites
You should not have to translate the wheel back to 0,0,0 to rotate it. If you rotate the wheel last, it will rotate on its local axis (assuming you perform the matrix operation in the right order). It should not matter if the car (and thus wheel) is upside down, flipped over, whatever. Put the wheel wherever you want it, and if you call glRotate last it will rotate on its own axis.

If you look at the sequence that I put in my first post, this should be all you need.

It looks like you are using your own matrix solution, which might be confusing you. Do you understand the difference between pre-multiplying or post-multiplying with your rotation matrix? It is not commutative. I will suggest that you try to get it working with OpenGL transformations until you get a better grasp for transformations, then you can go back and implement your own solution again if you prefer.

Anyway this is all covered in the Red Book chapter I mentioned, specifically pay attention to the section "Thinking about Transformations". Go read that and think about it for a few hours, and see if it makes more sense to you. If after studying that you're still having trouble then come back and we'll see what we can do.

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002054
×