direction vector from quaternion?

Started by
5 comments, last by McNugget 20 years, 11 months ago
Hi all, I tried to learn everything about quaternions and matrices the past weeks and solved the loved gible lock now. When I try to move in 3D space I have now a new problem. My quaternion code is based on gametutorial 7 and some articles from gameasutra and gamedev, because the gametut. 7 is not very intuitive... So what I''m going to do is to get the direction vector from the quaternion. I modyfied the gametut. function so looks a bit different but that shouldn''t matter:
  
MFVector3D MFQuaternion::GetDirectionVector()
{
    MFVector3D v3dTmp;
	Normalize();

	v3dTmp.x = 2.0f*(x*z-w*y);
	v3dTmp.y = 2.0f*(y*z+w*x);
	v3dTmp.z = 1.0f-2.0f*(x*x+y*y);
	
    return v3dTmp;
}  
I dont really know what the hack is wrong with this. Maybe its not a mistake made here, so lets see another snip of code:
  
MFQuaternion &MFQuaternion::Set(float xT, float yT, float zT)
{
	MFQuaternion xQ(xT, 1.0f, 0.0f, 0.0f);
	MFQuaternion yQ(yT, 0.0f, 1.0f, 0.0f);
	MFQuaternion zQ(zT, 0.0f, 0.0f, 1.0f);
	
	*this = xQ;

	*this *= yQ;

 	*this *= zQ;

	return *this;
}

MFQuaternion &MFQuaternion::Set(float angle, float xT, float yT, float zT)
{
	float factor = xT*xT+yT*yT+zT*zT;
	if(!factor)
		factor = (float) EPSILON;

	float scaleBy = (float) (1.0/sqrt(factor));

	w = (float) cos(angle/2.0f);

	float sinHalfAngle = (float) sin(angle/2.0f);
	x = xT*scaleBy*sinHalfAngle;
	y = yT*scaleBy*sinHalfAngle;
	z = zT*scaleBy*sinHalfAngle;

	return *this;
}
	
MFQuaternion MFQuaternion::operator* (const MFQuaternion Q)
{
    MFQuaternion Qtmp;
    	Qtmp.x = w*Q.x + x*Q.w + y*Q.z - z*Q.y;
    	Qtmp.y = w*Q.y + y*Q.w + z*Q.x - x*Q.z;
    	Qtmp.z = w*Q.z + z*Q.w + x*Q.y - y*Q.x;
    	Qtmp.w = w*Q.w - x*Q.x - y*Q.y - z*Q.z;
    return Qtmp;
}

void MFQuaternion::operator*=(const MFQuaternion Q)
{
    MFQuaternion Qtmp;
	Qtmp.x = w*Q.x + x*Q.w + y*Q.z - z*Q.y;
	Qtmp.y = w*Q.y + y*Q.w + z*Q.x - x*Q.z;
	Qtmp.z = w*Q.z + z*Q.w + x*Q.y - y*Q.x;
	Qtmp.w = w*Q.w - x*Q.x - y*Q.y - z*Q.z;
    x = Qtmp.x;
    y = Qtmp.y;
    z = Qtmp.z;
    w = Qtmp.w;
}
  
My rotation is applied as follows:
  
    rotX = -mouse.y / 500;  // Mouse Look

    rotY = -mouse.x / 500;  // Mouse Look

//    rotZ += 0.001*Engine.Tools->procPower;


    qRotation *= MFQuaternion(rotX, rotY, rotZ);
//	qRotation.QuatToAxisAngle(axisX, axisY, axisZ, rotAngle);

	mRotation = qRotation.QuatToM4();
	glMultMatrixf(mRotation.v);
//	glRotatef((float) (rotAngle*PIUNDER180), axisX, axisY, axisZ);


    v3dDirection = qRotation.GetDirectionVector();
    qRotationT = qRotation;

    qRotation = MFQuaternion(0.0f, 90.0f, 0.0f) * qRotation;
    v3dRight = qRotation.GetDirectionVector();
    qRotation = qRotationT;

    qRotation = MFQuaternion(90.0f, 0.0f, 0.0f) * qRotation;
    v3dUp = qRotation.GetDirectionVector();
    qRotation = qRotationT;
  
The direction vectors I get are not orthogonal. They seem to be messed up when I rotate the camera (I have a little coordinate System on my screen [thanks to vincoof] so I can see what happend to them). Maybe someone can find the mistake or you can help with ideas... Regards, McNugget ps. Its me, DarkKiller....
Its me, DarkKiller ;)-------------------------------------------What is the Matrix?
Advertisement
I'm not inclined to try and check your maths but I can point you in the direction of the Math3D library which is quality open source code including a solid quaternion library. My code for getting a vector from a quat would appear equivalent to yours. It may be that your problem stems from your use of Euler angles in creating quat representations of rotations but I'm really not sure (using quats won't entirely eliminate gimbal lock if you create them using euler angles all the time). You may have more luck if you post this in the Math and Physics forum.

Geocyte Has Committed Suicide.

[edited by - geocyte on May 8, 2003 7:12:23 AM]
Geocyte Has Committed Suicide.
Why did you kill yourself?


ok, about gimble lock: I solved the problem allready.

What I were talking about was a problem getting the direction vectors from the quat.

Euler angles are used in combination with MFQuaternion(rotx, roty, rotz) so they''re converted to a quat before I multiply the quat with them.
That should not be the problem... But where is it then?

BTW. I cannot download the math library but I think I have just everything written there implemented yet. (exept the 4D-Vector )

Thanks for you help anyways! I''ll try to download the file later. Maybe gozzilla causes the problem with it ...



Its me, DarkKiller

-------------------------------------------
What is the Matrix?

Its me, DarkKiller ;)-------------------------------------------What is the Matrix?
try getting math3d from the original source math3d.sourceforge.net I think the other link was a BEOS build?

Anyway, sorry I can''t be more help with your actual problem. I''m no maths whiz, which is why i use math3d ;¬)

An idea though: can you construct a matrix from the quaternion and extract the direction vector from that? EG rotate the identity matrix by the quat and pull the basis vectors from the matrix... or something. Like I said, just an idea, and I''m no maths genius...

Dan
[size="1"]
Can someone mail the Windows C++ version?
I hate my Gozilla! I cannot download the file.

Thanks for the tip! I took a look at the library again and found many nice functions!

Getting Vectors from Matrix:
I will try that! Somewere I''ve read how to do that... *ugh*

Some other ideas?



Its me, DarkKiller

-------------------------------------------
What is the Matrix?

Its me, DarkKiller ;)-------------------------------------------What is the Matrix?
Ok, I tried getting the Vectors from the modelview matrix.
It works as far as the vectors are orthogonal.


      v3dDirection.x = mRotation.v[8];    v3dDirection.y = mRotation.v[9];    v3dDirection.z = mRotation.v[10];        v3dRight.x = mRotation.v[0];    v3dRight.y = mRotation.v[1];    v3dRight.z = mRotation.v[2];    v3dUp.x = mRotation.v[4];    v3dUp.y = mRotation.v[5];    v3dUp.z = mRotation.v[6];  


But the direction is wrong. Do I have to multiply this values with the modelview matrix to get the translated and rotated vectors as I have to do when I try to get a real vertex position from a translated and rotated model?

I will try that....

Its me, DarkKiller

-------------------------------------------
What is the Matrix?

Its me, DarkKiller ;)-------------------------------------------What is the Matrix?
Hi again.

I tried some things out and got nothing yet...

Well, from the matrix FAQ we know the following:


    Q13. How do I multiply one or more vectors by a matrix?-------------------------------------------------------  The best way to perform this task is to treat the list of vectors as  a single matrix, with each vector represented as a column vector.  If N vectors are to be multiplied by a 4x4 matrix, then they can be  treated as a single 4xN matrix:  If the matrix is defined as:        | A B C D |    M = | E F G H |        | I J K L |        | M N O P |  and the list of vectors is defined as:        | x1 x2 x3 x4 x5|    V = | y1 y2 y3 y4 y5|        | z1 z2 z3 z4 z5|        | 1  1  1  1   1|  Note that an additional row of constant terms is added to the vector  list, all of which are set to 1.0. In real life, this row does not  exist. It is simply used to make the orders of the matrix M and the  vector list V match.  Then the multiplication is performed as follows:            M . V = V'  | A B C D |   | x1 x2 x3 x4 x5 |   | A.x1+B.y1+C.z1+D A.x2+B.y2+C.z2+D ... |  | E F G H | . | y1 y2 y3 y4 y5 | = | E.x1+F.y1+G.z1+H E.x2+F.y2+G.z2+H ... |  | I J K L |   | z1 z2 y3 y4 z5 |   | I.x1+J.y1+K.z1+L I.x2+J.y2+K.z2+L ... |  | M N O P |   | 1  1  1  1  1  |   | M.x1+N.y1+O.z1+P M.x2+N.y2+O.z2+P ... |  For each vector in the list there will be a total of 12 multiplication  16 addition and 1 division operation (for perspective).  If the matrix is known not to be a rotation or translation matrix then the  division operation can be skipped.    


When I do whats written there I get a four-dimensional vector (may I do something wrong?) and I can do nothing with it.
Well I used it anyways and got the same movementproblem as before without multiplying the vector with the matrix. (damn it)
Here I have some screenshots from the coordinate system when I just get the vectors from the quaternion. They do not look nice (the vectors) But maybe someone can point out the problem shown here.

please press here


Its me, DarkKiller

-------------------------------------------
What is the Matrix?



[edited by - McNugget on May 8, 2003 2:44:20 PM]
Its me, DarkKiller ;)-------------------------------------------What is the Matrix?

This topic is closed to new replies.

Advertisement