Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Quaternion problem

Posted on 2003-03-30
Medium Priority
Last Modified: 2013-12-26
I'm using quaternions to navigate a "space ship" in a 3D world. The mouse is used to point the ship in any desired direction, and that part is working just fine. The problem is that I don't know how to actually move the ship in the direction I'm facing.
So the question is: How do I get the values to put in the glTranslate-function, given a quaternion that describes the current rotation?

I'm using openGL and C.

Question by:Pegu
  • 3
  • 2

Expert Comment

ID: 8236559
A simple method would be to express the spaceships velocity as a vector and apply the quaternion rotation to that vector.  Then you could translate your spaceship using the rotated velocity vector.

Author Comment

ID: 8237163
Ok... I had pretty much figured that much out. I'm afraid the problem here is the math part. Lets say I have a rotation quaternion (w=0.92,x=-0.38,y=0,z=0). This would have me heading upwards at an angle of about 45 degrees (with the Z-axis as the default direction) and thus giving a vector looking something like (0.7,0.7,0.0).
But how exactly do I apply the quaternion to the velocity vector in order to reach this result?

Author Comment

ID: 8237197
Oops.. The velocity vector in the above example should of course be (0.0,0.7,0.7).
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Expert Comment

ID: 8240618
To rotate a vector with a quaternion, do the following:

(0, rotatedVector) = q_conj * (0, vector) * q

Where q is the quaternion, q_conj is q conjugated and (0, vector) is the vector expressed as a quaternion with the w component set to 0.

Author Comment

ID: 8246832
Hmmm, still can't make it work..
Ok, let me see if I've understood this.
Let's say the negative z-axis is my default direction. I'm heading 45 degrees upwards with a rotation quaternion (w=0.92, x=-0.38, y=0, z=0) and a velocity vector (0.0, 0.707, -0.707). I then turn 20 degrees upwards giving me a rotation quaternion (w=0.84, x=-0.537, y=0, z=0).

So to find the new velocity vector (0.0, 0.906, -0.423) I would calculate (0.84,0.537,0.0,0.0) * (0.0,0.0,0.0,-1.0) * (0.84,-0.537,0.0,0.0), with Q1*Q2=(w1.w2 - v1.v2, w1.v2 + w2.v1 + v1*v2)?
Or should the middle term, (0.0, vector), be the current velocity vector (0.0,0.707,-0.707)?

This quaternion math is a bit hard to grasp, so I could really use a more extensive example!

Accepted Solution

Aleph earned 200 total points
ID: 8253394
The middle quaternion is constructed from you current vector.  If you have a quaternion q representing a rotation, you form a quaternion qv from your vector by letting the w element of qv be zero, and the other elements correspond to the vector elements. The rotation is then performed by doing the quaternion multiplication q_conj * qv * q, giving you a new quaternion which represents your new, rotated vector. Just skip the w element in it (which is zero) and you have your new vector.

If you want to rotate the vector (0.0, 0.707, -0.707) with the quaternion (w = 0.84, x=-0.537, y=0, z=0) you do the quaternion multiplication:

(0.84, 0.537, 0, 0)*(0, 0, 0.707, -0.707)*(0.84, -0.537, 0, 0) = Q

After that you have your new vector as x, y, z in Q. I assume you know how to multiply quaternions. Otherwise, tell me.

To combine two rotations, each represented by a quaternion, you just multiply them. q = q1 * q2 will be equivalent of a rotation first by q1 and then by q2.

This is because: (q1*q2).conj * qv * (q1*q2) = q2.conj * q1.conj * qv * q1 * q2

Actually, one can also rotate vectors by doing q * qv * q.conj instead. This corresponds to the inverse rotation of what I have described above. Combining two rotations is then done in the opposite way, that is q = q2 * q1. Which one you use depends on how you create the quaternion. It is a matter of taste really. When I think about it, the latter convention is probably the more common, albeit not the one described by Shomake in the original paper about quaternions.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question