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