Explorar el Código

Fixed angle conversion issues

Fixed a variable name and method that should be const.
Also fixed several angle conversion functions that didn't convert the
angle correct.
Nathan Bowhay hace 10 años
padre
commit
ad267f0505
Se han modificado 3 ficheros con 45 adiciones y 30 borrados
  1. 1 1
      Engine/source/math/mMatrix.cpp
  2. 43 28
      Engine/source/math/mQuat.cpp
  3. 1 1
      Engine/source/math/mQuat.h

+ 1 - 1
Engine/source/math/mMatrix.cpp

@@ -163,7 +163,7 @@ EulerF MatrixF::toEuler() const
    const F32 * mat = m;
    const F32 * mat = m;
 
 
    EulerF r;
    EulerF r;
-   r.x = mAsin(mat[MatrixF::idx(2,1)]);
+   r.x = mAsin(mClampF(mat[MatrixF::idx(2,1)], -1.0, 1.0));
 
 
    if(mCos(r.x) != 0.f)
    if(mCos(r.x) != 0.f)
    {
    {

+ 43 - 28
Engine/source/math/mQuat.cpp

@@ -32,33 +32,48 @@ const QuatF QuatF::Identity(0.0f,0.0f,0.0f,1.0f);
 
 
 QuatF& QuatF::set( const EulerF & e )
 QuatF& QuatF::set( const EulerF & e )
 {
 {
-   F32 cx, sx;
-   F32 cy, sy;
-   F32 cz, sz;
-   mSinCos( e.x * 0.5f, sx, cx );
-   mSinCos( e.y * 0.5f, sy, cy );
-   mSinCos( e.z * 0.5f, sz, cz );
-
-   // Qyaw(z) = [ (0, 0, sin z/2), cos z/2 ]
-   // Qpitch(x) = [ (sin x/2, 0, 0), cos x/2 ]
-   // Qroll(y) = [ (0, sin y/2, 0), cos y/2 ]
-   // this = Qresult = Qyaw*Qpitch*Qroll ZXY
-   //
-   // The code that folows is a simplification of:
-   // roll*=pitch;
-   // roll*=yaw;
-   // *this = roll;
-   F32 cycz, sysz, sycz, cysz;
-   cycz = cy*cz;
-   sysz = sy*sz;
-   sycz = sy*cz;
-   cysz = cy*sz;
-   w = cycz*cx - sysz*sx;
-   x = cycz*sx + sysz*cx;
-   y = sycz*cx + cysz*sx;
-   z = cysz*cx - sycz*sx;
-
-   return *this;
+	/*
+	F32 cx, sx;
+	F32 cy, sy;
+	F32 cz, sz;
+	mSinCos( -e.x * 0.5f, sx, cx );
+	mSinCos( -e.y * 0.5f, sy, cy );
+	mSinCos( -e.z * 0.5f, sz, cz );
+
+	// Qyaw(z)   = [ (0, 0, sin z/2), cos z/2 ]
+	// Qpitch(x) = [ (sin x/2, 0, 0), cos x/2 ]
+	// Qroll(y)  = [ (0, sin y/2, 0), cos y/2 ]
+	// this = Qresult = Qyaw*Qpitch*Qroll  ZXY
+	//
+	// The code that folows is a simplification of:
+	//    roll*=pitch;
+	//    roll*=yaw;
+	//    *this = roll;
+	F32 cycz, sysz, sycz, cysz;
+	cycz = cy*cz;
+	sysz = sy*sz;
+	sycz = sy*cz;
+	cysz = cy*sz;
+	w = cycz*cx + sysz*sx;
+	x = cycz*sx + sysz*cx;
+	y = sycz*cx - cysz*sx;
+	z = cysz*cx - sycz*sx;
+	*/
+	// Assuming the angles are in radians.
+	F32 c1 = mCos(e.y/2.0f);
+	F32 s1 = mSin(e.y/2.0f);
+	F32 c2 = mCos(e.z/2.0f);
+	F32 s2 = mSin(e.z/2.0f);
+	F32 c3 = mCos(e.x/2.0f);
+	F32 s3 = mSin(e.x/2.0f);
+	F32 c1c2 = c1*c2;
+	F32 s1s2 = s1*s2;
+	w =c1c2*c3 - s1s2*s3;
+	x =c1c2*s3 + s1s2*c3;
+	y =s1*c2*c3 + c1*s2*s3;
+	z =c1*s2*c3 - s1*c2*s3;
+
+	return *this;
 }
 }
 
 
 QuatF& QuatF::operator *=( const QuatF & b )
 QuatF& QuatF::operator *=( const QuatF & b )
@@ -289,7 +304,7 @@ QuatF & QuatF::interpolate( const QuatF & q1, const QuatF & q2, F32 t )
    return *this;
    return *this;
 }
 }
 
 
-Point3F & QuatF::mulP(const Point3F& p, Point3F* r)
+Point3F & QuatF::mulP(const Point3F& p, Point3F* r) const
 {
 {
    QuatF qq;
    QuatF qq;
    QuatF qi = *this;
    QuatF qi = *this;

+ 1 - 1
Engine/source/math/mQuat.h

@@ -81,7 +81,7 @@ public:
    QuatF& interpolate( const QuatF & q1, const QuatF & q2, F32 t );
    QuatF& interpolate( const QuatF & q1, const QuatF & q2, F32 t );
    F32  angleBetween( const QuatF & q );
    F32  angleBetween( const QuatF & q );
 
 
-   Point3F& mulP(const Point3F& a, Point3F* b);   // r = p * this
+   Point3F& mulP(const Point3F& a, Point3F* r) const;   // r = p * this
    QuatF& mul(const QuatF& a, const QuatF& b);    // This = a * b
    QuatF& mul(const QuatF& a, const QuatF& b);    // This = a * b
 
 
    // Vectors passed in must be normalized
    // Vectors passed in must be normalized