Sign in to follow this  
kucheg

3D Quaternion Rotation Problem!!!

Recommended Posts

I am programing a simple SpaceShip Simulator for a UNI project, similar to the one on spacesimulator.net. But I am having trouble rotating the ship correctly, What I have done is create 3 vectors for the ship(Heading, Up and Side), so it can rotate around it's own axies, and I am using Quaternions to rotate two vectors around the third one. To simulate Roll, I will rotate the Side and Up vectors along the Heading Vector, and so on... All of this works well, if it is rotated by only one axis, maybie two, but if I try to rotate along all three axis then I get problems, I heard this could be gimbal lock... but shouldn't using Quaternions fix the problem! Here is some code if it helps. //Call rotateHeading to rotate the heading vector about the up vector //Rotate Heading Vector About the Up Vector rotateHeading( RAD*(playerShip.physics.rotation[1]-prev[1]), playerShip.physics.up[0], playerShip.physics.up[1], playerShip.physics.up[2] ); Here is the rotation function... void rotateHeading(double Angle, float x, float y, float z) { quaternion temp, quat_heading, result; //Create the Temp Quat to roatate by temp.x = x * sin(Angle/2); temp.y = y * sin(Angle/2); temp.z = z * sin(Angle/2); temp.w = cos(Angle/2); //Making the heading vector into quat quat_heading.x = playerShip.physics.heading[0]; quat_heading.y = playerShip.physics.heading[1]; quat_heading.z = playerShip.physics.heading[2]; quat_heading.w = 0; //Apply the quat rotation result = mult(mult(temp, quat_heading), conjugate(temp)); //The resulting quat will give you the rotated vector playerShip.physics.heading[0] = result.x; playerShip.physics.heading[1] = result.y; playerShip.physics.heading[2] = result.z; } Thank you in advance. Oh and if you need more info just ask... The project is due in a week and I need to get this fixed.

Share this post


Link to post
Share on other sites
This problem should be pretty easy to fix. However, this forum has a pretty strict no-homework policy, and I hesitate to answer for fear of violating it. Perhaps you could ask your teacher if it's ok to get some help online? (Or perhaps I'm being overly cautious...)

Meanwhile, I think I'd be safe in saying that no, quaternions do not magically fix gimbal lock, and that furthermore you really don't need them for what you're doing.

Share this post


Link to post
Share on other sites
Oh, well that’s a relief. I guess I made the problem a little more complicated then it should have been. I will send an e-mail to my lecturer today and ask if it okay to get some help online?

The reason I wanted to use quaternions, is that I can rotate the ship about it's own axies, because if I rotated it about the Global axies the movement would look unrealistic, if you get what I mean.

Once again Thanks For the help.

Share this post


Link to post
Share on other sites
Quote:
The reason I wanted to use quaternions, is that I can rotate the ship about it's own axies, because if I rotated it about the Global axies the movement would look unrealistic, if you get what I mean.
I'll certainly be glad to help, once we're in the clear with the forum mods. Meanwhile, I'll just tell you that quaternions have absolutely nothing to do with whether or not you can rotate about a local axis. You can do local rotations with matrices or vectors just as easily.

Share this post


Link to post
Share on other sites
Still Waiting for a reply to my e-mail. Do you think you could provide me with a link to some info on what I want to do, or maybe what I should be searching for?

I also downloaded the Freespace 2 Source Code, but I can't make much sense out of it... Plus there is 300,000+ lines of code!

Share this post


Link to post
Share on other sites
Ok I have a few things you might want to think about

I can’t see the data you are passing in but the formula for a quaternion calls for the vector part to be normalized. This might already be done when your first rotation call is performed but make sure you always normalize the vector.

// x, y, z need to be normalized
temp.x = (x / magnitude) * sin(Angle/2);
temp.y = (y / magnitude) * sin(Angle/2);
temp.z = (z / magnitude) * sin(Angle/2);
temp.w = cos(Angle/2);

Another thing is why don’t you just pass in an angle and vector?

Last thing I would note is that I don’t know what lib you are using for your quaternion type but take advantage of its constructor(s).

Share this post


Link to post
Share on other sites
Allright, I got an e-mail from my lecturer and this is what he said

"it is ok to get help online as it is one way to demonstrate that you can be independant (useful for employment purposes) but this help needs to be acknowledged."

So I guess it's fine if you help me, you will also get a mention in my documentation :)

One of the problems of the project is that we can't use OOP, as we have not learned it using C++ yet, and it won't be fair on those who don't know it. Therefore I can't really use any class libraries.

I got the code to do the Quaternion calculations from Robert Crossland http://www.gamedev.net/reference/articles/article1997.asp

Dean Johnson - Thanks for the link. I don't have the time to read it completely at the moment but it looks as if it will help.

Share this post


Link to post
Share on other sites
Okay, from the link Dean gave, I will try use this formula to turn the ship:

F' = cos(y) F + sin(y) L
L' = - sin(y) F + cos(y) L
U' = U

F'' = cos(p) F' + sin(p) U'
U'' = -sin(p) F' + cos(p) U'
L" = L'

L1 = cos(r) L'' + sin(r) U''
U1 = -sin(r) L'' + cos(r) U''
F1 = F''

But I don't understand what it means by

The final vector triplet {L1, U1, F1} replaces the original {L, U, F} and gives the 3x3 rotational part of the aircraft's new world matrix, the world transform matrix that you pass to D3D in order to draw the craft in its new orientation.

Specifically "3x3 rotational part of the aircraft's new world matrix"

Do I have to copy the values into the openGL modelview matrix or multiply the Modelview matrix by these values? Something else perhaps?

Share this post


Link to post
Share on other sites
Success, I have got the algorithm implemented to rotate the Forward, Up and Side Vectors, and it works perfectly!

Now how do I get the ship to rotate with these vectors? I have tried using the glRotatef() function to rotate around the vectors, but the ship is rotating out of 'sync' with the vectors... Any suggestions?


Share this post


Link to post
Share on other sites
Quote:
Do I have to copy the values into the openGL modelview matrix or multiply the Modelview matrix by these values?
Yes, I think this will be the easiest way to load the ship's orientation into OpenGL. How to do that exactly depends on whether you want an object or view transform. (That is, are you viewing the ship from outside, or viewing the world from the point of view of the ship.)

Share this post


Link to post
Share on other sites
Cheers guys. Got it all sorted now. Now I can stop stressing and continue coding in some fun.

If you don't mind, can I have your name jyk, so I can give you and Dean some credit for helping. You can e-mail it if you don't want to post it on the forum.

Thanks Again :) - Will post screens when done too -

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this