PDA

View Full Version : Rotation problem



best4gotten
12-19-2008, 11:28 AM
Hi Guys
I have a question about rotation I hope I am in the correct forum.

I want to draw my car model in openGL which is made in 3d max. I have problem setting the orientation. In 3ds MAX My model is placed such that the car is pointing towards -y Axis and the top axis is +Z.
However in OpenGL I must draw my Car in the way that it should be on point L (translate to L) facing to wards vector LM and the top Vector should be LC.
This is how I tried this in openGL
(LC and LM are unit vectors and LC and LM have 90 Degree Angel)

glPushMatrix();
Translate(L.x,L.y,L.z);

top=(0,0,1);
front=(0,-1,0);

Angel1=dotproduct(top,LC);// Angel between Z axis and LC vector
RotAxis1=crossProduct(top,LC);// Rotation axis between top and LC
glRotated(Angel1(indegrees), RotAxis1.x,RotAxis1.y,RotAxis1.z);

Angel2=dotproduct(front, LM);// Angel between -Y axis and LM vector
RotAxis2=crossProduct(front,LM);// Rotation axis between front and LM
glRotated(Angel2(indegrees), RotAxis2.x,RotAxis2.y,RotAxis2.z)

Car.Draw();
glPopMatrix();


The problem is these rotations work properly when i do it Alone however when I do that together second rotation changes the first roation aswell and I get a wrong orientation.

I want to ask is there anyway I can combine these rotations?
that means only one rotation is enough to set the right orientation or is there any other mistake I am doing or my procedure is wrong Please let me know

Regards

MaxH
12-19-2008, 12:34 PM
I find that when the desired orientation of an object is defined by vectors (instead of angles), it's easier to compute the transformation matrix directly and apply it with a glMultMatrix, than trying to use glRotate's. Your translation is o.k., but to orient the car, I'd do something like this. Define an array OR[16]. Initialize it to zero's except for the last element which should be 1. Let vector C be your LC (up vector), M be the pointing vector (your LM), and Q = C cross M. You are almost done. Fill array OR as follows: OR[0] = Q[0], OR[1] = Q[1], OR[[2] = Q[2], OR[4] = -M[0], OR[5] = -M[1], OR[6] = -M[2], OR[8] = C[0], OR[9] = C[1], and OR[10] = C[2]. Now instead of making calls to glRotate, call glMultMatrix (0R). That should do the trick. You still have to use glPushMatrix and glPopMatrix as you are doing. Let me know if it works.

Rosario Leonardi
12-19-2008, 01:31 PM
I want to ask is there anyway I can combine these rotations?
When you combine rotation you have to remember that the first rotation rotate the object and his local axis too.. :-S

After the first rotation the object is facing the Z axis so your front vector is wrong, you have to rotate it. :)

MaxH solution is the best if you know at least two axis.
The rotation matrix is the representation of the tree axis X, Y Z


x1 x2 x3 v1
y1 y2 y3 v2
z1 z2 z3 v3
0 0 0 1
So you can easily compute it.
Where v is the translation vector multiplied for the rotation matrix. :)

Another thing... better to put the glTranslate at the end or you will rotate the object on a strange pivot and you will not find the object in the desired position.
The best solution is ALWAYS to compute the matrix by hand, fixed pipeline is dead.

MaxH
12-19-2008, 09:16 PM
I have written a short, OpenGL, GLUT, program to demonstrate the technique I discussed previously. It is at

Pointing Car Demo (http://pastebin.com/m1c068a3f)

Don't be scared off by the fact that it's 276 lines of code. Most of those lines develop a way to draw a simple car, put it in a scene, rotate the scene, etc.

All of the important stuff takes place between lines 215 and 221. In those 6 lines of code (sic), a matrix is derived that performs the requested orientation of the car. The matrix is actually applied to the car on line 243. No calls to glRotate are required to re-orient the car!

best4gotten
12-20-2008, 07:58 AM
First of all thanks a Lot for Reply and specially MAXH your effort to write that CODE for explanations secondly for me its Hard to Believe but this one line of Code Worked at the First HIT this was Amazing

Fill array OR as follows: OR[0] = Q[0], OR[1] = Q[1], OR[[2] = Q[2], OR[4] = -M[0], OR[5] = -M[1], OR[6] = -M[2], OR[8] = C[0], OR[9] = C[1], and OR[10] = C[2]. Now instead of making calls to glRotate, call glMultMatrix (0R)


now the next Part is I do not understand How did you do that??? :confused:
I hope I am not Bothering but can you Explain me a Bit what made you -ve my M vector??
How did you Found out about the Order in which you put these rotations ( Destination then M vector and then C vector )??

What if there were three rotations? I mean If i was setting the orientation of a JET instead of a car then there would have been 3 rotations all I want to ask is WHAT IS THE BASIC rule behind it??
If the answers are time consuming at least give me something to read ...

Even If you dont answer I would still say thank you very very much

Rosario Leonardi
12-20-2008, 09:37 AM
all I want to ask is WHAT IS THE BASIC rule behind it??
As I told you before you have to consider that rotation rotate the local axis of the object, so you have to be very careful when you combine rotation.
http://www.opengl.org/resources/faq/technical/transformations.htm#tran0070

About the rotation matrix interpretation...
check these two images taken from the book "3D Math Primer For Graphics And Game Development"
http://img440.imageshack.us/img440/5932/matrixidentityss1.png (http://imageshack.us)

http://img211.imageshack.us/img211/3361/matrixtransformationir7.png (http://imageshack.us)
as you can see the matrix represent the axis of the transformed object (rotated and scaled).

Another good solution to avoid multiple glRotate is to use quaternion an then compute the rotation matrix by hand.

MaxH
12-20-2008, 05:22 PM
This technique most definitely would work for what you think of as '3 rotations' (a jet). I use it to orient satellites in orbit around the earth. Let me think about how to explain it clearly, or find a good reference for you. I'll get back to you.

MaxH
12-21-2008, 01:07 PM
The 'Rule' for filling my orientation matrix (OR[16]) is here (http://pastebin.com/m33c00c1a).

best4gotten
12-21-2008, 01:15 PM
Thanks a Lot MaxH... I hope you Do understand why I am looking for a reference/Explanation. I have been Struggling with these rotation problems for a long time and alternatives are never that Easy to implement or rather not easy to understand. Your matrix multiplication was SPOT ON and believe me I was't even a BIT confident about it but Surprisingly it worked... i am really waiting for a Reply Thanks again


@Rosario Leonardi Thanks for the reply mate
I do understand the concept of rotation rotate the local axis of the object, But I was just wondering If the second rotation disturbs the First one than what would be the correct way of doing that and as i said before MaxH's technique worked I just want to know the REAL RULE behind it ( In which Order and How the vector would be placed in the matrix) and I hope MaxH would find a simple explanation for it :D

Thanks Once again

best4gotten
12-23-2008, 09:27 AM
Hi MaxH... Sorry for Late reply I think we Posted msg at the same time so could not realize that you Have already replied my question...


I must admit its the most Simplest Rotation technique when you have vectors/Angle it self...

thanks a Lot for your Effort in writting a lengthy Example and explanation Thumbs Up to you... I would have gone for Quaternions and you know its not that easy to Implement and understand... you surely saved a Lot of time or rather a Week...

Best Regards

MaxH
12-23-2008, 11:24 AM
Yes. We did post at just about the same time. It was confusing to me too. My 'lengthy Example' was not written from scratch. Most of it came from previously written examples. Tailoring it to your situation only took an hour. The method of directly computing a transformation matrix is useful when an object's desired orientation is defined by vectors or points (which is very common). The resulting matrix is the same as the matrix that OpenGL would generate internally if you somehow figured out a series of glRotate calls that accomplished the same, desired, re-orientation.