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

x
Solved

# Quaternion problem

Posted on 2003-03-30
Medium Priority
336 Views
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.

/Peter
0
Question by:Pegu
• 3
• 2

LVL 1

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.
0

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?
0

Author Comment

ID: 8237197
Oops.. The velocity vector in the above example should of course be (0.0,0.7,0.7).
0

LVL 2

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.
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!
0

LVL 2

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.

Example:
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.
0

## Featured Post

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
Course of the Month11 days, 8 hours left to enroll