00001 #include <windows.h>
00002
00003
00004 #include "quat.h"
00005 #include "vector.h"
00006
00007 QUAT::QUAT(float sx, float sy, float sz, float sw)
00008 :
00009 x(sx),
00010 y(sy),
00011 z(sz),
00012 w(sw)
00013 {
00014 }
00015
00016 QUAT::~QUAT()
00017 {
00018 }
00019
00020 void QUAT::Set()
00021 {
00022 x = 0.0;
00023 y = 0.0;
00024 z = 0.0;
00025 w = 1.0;
00026 }
00027
00028 void QUAT::CopyQuat(QUAT q)
00029 {
00030 x = q.x;
00031 y = q.y;
00032 z = q.z;
00033 w = q.w;
00034 }
00035
00036 void QUAT::AxisAngleToQuat(VECTOR axis, float theta)
00037 {
00038 float halfTheta = theta * 0.5;
00039 float cosHalfTheta = cos(halfTheta);
00040 float sinHalfTheta = sin(halfTheta);
00041 x = axis.x * sinHalfTheta;
00042 y = axis.y * sinHalfTheta;
00043 z = axis.z * sinHalfTheta;
00044 w = cosHalfTheta;
00045 }
00046
00047 void QUAT::EulerToQuat(float roll, float pitch, float yaw)
00048 {
00049 float cr, cp, cy, sr, sp, sy, cpcy, spsy;
00050 cr = cos(roll/2);
00051 cp = cos(pitch/2);
00052 cy = cos(yaw/2);
00053 sr = sin(roll/2);
00054 sp = sin(pitch/2);
00055 sy = sin(yaw/2);
00056 cpcy = cp * cy;
00057 spsy = sp * sy;
00058 w = cr * cpcy + sr * spsy;
00059 x = sr * cpcy - cr * spsy;
00060 y = cr * sp * cy + sr * cp * sy;
00061 z = cr * cp * sy - sr * sp * cy;
00062 }
00063
00064 float QUAT::MagnitudeQuat()
00065 {
00066 return( sqrt(w*w+x*x+y*y+z*z));
00067 }
00068
00069 void QUAT::NormaliseQuat()
00070 {
00071 float Mag;
00072 Mag = MagnitudeQuat();
00073 w = w/Mag;
00074 x = x/Mag;
00075 y = y/Mag;
00076 z = z/Mag;
00077 }
00078
00079 void QUAT::MultQuat(QUAT q)
00080 {
00081 QUAT q3;
00082 VECTOR vectorq1;
00083 VECTOR vectorq2;
00084 vectorq1.x = x;
00085 vectorq1.y = y;
00086 vectorq1.z = z;
00087 vectorq2.x = q.x;
00088 vectorq2.y = q.y;
00089 vectorq2.z = q.z;
00090
00091 VECTOR tempvec1;
00092 VECTOR tempvec2;
00093 VECTOR tempvec3;
00094 tempvec1 = vectorq1;
00095 q3.w = (w*q.w) - tempvec1.DotProduct(vectorq2);
00096 tempvec1.CrossProduct(vectorq2);
00097 tempvec2.x = w * q.x;
00098 tempvec2.y = w * q.y;
00099 tempvec2.z = w * q.z;
00100 tempvec3.x = q.w * x;
00101 tempvec3.y = q.w * y;
00102 tempvec3.z = q.w * z;
00103 q3.x = tempvec1.x + tempvec2.x + tempvec3.x;
00104 q3.y = tempvec1.y + tempvec2.y + tempvec3.y;
00105 q3.z = tempvec1.z + tempvec2.z + tempvec3.z;
00106 CopyQuat(q3);
00107 }