Skip to content

Commit

Permalink
TITANIC: Finished DVector class
Browse files Browse the repository at this point in the history
  • Loading branch information
dreammaster committed Mar 24, 2017
1 parent e3a11ba commit c676c53
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 29 deletions.
4 changes: 2 additions & 2 deletions engines/titanic/star_control/dmatrix.cpp
Expand Up @@ -120,9 +120,9 @@ void DMatrix::fn3(CStarControlSub26 *sub26) {
error("TODO: DMatrix::fn3 %d", (int)v);
}

const DMatrix *DMatrix::fn4(DMatrix &dest, const DMatrix &m1, const DMatrix &m2) {
DMatrix DMatrix::fn4(const DMatrix &m) {
// TODO
return nullptr;
return DMatrix();
}

} // End of namespace Titanic
2 changes: 1 addition & 1 deletion engines/titanic/star_control/dmatrix.h
Expand Up @@ -60,7 +60,7 @@ class DMatrix {
void fn1(DMatrix &m);
void fn3(CStarControlSub26 *sub26);

const DMatrix *fn4(DMatrix &dest, const DMatrix &m1, const DMatrix &m2);
DMatrix fn4(const DMatrix &m);
};

} // End of namespace Titanic
Expand Down
68 changes: 57 additions & 11 deletions engines/titanic/star_control/dvector.cpp
Expand Up @@ -21,43 +21,89 @@
*/

#include "titanic/star_control/dvector.h"
#include "titanic/star_control/dmatrix.h"
#include "common/algorithm.h"

namespace Titanic {

void DVector::normalize() {
double DVector::normalize() {
double hyp = sqrt(_x * _x + _y * _y + _z * _z);
assert(hyp);

_x *= 1.0 / hyp;
_y *= 1.0 / hyp;
_z *= 1.0 / hyp;
return hyp;
}

double DVector::getDistance(const DVector &src) {
return sqrt((src._x - _x) * (src._x - _x) + (src._y - _y) * (src._y - _y) + (src._z - _z) * (src._z - _z));
}

DVector *DVector::fn1(DVector &dest, const DMatrix &m) {
// TODO
return nullptr;
dest._x = m._row3._x * _z + m._row2._x * _y + _x * m._row1._x + m._row4._x;
dest._y = m._row2._y * _y + m._row3._y * _z + m._row1._y * _x + m._row4._z;
dest._z = m._row3._z * _z + m._row2._z * _y + m._row1._z * _x + m._row4._y;
return &dest;
}

void DVector::fn2(double val) {
// TODO
const double FACTOR = 2 * M_PI / 360.0;
double sinVal = sin(val * FACTOR);
double cosVal = cos(val * FACTOR);

_x = cosVal * _x - sinVal * _z;
_z = cosVal * _z + sinVal * _x;
}

void DVector::fn3(DVector &dest) {
// TODO
DVector DVector::fn3() const {
DVector vector = *this;
DVector dest;
dest._x = vector.normalize();
dest._z = acos(vector._y);

if (ABS(vector._z) < 0.00001) {
if (vector._x < 0.0) {
dest._y = 2 * M_PI - (M_PI / 2.0);
} else {
dest._y = M_PI / 2.0;
}
} else {
dest._y = atan(vector._x / vector._z);
if (vector._x < 0.0)
dest._y += 2 * M_PI;
}

return dest;
}

const DMatrix *DVector::fn4(const DVector &v, DMatrix &m) {
// TODO
return nullptr;
void DVector::fn4(const DVector &v, DMatrix &m) {
const double FACTOR = 180.0 / M_PI;
DMatrix matrix1, matrix2, matrix3, matrix4;
DMatrix dest;
DVector vector1 = fn3();

matrix1.setRotationMatrix(X_AXIS, vector1._y * FACTOR);
matrix2.setRotationMatrix(Y_AXIS, -(vector1._z * FACTOR));
matrix3 = matrix1.fn4(matrix2);
matrix3.fn1(matrix4);

vector1 = v.fn3();
matrix1.setRotationMatrix(X_AXIS, vector1._y * FACTOR);
matrix2.setRotationMatrix(Y_AXIS, -(vector1._z * FACTOR));
matrix3 = matrix1.fn4(matrix2);
matrix1.fn1(matrix4);

m = matrix4.fn4(matrix3);
}

void DVector::fn5(DMatrix &dest) {
// TODO
DMatrix DVector::fn5() const {
const double FACTOR = 180.0 / M_PI;
DVector v1 = fn3();
DMatrix m1, m2;
m1.setRotationMatrix(X_AXIS, v1._y * FACTOR);
m2.setRotationMatrix(Y_AXIS, -(v1._z * FACTOR));
return m1.fn4(m2);
}

} // End of namespace Titanic
8 changes: 4 additions & 4 deletions engines/titanic/star_control/dvector.h
Expand Up @@ -41,7 +41,7 @@ class DVector {
DVector(double x, double y, double z) : _x(x), _y(y), _z(z) {}
DVector(const FVector &v) : _x(v._x), _y(v._y), _z(v._z) {}

void normalize();
double normalize();

/**
* Returns the distance between this vector and the passed one
Expand All @@ -50,9 +50,9 @@ class DVector {

DVector *fn1(DVector &dest, const DMatrix &m);
void fn2(double val);
void fn3(DVector &dest);
const DMatrix *fn4(const DVector &v, DMatrix &m);
void fn5(DMatrix &dest);
DVector fn3() const;
void fn4(const DVector &v, DMatrix &m);
DMatrix fn5() const;

/**
* Returns true if the passed vector equals this one
Expand Down
14 changes: 5 additions & 9 deletions engines/titanic/star_control/star_control_sub12.cpp
Expand Up @@ -324,12 +324,10 @@ void CStarControlSub12::setViewportPosition(const FPoint &angles) {

tempV1 = _matrix._row2 - _matrix._row1;
diffV = tempV1;
diffV.fn5(m1);
sub.fn4(sub, m1, subX);
m1 = sub;
m1 = diffV.fn5();
m1 = m1.fn4(subX);
m1.fn1(subX);
subX.fn4(m2, subX, subY);
subX = m2;
subX = subX.fn4(subY);

FMatrix m3 = _sub13.getMatrix();
tempV2 = _sub13._position;
Expand Down Expand Up @@ -509,11 +507,9 @@ void CStarControlSub12::fn3(CStarControlSub13 *sub13, const FVector &v) {
DMatrix m2(0, tempV1);

tempV1 = v - _matrix._row1;
tempV1.fn5(m1);
m1 = tempV1.fn5();

DMatrix m3;
const DMatrix *m = m1.fn4(m3, m1, m2);
m1 = *m;
m1 = m1.fn4(m2);
m1.fn1(m2);

DVector tempV2 = _sub13._position;
Expand Down
3 changes: 1 addition & 2 deletions engines/titanic/star_control/star_control_sub21.cpp
Expand Up @@ -46,9 +46,8 @@ void CStarControlSub21::proc10(const FVector &v1, const FVector &v2, const FVect
DVector vector2 = v2;
DMatrix matrix1, matrix2 = m, matrix3;
vector2.fn4(vector1, matrix1);
const DMatrix *matrixP = matrix1.fn4(matrix3, matrix1, matrix2);
FMatrix matrix4 = matrix1.fn4(matrix2);

FMatrix matrix4 = *matrixP;
_sub24.proc3(m, matrix4);
incLockCount();
}
Expand Down

0 comments on commit c676c53

Please sign in to comment.