Solved

# Quaternion problem

Posted on 2003-03-30
Medium Priority
325 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
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 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

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
###### Suggested Courses
Course of the Month13 days, 7 hours left to enroll