NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
m_vec.hpp
1#pragma once
2#include <nw4r/math.h>
3#include <game/mLib/m_angle.hpp>
4#include <lib/egg/math/eggVector.h>
6
7/// A plain-old-data structure version of mVec2_c.
8/// @unofficial
9struct mVec2_POD_c {
10 void set(float fx, float fy) {
11 x = fx;
12 y = fy;
13 }
14 void set(const mVec2_POD_c &v) {
15 set(v.x, v.y);
16 }
17 void setX(float fx) { x = fx; }
18 void setY(float fy) { y = fy; }
19
20 float x;
21 float y;
22};
23
24/// @brief A two-dimensional floating point vector.
25/// @ingroup mlib
26class mVec2_c : public EGG::Vector2f {
27public:
28
29 /// @brief Constructs an empty vector.
31
32 ~mVec2_c() {}
33
34 /// @brief Constructs a vector from a float array.
35 mVec2_c(const f32 *p) { x = p[0]; y = p[1]; }
36
37 /// @brief Constructs a vector from two floating point values.
38 mVec2_c(f32 fx, f32 fy) { set(fx, fy); }
39
40 /// @brief Copy constructor.
41 mVec2_c(const mVec2_c &v) { set(v.x, v.y); }
42 mVec2_c(const nw4r::math::VEC2 &v) { set(v.x, v.y); }
43
44 void set(float x, float y) {
45 this->x = x;
46 this->y = y;
47 }
48
49 /// @brief Increments the X coordinate.
50 /// @param x The value to increment by.
51 void incX(float x) { this->x += x; }
52
53 /// @brief Increments the Y coordinate.
54 /// @param y The value to increment by.
55 void incY(float y) { this->y += y; }
56
57 // mVec2_c &operator=(const mVec2_c &v) { set(v.x, v.y); return *this; }
58 mVec2_c &operator=(const mVec2_c &v) { x = v.x; y = v.y; return *this; }
59
60 /// @brief Float cast operator.
61 operator f32*() { return &x; }
62
63 /// @brief Const float cast operator.
64 operator const f32*() const { return &x; }
65
66 /// @brief Vec2 cast operator.
67 operator Vec2*() { return (Vec2*)&x; }
68
69 /// @brief Const Vec2 cast operator.
70 operator const Vec2*() const { return (const Vec2*)&x; }
71
72 /// @brief nw4r::math::VEC2 cast operator.
73 operator nw4r::math::VEC2*() { return (nw4r::math::VEC2*)&x; }
74
75 /// @brief Const nw4r::math::VEC2 cast operator.
76 operator const nw4r::math::VEC2*() const { return (const nw4r::math::VEC2*)&x; }
77
78 /// @brief Augmented addition operator.
79 mVec2_c &operator+=(const mVec2_c &v) { x += v.x; y += v.y; return *this; }
80
81 /// @brief Augmented subtraction operator.
82 mVec2_c &operator-=(const mVec2_c &v) { x -= v.x; y -= v.y; return *this; }
83
84 /// @brief Augmented scalar product operator.
85 mVec2_c &operator*=(f32 f) { x *= f; y *= f; return *this; }
86
87 /// @brief Augmented scalar division operator.
88 mVec2_c &operator/=(f32 f) { return operator*=(1.0f / f); }
89
90 /// @brief Positive operator.
91 mVec2_c operator+() const { return *this; }
92
93 /// @brief Negative operator.
94 mVec2_c operator-() const { return mVec2_c(-x, -y); }
95
96 /// @brief Addition operator.
97 mVec2_c operator+(const mVec2_c &v) const { return mVec2_c(x + v.x, y + v.y); }
98
99 /// @brief Subtraction operator.
100 mVec2_c operator-(const mVec2_c &v) const { return mVec2_c(x - v.x, y - v.y); }
101
102 /// @brief Scalar product operator.
103 mVec2_c operator*(f32 f) const { return mVec2_c(f * x, f * y); }
104
105 /// @brief Scalar division operator.
106 mVec2_c operator/(f32 f) const { f32 r = 1.0f / f; return operator*(r); }
107
108 /// @brief Equality operator.
109 bool operator==(const mVec2_c &v) const { return x == v.x && y == v.y; }
110
111 /// @brief Inequality operator.
112 bool operator!=(const mVec2_c &v) const { return x != v.x || y != v.y; }
113};
114
115inline mVec2_c operator*(float f, const mVec2_c &v) {
116 return mVec2_c(f * v.x, f * v.y);
117}
118
119/// @brief A three-dimensional floating point vector.
120/// @ingroup mlib
121/// @todo Add EGG::vector3f operators.
122class mVec3_c : public EGG::Vector3f {
123public:
124
125 /// @brief Constructs an empty vector.
127
128 ~mVec3_c() {}
129
130 /// @brief Constructs a vector from a float array.
131 mVec3_c(const f32 *p) { x = p[0]; y = p[1]; z = p[2]; }
132
133 /// @brief Constructs a vector from three floating point values.
134 mVec3_c(f32 fx, f32 fy, f32 fz) { x = fx; y = fy; z = fz; }
135
136 /// @brief Constructs a new vector from an existing vector from the MTX library.
137 mVec3_c(const Vec &v) { x = v.x; y = v.y; z = v.z; }
138
139 /// @brief Copy constructor.
140 mVec3_c(const mVec3_c &v) { set(v.x, v.y, v.z); }
141
142 /// @brief Constructs a new vector from an existing vector from the NW4R library.
143 mVec3_c(const nw4r::math::VEC3 &v) { set(v.x, v.y, v.z); }
144
145 /// @brief Copy constructor with a different Z value.
146 mVec3_c(const mVec3_c &v, float fz) { x = v.x; y = v.y; z = fz; }
147 mVec3_c(const mVec2_c &v, float fz) { x = v.x; y = v.y; z = fz; }
148
149 /// @brief Assignment operator.
150 mVec3_c &operator=(const mVec3_c &v) { x = v.x; y = v.y; z = v.z; return *this; }
151 mVec3_c &operator=(const nw4r::math::VEC3 &v) { x = v.x; y = v.y; z = v.z; return *this; }
152
153 /// @brief Float cast operator.
154 operator f32*() { return &x; }
155
156 /// @brief Const float cast operator.
157 operator const f32*() const { return &x; }
158
159 /// @brief Vec cast operator.
160 operator Vec*() { return (Vec*)&x; }
161
162 /// @brief Const Vec cast operator.
163 operator const Vec*() const { return (const Vec*)&x; }
164
165 /// @brief nw4r::math::VEC3 cast operator.
166 operator nw4r::math::VEC3*() { return (nw4r::math::VEC3*)&x; }
167
168 /// @brief Const nw4r::math::VEC3 cast operator.
169 operator const nw4r::math::VEC3*() const { return (const nw4r::math::VEC3*)&x; }
170
171 /// @brief Augmented addition operator.
172 mVec3_c &operator+=(const mVec3_c &v) { x += v.x; y += v.y; z += v.z; return *this; }
173
174 /// @brief Augmented subtraction operator.
175 mVec3_c &operator-=(const mVec3_c &v) { x -= v.x; y -= v.y; z -= v.z; return *this; }
176
177 /// @brief Augmented scalar product operator.
178 mVec3_c &operator*=(f32 f) { x *= f; y *= f; z *= f; return *this; }
179
180 /// @brief Augmented scalar division operator.
181 mVec3_c &operator/=(f32 f) { return operator*=(1.0f / f); }
182
183 /// @brief Positive operator.
184 mVec3_c operator+() const { return *this; }
185
186 /// @brief Negative operator.
187 mVec3_c operator-() const { return mVec3_c(-x, -y, -z); }
188
189 /// @brief Addition operator.
190 mVec3_c operator+(const mVec3_c &v) const { return mVec3_c(x + v.x, y + v.y, z + v.z); }
191
192 /// @brief Subtraction operator.
193 mVec3_c operator-(const mVec3_c &v) const { return mVec3_c(x - v.x, y - v.y, z - v.z); }
194
195 /// @brief Scalar product operator.
196 mVec3_c operator*(f32 f) const { return mVec3_c(f * x, f * y, f * z); }
197
198 /// @brief Scalar division operator.
199 mVec3_c operator/(f32 f) const { f32 r = 1.0f / f; return mVec3_c(x * r, y * r, z * r); }
200
201 /// @brief Equality operator.
202 bool operator==(const mVec3_c &v) const { return x == v.x && y == v.y && z == v.z; }
203
204 /// @brief Inequality operator.
205 bool operator!=(const mVec3_c &v) const { return x != v.x || y != v.y || z != v.z; }
206
207 float xzLen() const {
208 return EGG::Mathf::sqrt(x * x + z * z);
209 }
210
211 short xzAng() const {
212 return cM::atan2s(x, z);
213 }
214
215 float distTo(const mVec3_c &other) const {
216 return EGG::Mathf::sqrt(PSVECSquareDistance((const Vec*) this, (const Vec*) &other));
217 }
218
219 friend mVec3_c operator*(f32 f, const mVec3_c &v) {
220 return mVec3_c(v.x * f, v.y * f, v.z * f);
221 }
222
223 bool isSmallerThan1() const {
224 return PSVECMag(*this) <= 1.0f;
225 }
226
227 /// @brief Normalizes the vector.
228 /// @return The vector's magnitude.
229 float normalize();
230
231 /// @brief Normalizes the vector.
232 /// @return If the operation was successful.
233 bool normalizeRS();
234
235 void rotX(mAng angle); ///< Rotates the vector on the X axis by the given angle.
236 void rotY(mAng angle); ///< Rotates the vector on the Y axis by the given angle.
237
238 static mVec3_c Zero; ///< The null vector.
239 static mVec3_c Ex; ///< The unit vector for the X axis.
240 static mVec3_c Ey; ///< The unit vector for the Y axis.
241 static mVec3_c Ez; ///< The unit vector for the Z axis.
242};
A two-dimensional floating point vector.
Definition m_vec.hpp:26
mVec2_c(const mVec2_c &v)
Copy constructor.
Definition m_vec.hpp:41
mVec2_c operator+() const
Positive operator.
Definition m_vec.hpp:91
bool operator!=(const mVec2_c &v) const
Inequality operator.
Definition m_vec.hpp:112
mVec2_c & operator+=(const mVec2_c &v)
Augmented addition operator.
Definition m_vec.hpp:79
mVec2_c operator+(const mVec2_c &v) const
Addition operator.
Definition m_vec.hpp:97
mVec2_c()
Constructs an empty vector.
Definition m_vec.hpp:30
mVec2_c & operator/=(f32 f)
Augmented scalar division operator.
Definition m_vec.hpp:88
mVec2_c operator/(f32 f) const
Scalar division operator.
Definition m_vec.hpp:106
bool operator==(const mVec2_c &v) const
Equality operator.
Definition m_vec.hpp:109
mVec2_c(const f32 *p)
Constructs a vector from a float array.
Definition m_vec.hpp:35
void incY(float y)
Increments the Y coordinate.
Definition m_vec.hpp:55
mVec2_c(f32 fx, f32 fy)
Constructs a vector from two floating point values.
Definition m_vec.hpp:38
mVec2_c & operator*=(f32 f)
Augmented scalar product operator.
Definition m_vec.hpp:85
mVec2_c operator*(f32 f) const
Scalar product operator.
Definition m_vec.hpp:103
mVec2_c & operator-=(const mVec2_c &v)
Augmented subtraction operator.
Definition m_vec.hpp:82
mVec2_c operator-(const mVec2_c &v) const
Subtraction operator.
Definition m_vec.hpp:100
mVec2_c operator-() const
Negative operator.
Definition m_vec.hpp:94
void incX(float x)
Increments the X coordinate.
Definition m_vec.hpp:51
A three-dimensional floating point vector.
Definition m_vec.hpp:122
mVec3_c operator/(f32 f) const
Scalar division operator.
Definition m_vec.hpp:199
static mVec3_c Ex
The unit vector for the X axis.
Definition m_vec.hpp:239
static mVec3_c Ey
The unit vector for the Y axis.
Definition m_vec.hpp:240
mVec3_c(const nw4r::math::VEC3 &v)
Constructs a new vector from an existing vector from the NW4R library.
Definition m_vec.hpp:143
mVec3_c & operator-=(const mVec3_c &v)
Augmented subtraction operator.
Definition m_vec.hpp:175
mVec3_c operator*(f32 f) const
Scalar product operator.
Definition m_vec.hpp:196
mVec3_c & operator+=(const mVec3_c &v)
Augmented addition operator.
Definition m_vec.hpp:172
bool normalizeRS()
Normalizes the vector.
Definition m_vec.cpp:26
mVec3_c operator-() const
Negative operator.
Definition m_vec.hpp:187
mVec3_c operator-(const mVec3_c &v) const
Subtraction operator.
Definition m_vec.hpp:193
mVec3_c(const mVec3_c &v, float fz)
Copy constructor with a different Z value.
Definition m_vec.hpp:146
mVec3_c(f32 fx, f32 fy, f32 fz)
Constructs a vector from three floating point values.
Definition m_vec.hpp:134
mVec3_c(const f32 *p)
Constructs a vector from a float array.
Definition m_vec.hpp:131
float normalize()
Normalizes the vector.
Definition m_vec.cpp:17
bool operator==(const mVec3_c &v) const
Equality operator.
Definition m_vec.hpp:202
mVec3_c & operator*=(f32 f)
Augmented scalar product operator.
Definition m_vec.hpp:178
mVec3_c operator+() const
Positive operator.
Definition m_vec.hpp:184
void rotX(mAng angle)
Rotates the vector on the X axis by the given angle.
Definition m_vec.cpp:36
void rotY(mAng angle)
Rotates the vector on the Y axis by the given angle.
Definition m_vec.cpp:45
static mVec3_c Zero
The null vector.
Definition m_vec.hpp:238
mVec3_c & operator/=(f32 f)
Augmented scalar division operator.
Definition m_vec.hpp:181
mVec3_c(const mVec3_c &v)
Copy constructor.
Definition m_vec.hpp:140
bool operator!=(const mVec3_c &v) const
Inequality operator.
Definition m_vec.hpp:205
mVec3_c & operator=(const mVec3_c &v)
Assignment operator.
Definition m_vec.hpp:150
mVec3_c(const Vec &v)
Constructs a new vector from an existing vector from the MTX library.
Definition m_vec.hpp:137
mVec3_c operator+(const mVec3_c &v) const
Addition operator.
Definition m_vec.hpp:190
mVec3_c()
Constructs an empty vector.
Definition m_vec.hpp:126
static mVec3_c Ez
The unit vector for the Z axis.
Definition m_vec.hpp:241
s16 atan2s(float sin, float cos)
Converts a sine and a cosine to an angle in units.
Definition c_math.cpp:167