Mario Kart 64
Loading...
Searching...
No Matches
CoreMath.h
Go to the documentation of this file.
1#ifndef CORE_MATH_H
2#define CORE_MATH_H
3
4#ifdef __cplusplus
5#include <nlohmann/json.hpp>
6#endif
7
8#include <libultraship.h>
9
16
17struct RGBA8 {
18 uint8_t r, g, b, a;
19#ifdef __cplusplus
20 NLOHMANN_DEFINE_TYPE_INTRUSIVE(RGBA8, r, g, b, a)
21#endif
22};
23
24
30struct FVector {
31 float x, y, z;
32
33#ifdef __cplusplus
34 // Operator to add two FVector objects
35 FVector operator+(const FVector& other) const {
36 return FVector(x + other.x, y + other.y, z + other.z);
37 }
38
39 // Operator to subtract two FVector objects
40 FVector operator-(const FVector& other) const {
41 return FVector(x - other.x, y - other.y, z - other.z);
42 }
43
44 // Operator to multiply a FVector by a scalar (float)
45 FVector operator*(float scalar) const {
46 return FVector(x * scalar, y * scalar, z * scalar);
47 }
48
49 float Dot(const FVector& other) const {
50 return x * other.x + y * other.y + z * other.z;
51 }
52
53
54 FVector Cross(const FVector& other) const {
55 return FVector(
56 y * other.z - z * other.y,
57 z * other.x - x * other.z,
58 x * other.y - y * other.x
59 );
60 }
61
62 float Magnitude() const {
63 return std::sqrt(x * x + y * y + z * z);
64 }
65
66 FVector Normalize() const {
67 float len = std::sqrt(x * x + y * y + z * z);
68 if (len > 0.0001f) {
69 return FVector(
70 x / len, y / len, z / len
71 );
72 }
73 return FVector(0, 0, 0);
74 }
75
76 FVector() : x(0), y(0), z(0) {}
77 FVector(float x, float y, float z) : x(x), y(y), z(z) {}
78 NLOHMANN_DEFINE_TYPE_INTRUSIVE(FVector, x, y, z)
79#endif // __cplusplus
80};
81
82struct FVector4 {
83 float x, y, z, w;
84
85#ifdef __cplusplus
86
87 FVector4() : x(0), y(0), z(0), w(0) {}
88 FVector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
89#endif // __cplusplus
90};
91
98struct FVector2D {
99 float x, z;
100
101#ifdef __cplusplus
102 FVector2D& operator=(const FVector2D& other) {
103 x = other.x;
104 z = other.z;
105 return *this;
106 }
107
108 FVector2D() : x(0), z(0) {}
109 FVector2D(float x, float z) : x(x), z(z) {}
110 NLOHMANN_DEFINE_TYPE_INTRUSIVE(FVector2D, x, z)
111#endif // __cplusplus
112};
113
114// Sets integer X Z coordinates
115typedef struct IVector2D {
116 int32_t X, Y;
117
118#ifdef __cplusplus
119 IVector2D() : X(0), Y(0) {} // Default constructor
120
121 IVector2D(int32_t x, int32_t y) : X(x), Y(y) {} // Constructor to initialize with values
122
123
124 IVector2D& operator=(const IVector2D& other) {
125 X = other.X;
126 Y = other.Y;
127 return *this;
128 }
129#endif // __cplusplus
131
137struct IRotator {
138 uint16_t pitch, yaw, roll;
139
140#ifdef __cplusplus
141 NLOHMANN_DEFINE_TYPE_INTRUSIVE(IRotator, pitch, yaw, roll)
142
143 IRotator& operator=(const IRotator& other) {
144 pitch = other.pitch;
145 yaw = other.yaw;
146 roll = other.roll;
147 return *this;
148 }
149
150 [[nodiscard]] void Set(uint16_t p, uint16_t y, uint16_t r) {
151 pitch = p;
152 yaw = y;
153 roll = r;
154 }
155
156 IRotator() : pitch(0), yaw(0), roll(0) {}
157 IRotator(float p, float y, float r) {
158 pitch = p * (UINT16_MAX / 360);
159 yaw = y * (UINT16_MAX / 360);
160 roll = r * (UINT16_MAX / 360);
161 }
162
163 // Convert to radians as FVector
164 [[nodiscard]] FVector ToRadians() const {
165 float scale = 2.0f * M_PI / 65536.0f;
166 return FVector(
167 pitch * scale,
168 yaw * scale,
169 roll * scale
170 );
171 }
172#endif // __cplusplus
173};
174
180struct FRotator {
181 float pitch, yaw, roll;
182
183#ifdef __cplusplus
184 FRotator& operator=(const FRotator& other) {
185 pitch = other.pitch;
186 yaw = other.yaw;
187 roll = other.roll;
188 return *this;
189 }
190
191 // Convert to binary rotator 0 --> INT16_MAX
192 [[nodiscard]] IRotator ToBinary() const {
193 return IRotator(
194 static_cast<uint16_t>(pitch * (UINT16_MAX / 360)),
195 static_cast<uint16_t>(yaw * (UINT16_MAX / 360)),
196 static_cast<uint16_t>(roll * (UINT16_MAX / 360))
197 );
198 }
199
200 FRotator() : pitch(0), yaw(0), roll(0) {}
201 FRotator(float p, float y, float r) : pitch(p), yaw(y), roll(r) {}
202 FRotator(IRotator rot) {
203 pitch = static_cast<float>(rot.pitch * (360 / UINT16_MAX));
204 yaw = static_cast<float>(rot.yaw * (360 / UINT16_MAX));
205 roll = static_cast<float>(rot.roll * (360 / UINT16_MAX));
206 }
207#endif // __cplusplus
208};
209
214struct IPathSpan {
215 int32_t Start, End;
216
217#ifdef __cplusplus
218 NLOHMANN_DEFINE_TYPE_INTRUSIVE(IPathSpan, Start, End)
219 // Default Constructor
220 IPathSpan() : Start(0), End(0) {}
221
222 // Parameterized Constructor
223 IPathSpan(int InStart, int InEnd)
224 : Start(InStart), End(InEnd) {}
225
226 // Copy Assignment Operator
227 IPathSpan& operator=(const IPathSpan& Other) {
228 if (this != &Other) { // Avoid self-assignment
229 Start = Other.Start;
230 End = Other.End;
231 }
232 return *this;
233 }
234
235 // Equality Operator
236 bool operator==(const IPathSpan& Other) const {
237 return Start == Other.Start && End == Other.End;
238 }
239
240 // Inequality Operator
241 bool operator!=(const IPathSpan& Other) const {
242 return !(*this == Other);
243 }
244#endif // __cplusplus
245};
246
247#endif // CORE_MATH_H
struct IVector2D IVector2D
#define M_PI
Definition matrix.h:30
bool operator==(Color_RGB8 const &l, Color_RGB8 const &r) noexcept
Definition Menu.cpp:29
Definition CoreMath.h:180
float yaw
Definition CoreMath.h:181
float roll
Definition CoreMath.h:181
float pitch
Definition CoreMath.h:181
Definition CoreMath.h:98
float x
Definition CoreMath.h:99
float z
Definition CoreMath.h:99
Definition CoreMath.h:82
float y
Definition CoreMath.h:83
float w
Definition CoreMath.h:83
float z
Definition CoreMath.h:83
float x
Definition CoreMath.h:83
Definition CoreMath.h:30
float x
Definition CoreMath.h:31
float z
Definition CoreMath.h:31
float y
Definition CoreMath.h:31
Definition CoreMath.h:214
int32_t Start
Definition CoreMath.h:215
int32_t End
Definition CoreMath.h:215
Definition CoreMath.h:137
uint16_t roll
Definition CoreMath.h:138
uint16_t yaw
Definition CoreMath.h:138
uint16_t pitch
Definition CoreMath.h:138
Definition CoreMath.h:115
int32_t Y
Definition CoreMath.h:116
int32_t X
Definition CoreMath.h:116
Definition CoreMath.h:17
uint8_t g
Definition CoreMath.h:18
uint8_t a
Definition CoreMath.h:18
uint8_t r
Definition CoreMath.h:18
uint8_t b
Definition CoreMath.h:18