From: <axl...@us...> - 2009-06-04 00:11:32
|
Revision: 302 http://hgengine.svn.sourceforge.net/hgengine/?rev=302&view=rev Author: axlecrusher Date: 2009-06-04 00:11:19 +0000 (Thu, 04 Jun 2009) Log Message: ----------- updates, trying to fix broken rotations Modified Paths: -------------- Mercury2/src/Camera.cpp Mercury2/src/MQuaternion.cpp Mercury2/src/MQuaternion.h Mercury2/src/MercuryMatrix.cpp Mercury2/src/TransformNode.cpp Modified: Mercury2/src/Camera.cpp =================================================================== --- Mercury2/src/Camera.cpp 2009-06-03 01:22:25 UTC (rev 301) +++ Mercury2/src/Camera.cpp 2009-06-04 00:11:19 UTC (rev 302) @@ -26,7 +26,7 @@ m_lookAt.NormalizeSelf(); LOOKAT = m_lookAt; - r[MQuaternion::W] *= -1; //reverse angle for camera + r.W() *= -1; //reverse angle for camera //rotate then translate since we are a camera r.toMatrix4( local ); Modified: Mercury2/src/MQuaternion.cpp =================================================================== --- Mercury2/src/MQuaternion.cpp 2009-06-03 01:22:25 UTC (rev 301) +++ Mercury2/src/MQuaternion.cpp 2009-06-04 00:11:19 UTC (rev 302) @@ -61,11 +61,15 @@ void MQuaternion::FromAxisAngle(const MercuryVertex& p, const float radians) { + MercuryVertex v( p.Normalize() ); + float sn = SIN(radians/2.0f); m_wxyz[0] = COS(radians/2.0f); m_wxyz[1] = sn * p[0]; m_wxyz[2] = sn * p[1]; m_wxyz[3] = sn * p[2]; + + *this = this->normalize(); } void MQuaternion::ToAxisAngle(float& angle, float& x, float& y, float& z) const @@ -115,28 +119,33 @@ //Converts MQuaternion to 4x4 Matrix(3x3 Spatial) void MQuaternion::toMatrix( MercuryMatrix &matrix ) const { - float X = 2*m_wxyz[1]*m_wxyz[1]; //Reduced calulation for speed - float Y = 2*m_wxyz[2]*m_wxyz[2]; - float Z = 2*m_wxyz[3]*m_wxyz[3]; - float a = 2*m_wxyz[0]*m_wxyz[1]; - float b = 2*m_wxyz[0]*m_wxyz[2]; - float c = 2*m_wxyz[0]*m_wxyz[3]; - float d = 2*m_wxyz[1]*m_wxyz[2]; - float e = 2*m_wxyz[1]*m_wxyz[3]; - float f = 2*m_wxyz[2]*m_wxyz[3]; + MQuaternion q( this->normalize() ); + + //Reduced calulation for speed + float xx = 2*q.X()*q.X(); + float xy = 2*q.X()*q.Y(); + float xz = 2*q.X()*q.Z(); + float xw = 2*q.X()*q.W(); + + float yy = 2*q.Y()*q.Y(); + float yz = 2*q.Y()*q.Z(); + float yw = 2*q.Y()*q.W(); + + float zz = 2*q.Z()*q.Z(); + float zw = 2*q.Z()*q.W(); //row major - matrix[0][0] = 1-Y-Z; - matrix[0][1] = d-c; - matrix[0][2] = e+b; + matrix[0][0] = 1-yy-zz; + matrix[0][1] = xy-zw; + matrix[0][2] = xz+yw; - matrix[1][0] = d+c; - matrix[1][1] = 1-X-Z; - matrix[1][2] = f-a; + matrix[1][0] = xy+zw; + matrix[1][1] = 1-xx-zz; + matrix[1][2] = yz-xw; - matrix[2][0] = e-b; - matrix[2][1] = f+a; - matrix[2][2] = 1-X-Y; + matrix[2][0] = xz-yw; + matrix[2][1] = yz+xw; + matrix[2][2] = 1-xx-yy; } void MQuaternion::toMatrix4( MercuryMatrix &matrix ) const { Modified: Mercury2/src/MQuaternion.h =================================================================== --- Mercury2/src/MQuaternion.h 2009-06-03 01:22:25 UTC (rev 301) +++ Mercury2/src/MQuaternion.h 2009-06-04 00:11:19 UTC (rev 302) @@ -9,7 +9,7 @@ ///Mathematical Quaternion (Used for Rotation) class MQuaternion { public: - enum WXYZ { W = 0, X, Y, Z }; + enum WXYZ { QW = 0, QX, QY, QZ }; //Defines a Quaternion such that q = w + xi + yj + zk MQuaternion(); @@ -67,6 +67,11 @@ void Print(const MString& s = "MQuaternion") const; + inline float& W() { return m_wxyz[0]; } + inline float& X() { return m_wxyz[1]; } + inline float& Y() { return m_wxyz[2]; } + inline float& Z() { return m_wxyz[3]; } + // private: FloatRow m_wxyz; } M_ALIGN(32); Modified: Mercury2/src/MercuryMatrix.cpp =================================================================== --- Mercury2/src/MercuryMatrix.cpp 2009-06-03 01:22:25 UTC (rev 301) +++ Mercury2/src/MercuryMatrix.cpp 2009-06-04 00:11:19 UTC (rev 302) @@ -123,7 +123,7 @@ void MercuryMatrix::Rotate(const MQuaternion& q) { MercuryMatrix m; - q.normalize().toMatrix4( m ); + q.toMatrix4( m ); *this *= m; } Modified: Mercury2/src/TransformNode.cpp =================================================================== --- Mercury2/src/TransformNode.cpp 2009-06-03 01:22:25 UTC (rev 301) +++ Mercury2/src/TransformNode.cpp 2009-06-04 00:11:19 UTC (rev 302) @@ -104,13 +104,13 @@ //only change the values that exist in the XML if ( !node.Attribute("rotx").empty() ) - rot *= MQuaternion::CreateFromAxisAngle(MercuryVector(1,0,0), StrToFloat( node.Attribute("rotx") )); + rot *= MQuaternion::CreateFromAxisAngle(MercuryVector(1,0,0), StrToFloat( node.Attribute("rotx") )*DEGRAD ); if ( !node.Attribute("roty").empty() ) - rot *= MQuaternion::CreateFromAxisAngle(MercuryVector(0,1,0), StrToFloat( node.Attribute("roty") )); + rot *= MQuaternion::CreateFromAxisAngle(MercuryVector(0,1,0), StrToFloat( node.Attribute("roty") )*DEGRAD ); if ( !node.Attribute("rotz").empty() ) - rot *= MQuaternion::CreateFromAxisAngle(MercuryVector(0,0,1), StrToFloat( node.Attribute("rotz") )); + rot *= MQuaternion::CreateFromAxisAngle(MercuryVector(0,0,1), StrToFloat( node.Attribute("rotz") )*DEGRAD ); if ( !node.Attribute("scalex").empty() ) scale.SetX( StrToFloat( node.Attribute("scalex") ) ); @@ -157,8 +157,8 @@ void RotatorNode::Update(float dTime) { MQuaternion r = GetRotation(); - r[MQuaternion::X] += (dTime)*25; - r[MQuaternion::Y] += (dTime)*75; + r.X() += (dTime)*25; + r.Y() += (dTime)*75; SetRotation( r ); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |