I can provide more detail as necessary.
I have code but id prefer not to post it in here so email me for it.
Here is the question:
I have had good luck with 3D rotations using Quaternions.
This page has all the pertinent info:
http://www.cprogramming.com/tutorial/3d/quaternions.html
Now, multiplying two quaternions together
should give you a rotation that is the equivalent of doing
the two respective rotations (in a certain order of course).
I am NOT having success with this and want to know what i am
doing wrong.
It seems to work sometimes but sometimes it doesnt.
Ex. Say i have a
Vector = (xyz) 1, 0, 0
rotate around X axis - 1, 0, 0
rotating that vector X degrees should ALWAYS give 1, 0, 0 as a
result vector right?
however if i rotate it 90 and then 90 the answer is completely wrong.
90 and 0 works, 0 + 0 works, but many others (0-360) + (0-360) DONT.
shouldnt they?
is there a special case for rotating vectors around axes that are the same?
like i said i have code that would clarify
but i cant post it here so let me know and i can provide it.
Here it is briefly:
Vector v1 = 1, 0, 0 (x axis)
Axis a1 = 0, 1, 0 (y axis)
Angle ang1 = 90 deg
Vector result1 = 0, 0, 1 (z axis)
Axis a2 = 1, 0, 0 (x axis)
Angle ang2 = 90 deg
Vector result2 = 0, 1, 0 (y axis)
-----------------------
Quaternions q1, q2 =
ang = Math.toRadians(ang);
q.w = Math.cos(ang / 2.0);
q.x = axis.x * Math.sin(ang / 2.0);
q.y = axis.y * Math.sin(ang / 2.0);
q.z = axis.z * Math.sin(ang / 2.0);
the quaternion matrices are made by:
q.m[0] = 1 - 2 * (q.y * q.y - q.z * q.z);
q.m[1] = 2 * (q.x * q.y - q.w * q.z);
q.m[2] = 2 * (q.x * q.z + q.w * q.y);
q.m[3] = 2 * (q.x * q.y + q.w * q.z);
q.m[4] = 1 - 2 * (q.x * q.x - q.z * q.z);
q.m[5] = 2 * (q.y * q.z - q.w * q.x);
q.m[6] = 2 * (q.x * q.z - q.w * q.y);
q.m[7] = 2 * (q.y * q.z + q.w * q.x);
q.m[8] = 1 - 2 * (q.x * q.x - q.y * q.y);
and the product of 2 quats
Quaternion product =
product.w = (q2.w * q.w) - (q2.x * q.x) - (q2.y * q.y) - (q2.z * q.z);
product.x = (q2.w * q.x) + (q2.x * q.w) + (q2.y * q.z) - (q2.z * q.y);
product.y = (q2.w * q.y) - (q2.x * q.z) + (q2.y * q.w) + (q2.x * q.x);
product.z = (q2.w * q.z) + (q2.x * q.y) - (q2.y * q.x) + (q2.z * q.w);
and then i form the Q Matrix as usual from w,x,y,z and mult it by the
orig vector (1, 0, 0).
this works.
but sometimes some rotations dont work.
So essential i want to know if i can rely on this mult. tech.
to always work.
Also why doesnt the 1,0,0 around 1,0,0 - first 90 deg and then 90 degrees
doesnt work.
Thanks! |
Clarification of Question by
asdf123-ga
on
21 Nov 2005 18:41 PST
Haha, im sorry about the scattered ranting.
I began to talk about how rotating a vector around itself
works as individual rotations but fails as a combined rotation
obtained by multiplying the two individual rotations.
The long psuedo-coded example is an example that HAS worked
rotating the vector 1,0,0 90 degrees around 0,1,0 and then 1,0,0
should yield 0,1,0 and it does if you multiply the two individual
quaternion rotation matrices.
However, id like to know under what circumstances this fails or
if im doing something wrong.
i could have just made a mistake in my code or i could have possibly
misunderstood the intent / restrictions of multiplying two quaternions.
Does multiplying two rotation quaternions always yield a quaternion
that represents both rotations?
Also, everything ive read says that two unit quaternions multiplied
together should yield a unit length quaternion.
Sometimes i am NOT getting this!
I desperately need this to work. Please help.
|