# Summing up quaternion issues

This topic is 4381 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I'm studying the topic of quaternions and I wanted to sum up some issues. I'd be glad if you approve or correct me if I'm wrong, thanks: 1. Basically we'll want to represent quaternions when we want to represent rotations, as they are convenient for interpolating rotations, and they can be contatenated faster than matrix concatenations (16 vs. 27 multiply-add operations); 2. When we want to represent a rotation of angle theta around an axis represented by a vector matrix A, we need the following values in the quaternion:
q = <w,x,y,z>
w = cos(theta/2)
x = sin(theta/2) * Ax
y = sin(theta/2) * Ay
z = sin(theta/2) * Az

3. When we have a given quaternion we can transform it into a rotation matrix using the following formulas:
M11 = 1 - 2y^2 - 2z^2
M21 = 2xy + 2wz
M31 = 2xz - 2wy
M12 = 2xy - 2wz
M22 = 1 - 2x^2 - 2z^2
M32 = 2yz + 2wx
M13 = 2xz + 2wy
M23 = 2yz - 2wx
M33 = 1 - 2x^2 -2y^2

4. Basically, suppose I store all my relative transformations (between an object and its container for example) as a quaternion for rotations and a vector for translations, I would need to follow the following in order to create a model-view transform matrix for an object -
Translation_vec=<0,0,0>
Rotation_quat=<1,1,1,1> {??? is this correct as a starting value? }
For each container starting from the world and finishing at the object, DO:
Translation_vec += current container translation_vec
Rotation_quat *= current container rotation_quat
End loop
Final transform matrix =
Rotation_quat turned matrix | translation_vec
---------------------------------------------
0             |        1

(Of course a more appropriate way would be to only use the direct container vec & quat and not go all the way from the top down, but this is just for my understanding here). Thanks a lot, Eldad.

##### Share on other sites
Quote:
 Original post by DadleFishRotation_quat=<1,1,1,1> {??? is this correct as a starting value? }

Sorry, no. It should be: <1,0,0,0>. This is what you would get if you construct your quaternion from any rotation vector using theta = 0.

##### Share on other sites
Thanks Ury.

Did I get the rest right?

Also, just to make sure, when I want A to be, say, the Z axis, I use <0,0,1> - correct?

Thanks again.

##### Share on other sites
Quote:
 Original post by DadleFishDid I get the rest right?

I am afraid not.

As you probably know, a vector p can be rotated by quaternion q using:
p' = q*p*Conjugate(q).

Don't forget that each child's position is given in its parent coordinate system and not the world coordinate system.
So if the parent coordinate system is rotated with respect to the world, you must rotate the child's position accordingly.

func( node, q, p )	p += q * node.p * Conjugate( q )	q *= node.q	node.matrix = build_matrix( q, p )		for each child of node		func( child, q, p )endcheck()	// Setup world coordinate system	p = ( 0, 0, 0 )	q = ( 1, 0, 0, 0 )	func( root, q, p )end

Quote:
 Also, just to make sure, when I want A to be, say, the Z axis, I use <0,0,1> - correct?

Just like you said, a rotation quaternion is constructed using a unit vector A, which is the axis around which you want to rotate the world by theta degrees.
So yes, if the axis is Z, then A = ( 0, 0, 1 )

##### Share on other sites
Let me verify that I understand you correctly. Basically my mistake is only with the translation part - where I should have used the building quaternion (q in your example) and its conjugate to update the current translation point - is that correct?

Thanks a lot for the code, it cleared things up very well!

Thats right.