• ### Popular Now

• 15
• 15
• 11
• 9
• 10

#### Archived

This topic is now archived and is closed to further replies.

# Quaternion math

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

## Recommended Posts

I have been playing with quaternions a lot lately and finally feel like I am understanding what is going on. However there are some things I haven''t seen explained anywhere, I''m hoping someone can point me to some references. There are a lot of tutorials on vectors, and all the math that can be done with them for things like game physics. However I haven''t been able to find much for quaternions except the basics (simple rotations and translation between quaternions and matrices). For example, lets say you have an object in your game, and its heading and rotation is represented by a quaternion. Then lets say a collision occurs, applying a different rotational force on the object. How do you blend these two rotational forces together accurately, using quaternions? There are others, like performing a slerp (or something like it) using radians for measurement, rather than percentage values between start and finish (i.e. rotating a gun turret, where the rotational speed is determined by the turret''s weight). Is this even possible? If someone has any ideas or tips that would point me in the right direction, I''d really appreciate it!

##### Share on other sites
Okay, the best thing I can tell you is this ..

A Quaternion is NOT a vector, it is a representation of either a matrix or an axis and angle. You CAN interpolate between Quaternions, but Not matrices, and that is why they are so useful in 3d programming.

Google for "Quaternion Math" or simply "Quaternion" and you will get more than enough information.

##### Share on other sites
I could give you my code, if you want, but that would be defeating the purpose of teaching.

##### Share on other sites
Right, I dislike using other peoples code since part of the purpose for me is learning how it all works. Though in a crunch, code can sometimes be used to discover how something works. I''ve been searching Google up until now, but I guess I''ll keep plugging away at that.

I understand that a quaternion is not a vector, and can''t be directly manipulated like one, but given that it can represent an axis, or heading, I would think there has to be some set of operations you can perform on them to achieve comperable results to vectors, other than slerping. I''ve already learned how to build a quaternion using a set of world angles, so I''m just trying to continue that search and find a way to manipulate the quaternions.

##### Share on other sites
I will give you the interpolation code in my class

DCQuaternion DCSDK::DCMath::DCQuaternion::InterpolateSlerp (const DCQuaternion& Start, const DCQuaternion& end, float Time){	DCQuaternion  Result;	DCQuaternion  End = end;	    //determine if the angle is cw	float         Cos = Start.Quat.x * End.Quat.x + Start.Quat.y * End.Quat.y + 		                Start.Quat.z * End.Quat.z + Start.Quat.w * End.Quat.w;	//if not, reverse it	if(Cos < 0.0f)	{        Cos        = -Cos;		End.Quat.x = -End.Quat.x;		End.Quat.y = -End.Quat.y;		End.Quat.z = -End.Quat.z;		End.Quat.w = -End.Quat.w;	}	float ACos = 0.0f;	float Sin  = 0.0f;	float T1   = 1.0f-Time;	float T2   = Time;	//if the Quats are very close, keep Time 1 and Time 2 where they are	if(Cos>EPSILON)	{		//get angle for the sphere inter		ACos = acosf(Cos);		//find the sin of the angle		Sin  = sinf(ACos);		//calculate time 1 and time 2		T1   = sinf((1.0f-Time)*ACos)/Sin;		T2   = sinf(Time*ACos)/Sin;	}	//Interpolate	Result.Quat.x = T1*Start.Quat.x+T2*End.Quat.x;	Result.Quat.y = T1*Start.Quat.y+T2*End.Quat.y;	Result.Quat.z = T1*Start.Quat.z+T2*End.Quat.z;	Result.Quat.w = T1*Start.Quat.w+T2*End.Quat.w;	Normalize(Result.Quat);	return Result;     }//--------------------------------------------------------------------------------------------------------------DCQuaternion DCSDK::DCMath::DCQuaternion::InterpolateLinear (const DCQuaternion& Start, const DCQuaternion& end, float Time){	DCQuaternion  Result;	DCQuaternion  End = end;	//determine if the angle is cw	float         Cos = Start.Quat.x * End.Quat.x + Start.Quat.y * End.Quat.y + 		                Start.Quat.z * End.Quat.z + Start.Quat.w * End.Quat.w;	//if not, reverse the order	if(Cos < 0.0f)	{		End.Quat.x = -End.Quat.x;		End.Quat.y = -End.Quat.y;		End.Quat.z = -End.Quat.z;		End.Quat.w = -End.Quat.w;	}    //interpolate  linear	float T1      = 1.0f-Time;	float T2      = Time;	Result.Quat.x = T1*Start.Quat.x+T2*End.Quat.x;	Result.Quat.y = T1*Start.Quat.y+T2*End.Quat.y;	Result.Quat.z = T1*Start.Quat.z+T2*End.Quat.z;	Result.Quat.w = T1*Start.Quat.w+T2*End.Quat.w;	Normalize(Result.Quat);	return Result;}

but this is part of the code of the Dr-code Engine

Dr-Code

EDIT: I would be glad to share the whole source if you want

ICQ 171346210

[edited by - RhoneRanger on September 22, 2003 12:02:34 AM]