NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
m_mtx.cpp
1#include <types.h>
2#include <nw4r/math.h>
4#include <game/mLib/m_mtx.hpp>
5
6mMtx_c mMtx_c::Identity = mMtx_c(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
7
8mMtx_c::mMtx_c(float _00, float _01, float _02, float _03, float _10, float _11, float _12, float _13, float _20, float _21, float _22, float _23) {
9 m[0][0] = _00;
10 m[0][1] = _01;
11 m[0][2] = _02;
12 m[0][3] = _03;
13 m[1][0] = _10;
14 m[1][1] = _11;
15 m[1][2] = _12;
16 m[1][3] = _13;
17 m[2][0] = _20;
18 m[2][1] = _21;
19 m[2][2] = _22;
20 m[2][3] = _23;
21}
22
23void mMtx_c::XrotS(mAng angle) {
24 float cos = angle.cos();
25 float sin = angle.sin();
26
27 m[0][0] = 1.0f;
28 m[0][1] = 0.0f;
29 m[0][2] = 0.0f;
30 m[0][3] = 0.0f;
31 m[1][0] = 0.0f;
32 m[1][1] = cos;
33 m[1][2] = -sin;
34 m[1][3] = 0.0f;
35 m[2][0] = 0.0f;
36 m[2][1] = sin;
37 m[2][2] = cos;
38 m[2][3] = 0.0f;
39}
40
41void mMtx_c::XrotM(mAng angle) {
42 if ((s16) angle.mAngle != 0) {
43 mMtx_c rotatedMtx = mMtx_c();
44 rotatedMtx.XrotS(angle);
45 PSMTXConcat(*this, rotatedMtx, *this);
46 }
47}
48
49void mMtx_c::YrotS(mAng angle) {
50 float cos = angle.cos();
51 float sin = angle.sin();
52
53 m[0][0] = cos;
54 m[0][1] = 0.0f;
55 m[0][2] = sin;
56 m[0][3] = 0.0f;
57 m[1][0] = 0.0f;
58 m[1][1] = 1.0f;
59 m[1][2] = 0.0f;
60 m[1][3] = 0.0f;
61 m[2][0] = -sin;
62 m[2][1] = 0.0f;
63 m[2][2] = cos;
64 m[2][3] = 0.0f;
65}
66
67void mMtx_c::YrotM(mAng angle) {
68 if ((s16) angle.mAngle != 0) {
69 mMtx_c rotatedMtx = mMtx_c();
70 rotatedMtx.YrotS(angle);
71 PSMTXConcat(*this, rotatedMtx, *this);
72 }
73}
74
75void mMtx_c::ZrotS(mAng angle) {
76 float cos = angle.cos();
77 float sin = angle.sin();
78
79 m[0][0] = cos;
80 m[0][1] = -sin;
81 m[0][2] = 0.0f;
82 m[0][3] = 0.0f;
83 m[1][0] = sin;
84 m[1][1] = cos;
85 m[1][2] = 0.0f;
86 m[1][3] = 0.0f;
87 m[2][0] = 0.0f;
88 m[2][1] = 0.0f;
89 m[2][2] = 1.0f;
90 m[2][3] = 0.0f;
91}
92
93void mMtx_c::ZrotM(mAng angle) {
94 if ((s16) angle.mAngle != 0) {
95 mMtx_c rotatedMtx = mMtx_c();
96 rotatedMtx.ZrotS(angle);
97 PSMTXConcat(*this, rotatedMtx, *this);
98 }
99}
100
101void mMtx_c::ZXYrotM(mAng xRot, mAng yRot, mAng zRot) {
102 YrotM(yRot);
103 XrotM(xRot);
104 ZrotM(zRot);
105}
106
107void mMtx_c::XYZrotM(mAng xRot, mAng yRot, mAng zRot) {
108 ZrotM(zRot);
109 YrotM(yRot);
110 XrotM(xRot);
111}
112
113// [This approach is required for matching].
114inline float calcLengthSq(float x, float y) {
115 x *= x;
116 y *= y;
117 x += y;
118 return x;
119}
120
121void mMtx_c::toRot(mAng3_c &out) const {
122 float cos = nw4r::math::FSqrt(calcLengthSq(m[0][2], m[2][2]));
123
124 short xRot = cM::atan2s(-m[1][2], cos);
125 out.x = xRot;
126 if (xRot == 0x4000 || xRot == -0x4000) {
127 out.z = 0;
128 out.y = cM::atan2s(-m[2][0], m[0][0]);
129 } else {
130 out.y = cM::atan2s(m[0][2], m[2][2]);
131 out.z = cM::atan2s(m[1][0], m[1][1]);
132 }
133}
134
136 out.x = m[0][3];
137 out.y = m[1][3];
138 out.z = m[2][3];
139}
140
142 m[0][0] = 0.0f;
143 m[0][1] = 0.0f;
144 m[0][2] = 0.0f;
145 m[0][3] = 0.0f;
146 m[1][0] = 0.0f;
147 m[1][1] = 0.0f;
148 m[1][2] = 0.0f;
149 m[1][3] = 0.0f;
150 m[2][0] = 0.0f;
151 m[2][1] = 0.0f;
152 m[2][2] = 0.0f;
153 m[2][3] = 0.0f;
154}
A three-dimensional short angle vector.
Definition m_angle.hpp:59
mAng y
The rotation on the Y axis.
Definition m_angle.hpp:109
mAng z
The rotation on the Z axis.
Definition m_angle.hpp:110
mAng x
The rotation on the X axis.
Definition m_angle.hpp:108
A 3x4 matrix.
Definition m_mtx.hpp:9
mMtx_c()
Constructs an empty matrix.
Definition m_mtx.hpp:12
void ZrotS(mAng angle)
Generates a rotation matrix for the Z axis with the given angle.
Definition m_mtx.cpp:75
void XYZrotM(mAng xRot, mAng yRot, mAng zRot)
Rotates the matrix on the Z, Y and X axes by the given angles.
Definition m_mtx.cpp:107
void toRot(mAng3_c &out) const
Extracts the rotation vector from the matrix.
Definition m_mtx.cpp:121
void multVecZero(nw4r::math::VEC3 &out) const
Extracts the translation vector from the matrix.
Definition m_mtx.cpp:135
void ZrotM(mAng angle)
Rotates the matrix on the Z axis by the given angle.
Definition m_mtx.cpp:93
void XrotS(mAng angle)
Generates a rotation matrix for the X axis with the given angle.
Definition m_mtx.cpp:23
void YrotS(mAng angle)
Generates a rotation matrix for the Y axis with the given angle.
Definition m_mtx.cpp:49
void ZXYrotM(mAng xRot, mAng yRot, mAng zRot)
Rotates the matrix on the Y, X and Z axes by the given angles.
Definition m_mtx.cpp:101
static mMtx_c Identity
The identity matrix.
Definition m_mtx.hpp:55
void XrotM(mAng angle)
Rotates the matrix on the X axis by the given angle.
Definition m_mtx.cpp:41
void zero()
Zeroes out the matrix.
Definition m_mtx.cpp:141
void YrotM(mAng angle)
Rotates the matrix on the Y axis by the given angle.
Definition m_mtx.cpp:67
s16 atan2s(float sin, float cos)
Converts a sine and a cosine to an angle in units.
Definition c_math.cpp:167
A one-dimensional short angle vector.
Definition m_angle.hpp:12
float cos() const
Computes the cosine of the angle.
Definition m_angle.hpp:52
float sin() const
Computes the sine of the angle.
Definition m_angle.hpp:49
s16 mAngle
The rotation.
Definition m_angle.hpp:54