From ba128368e8e7c4c4369db48c0f024bd40e8dad80 Mon Sep 17 00:00:00 2001 From: David Fioramonti Date: Sat, 12 Aug 2017 19:49:14 -0700 Subject: TITANIC: star control dvector work, replace fn3 with getAnglesAsVect also replace atan2 implementation fn3 in dvector returns a vector that stores a magnitude, and 2 angles. The second angle (the z component of the returned vector) was the angle that the internal vector was between its z and x axis. This angle was obtained by doing a poor man 4-quadrant atan implementation and it gave large values for negative x. This has been replaced with the atan2 standard function. --- engines/titanic/star_control/dvector.cpp | 48 +++++++++++++------------------- engines/titanic/star_control/dvector.h | 8 +++++- 2 files changed, 26 insertions(+), 30 deletions(-) (limited to 'engines/titanic/star_control') diff --git a/engines/titanic/star_control/dvector.cpp b/engines/titanic/star_control/dvector.cpp index 728c94e5f0..86396c9945 100644 --- a/engines/titanic/star_control/dvector.cpp +++ b/engines/titanic/star_control/dvector.cpp @@ -26,6 +26,9 @@ namespace Titanic { +const double Rad2Deg = 180.0 / M_PI; +const double Deg2Rad = 1.0 / Rad2Deg; + double DVector::normalize() { double hyp = sqrt(_x * _x + _y * _y + _z * _z); assert(hyp); @@ -61,9 +64,8 @@ DVector DVector::DAffMatrixProdVec(const DAffine &m) { } void DVector::RotVectAxisY(double angle_deg) { - const double FACTOR = 2 * M_PI / 360.0; - double sinVal = sin(angle_deg * FACTOR); - double cosVal = cos(angle_deg * FACTOR); + double sinVal = sin(angle_deg * Deg2Rad); + double cosVal = cos(angle_deg * Deg2Rad); double x = cosVal * _x - sinVal * _z; double z = cosVal * _z + sinVal * _x; @@ -71,51 +73,39 @@ void DVector::RotVectAxisY(double angle_deg) { _z = z; } -DVector DVector::fn3() const { +DVector DVector::getAnglesAsVect() const { DVector vector = *this; DVector dest; - dest._x = vector.normalize(); - dest._y = acos(vector._y); - - if (ABS(vector._z) < 0.00001) { - if (vector._x < 0.0) { - dest._z = 2 * M_PI - (M_PI / 2.0); - } else { - dest._z = M_PI / 2.0; - } - } else { - dest._z = atan(vector._x / vector._z); - if (vector._x < 0.0) - dest._z += 2 * M_PI; - } + dest._x = vector.normalize(); // scale that makes this vector have magnitude=1, also does the scaling + dest._y = acos(vector._y); // radian distance/angle that this vector's y component is from the +y axis, + // result is restricted to [0,pi] + dest._z = atan2(vector._x,vector._z); // result is restricted to [-pi,pi] return dest; } DAffine DVector::fn4(const DVector &v) { - const double FACTOR = 180.0 / M_PI; DAffine matrix1, matrix2, matrix3, matrix4; - DVector vector1 = fn3(); - matrix1.setRotationMatrix(X_AXIS, vector1._y * FACTOR); - matrix2.setRotationMatrix(Y_AXIS, -(vector1._z * FACTOR)); + DVector vector1 = getAnglesAsVect(); + matrix1.setRotationMatrix(X_AXIS, vector1._y * Rad2Deg); + matrix2.setRotationMatrix(Y_AXIS, -(vector1._z * Rad2Deg)); matrix3 = matrix1.compose(matrix2); matrix4 = matrix3.inverseTransform(); - vector1 = v.fn3(); - matrix1.setRotationMatrix(X_AXIS, vector1._y * FACTOR); - matrix2.setRotationMatrix(Y_AXIS, -(vector1._z * FACTOR)); + vector1 = v.getAnglesAsVect(); + matrix1.setRotationMatrix(X_AXIS, vector1._y * Rad2Deg); + matrix2.setRotationMatrix(Y_AXIS, -(vector1._z * Rad2Deg)); matrix3 = matrix1.compose(matrix2); return matrix4.compose(matrix3); } DAffine DVector::fn5() const { - const double FACTOR = 180.0 / M_PI; - DVector v1 = fn3(); + DVector v1 = getAnglesAsVect(); DAffine m1, m2; - m1.setRotationMatrix(X_AXIS, v1._y * FACTOR); - m2.setRotationMatrix(Y_AXIS, -(v1._z * FACTOR)); + m1.setRotationMatrix(X_AXIS, v1._y * Rad2Deg); + m2.setRotationMatrix(Y_AXIS, -(v1._z * Rad2Deg)); return m1.compose(m2); } diff --git a/engines/titanic/star_control/dvector.h b/engines/titanic/star_control/dvector.h index 7daeda71aa..b37c8bb851 100644 --- a/engines/titanic/star_control/dvector.h +++ b/engines/titanic/star_control/dvector.h @@ -60,7 +60,13 @@ public: */ void RotVectAxisY(double angle_deg); - DVector fn3() const; + /** + * Returns a vector, v, that represents a magnitude, and two angles in radians + * 1. Scale this vector to be unit magnitude and store scale in x component of v + * 2. X rotation angle from +y axis of this vector is put in y component of v + * 3. z component output of v is the 4-quadrant angle that z makes with x (Y axis rotation) + */ + DVector getAnglesAsVect() const; DAffine fn4(const DVector &v); DAffine fn5() const; -- cgit v1.2.3