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