-
Notifications
You must be signed in to change notification settings - Fork 9
/
matrix.cpp
152 lines (131 loc) · 4.21 KB
/
matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "matrix.h"
#include <math.h>
namespace F4VRBody {
void Matrix44::getEulerAngles(float* heading, float* roll, float* attitude) {
if (data[2][0] < 1.0) {
if (data[2][0] > -1.0) {
*heading = atan2(-data[2][1], data[2][2]);
*attitude = asin(data[2][0]);
*roll = atan2(-data[1][0], data[0][0]);
}
else {
*heading = -atan2(-data[0][1], data[1][1]);
*attitude = -PI / 2;
*roll = 0.0;
}
}
else {
*heading = atan2(data[0][1], data[1][1]);
*attitude = PI / 2;
*roll = 0.0;
}
}
void Matrix44::setEulerAngles(float x, float y, float z) {
float sinX = sin(x);
float cosX = cos(x);
float sinY = sin(y);
float cosY = cos(y);
float sinZ = sin(z);
float cosZ = cos(z);
data[0][0] = cosY * cosZ;
data[1][0] = sinX * sinY * cosZ + sinZ * cosX;
data[2][0] = sinX * sinZ - cosX * sinY * cosZ;
data[0][1] = -cosY * sinZ;
data[1][1] = cosX * cosZ - sinX * sinY * sinZ;
data[2][1] = cosX * sinY * sinZ + sinX * cosZ;
data[0][2] = sinY;
data[1][2] = -sinX * cosY;
data[2][2] = cosX * cosY;
}
void Matrix44::rotateVectoVec(NiPoint3 toVec, NiPoint3 fromVec) {
toVec = vec3_norm(toVec);
fromVec = vec3_norm(fromVec);
double dotP = vec3_dot(fromVec, toVec);
if (dotP >= 0.99999) {
this->makeIdentity();
return;
}
NiPoint3 crossP = vec3_cross(toVec, fromVec);
crossP = vec3_norm(crossP);
float phi = acosf(dotP);
float rcos = cos(phi);
float rsin = sin(phi);
// Build the matrix
data[0][0] = rcos + crossP.x * crossP.x * (1.0 - rcos);
data[0][1] = -crossP.z * rsin + crossP.x * crossP.y * (1.0 - rcos);
data[0][2] = crossP.y * rsin + crossP.x * crossP.z * (1.0 - rcos);
data[1][0] = crossP.z * rsin + crossP.y * crossP.x * (1.0 - rcos);
data[1][1] = rcos + crossP.y * crossP.y * (1.0 - rcos);
data[1][2] = -crossP.x * rsin + crossP.y * crossP.z * (1.0 - rcos);
data[2][0] = -crossP.y * rsin + crossP.z * crossP.x * (1.0 - rcos);
data[2][1] = crossP.x * rsin + crossP.z * crossP.y * (1.0 - rcos);
data[2][2] = rcos + crossP.z * crossP.z * (1.0 - rcos);
}
NiMatrix43 Matrix44::make43() {
NiMatrix43 ret;
for (auto i = 0; i < 3; i++) {
for (auto j = 0; j < 3; j++) {
ret.data[i][j] = this->data[i][j];
}
}
ret.data[0][3] = 0.0f;
ret.data[1][3] = 0.0f;
ret.data[2][3] = 0.0f;
return ret;
}
NiMatrix43 Matrix44::multiply43Left(NiMatrix43 mat) {
return this->mult(mat, this->make43());
}
NiMatrix43 Matrix44::multiply43Right(NiMatrix43 mat) {
return this->mult(this->make43(), mat);
}
void Matrix44::matrixMultiply(Matrix44* worldMat, Matrix44* retMat, Matrix44* localMat) { // This uses the native transform function that the updateWorld call makes
using func_t = decltype(&Matrix44::matrixMultiply);
RelocAddr<func_t> func(0x1a8d60);
return func(worldMat, retMat, localMat);
}
NiMatrix43 Matrix44::mult(NiMatrix43 left, NiMatrix43 right) {
NiMatrix43 tmp;
// shamelessly taken from skse
tmp.data[0][0] =
right.data[0][0] * left.data[0][0] +
right.data[0][1] * left.data[1][0] +
right.data[0][2] * left.data[2][0];
tmp.data[1][0] =
right.data[1][0] * left.data[0][0] +
right.data[1][1] * left.data[1][0] +
right.data[1][2] * left.data[2][0];
tmp.data[2][0] =
right.data[2][0] * left.data[0][0] +
right.data[2][1] * left.data[1][0] +
right.data[2][2] * left.data[2][0];
tmp.data[0][1] =
right.data[0][0] * left.data[0][1] +
right.data[0][1] * left.data[1][1] +
right.data[0][2] * left.data[2][1];
tmp.data[1][1] =
right.data[1][0] * left.data[0][1] +
right.data[1][1] * left.data[1][1] +
right.data[1][2] * left.data[2][1];
tmp.data[2][1] =
right.data[2][0] * left.data[0][1] +
right.data[2][1] * left.data[1][1] +
right.data[2][2] * left.data[2][1];
tmp.data[0][2] =
right.data[0][0] * left.data[0][2] +
right.data[0][1] * left.data[1][2] +
right.data[0][2] * left.data[2][2];
tmp.data[1][2] =
right.data[1][0] * left.data[0][2] +
right.data[1][1] * left.data[1][2] +
right.data[1][2] * left.data[2][2];
tmp.data[2][2] =
right.data[2][0] * left.data[0][2] +
right.data[2][1] * left.data[1][2] +
right.data[2][2] * left.data[2][2];
tmp.data[0][3] = 0.0f;
tmp.data[1][3] = 0.0f;
tmp.data[2][3] = 0.0f;
return tmp;
}
}