SpaghettiKart
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 float Square() const {
77 return x * x + y * y + z * z;
78 }
79
80 FVector() : x(0), y(0), z(0) {}
81 FVector(float x, float y, float z) : x(x), y(y), z(z) {}
82 NLOHMANN_DEFINE_TYPE_INTRUSIVE(FVector, x, y, z)
83#endif // __cplusplus
84};
85
86struct FVector4 {
87 float x, y, z, w;
88
89#ifdef __cplusplus
90
91 FVector4() : x(0), y(0), z(0), w(0) {}
92 FVector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
93#endif // __cplusplus
94};
95
102struct FVector2D {
103 float x, z;
104
105#ifdef __cplusplus
106 FVector2D& operator=(const FVector2D& other) {
107 x = other.x;
108 z = other.z;
109 return *this;
110 }
111
112 FVector2D() : x(0), z(0) {}
113 FVector2D(float x, float z) : x(x), z(z) {}
114 NLOHMANN_DEFINE_TYPE_INTRUSIVE(FVector2D, x, z)
115#endif // __cplusplus
116};
117
118// Sets integer X Z coordinates
119typedef struct IVector2D {
120 int32_t X, Y;
121
122#ifdef __cplusplus
123 IVector2D() : X(0), Y(0) {} // Default constructor
124
125 IVector2D(int32_t x, int32_t y) : X(x), Y(y) {} // Constructor to initialize with values
126
127
128 IVector2D& operator=(const IVector2D& other) {
129 X = other.X;
130 Y = other.Y;
131 return *this;
132 }
133#endif // __cplusplus
135
141struct IRotator {
142 uint16_t pitch, yaw, roll;
143
144#ifdef __cplusplus
145 NLOHMANN_DEFINE_TYPE_INTRUSIVE(IRotator, pitch, yaw, roll)
146
147 IRotator& operator=(const IRotator& other) {
148 pitch = other.pitch;
149 yaw = other.yaw;
150 roll = other.roll;
151 return *this;
152 }
153
154 [[nodiscard]] void Set(uint16_t p, uint16_t y, uint16_t r) {
155 pitch = p;
156 yaw = y;
157 roll = r;
158 }
159
160 IRotator() : pitch(0), yaw(0), roll(0) {}
161 IRotator(float p, float y, float r) {
162 pitch = p * (UINT16_MAX / 360);
163 yaw = y * (UINT16_MAX / 360);
164 roll = r * (UINT16_MAX / 360);
165 }
166
167 // Convert to radians as FVector
168 [[nodiscard]] FVector ToRadians() const {
169 float scale = 2.0f * M_PI / 65536.0f;
170 return FVector(
171 pitch * scale,
172 yaw * scale,
173 roll * scale
174 );
175 }
176#endif // __cplusplus
177};
178
184struct FRotator {
185 float pitch, yaw, roll;
186
187#ifdef __cplusplus
188 FRotator& operator=(const FRotator& other) {
189 pitch = other.pitch;
190 yaw = other.yaw;
191 roll = other.roll;
192 return *this;
193 }
194
195 // Convert to binary rotator 0 --> INT16_MAX
196 [[nodiscard]] IRotator ToBinary() const {
197 return IRotator(
198 static_cast<uint16_t>(pitch * (UINT16_MAX / 360)),
199 static_cast<uint16_t>(yaw * (UINT16_MAX / 360)),
200 static_cast<uint16_t>(roll * (UINT16_MAX / 360))
201 );
202 }
203
204 FRotator() : pitch(0), yaw(0), roll(0) {}
205 FRotator(float p, float y, float r) : pitch(p), yaw(y), roll(r) {}
206 FRotator(IRotator rot) {
207 pitch = static_cast<float>(rot.pitch * (360 / UINT16_MAX));
208 yaw = static_cast<float>(rot.yaw * (360 / UINT16_MAX));
209 roll = static_cast<float>(rot.roll * (360 / UINT16_MAX));
210 }
211#endif // __cplusplus
212};
213
218struct IPathSpan {
219 int32_t Start, End;
220
221#ifdef __cplusplus
222 NLOHMANN_DEFINE_TYPE_INTRUSIVE(IPathSpan, Start, End)
223 // Default Constructor
224 IPathSpan() : Start(0), End(0) {}
225
226 // Parameterized Constructor
227 IPathSpan(int InStart, int InEnd)
228 : Start(InStart), End(InEnd) {}
229
230 // Copy Assignment Operator
231 IPathSpan& operator=(const IPathSpan& Other) {
232 if (this != &Other) { // Avoid self-assignment
233 Start = Other.Start;
234 End = Other.End;
235 }
236 return *this;
237 }
238
239 // Equality Operator
240 bool operator==(const IPathSpan& Other) const {
241 return Start == Other.Start && End == Other.End;
242 }
243
244 // Inequality Operator
245 bool operator!=(const IPathSpan& Other) const {
246 return !(*this == Other);
247 }
248#endif // __cplusplus
249};
250
251#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:184
float yaw
Definition CoreMath.h:185
float roll
Definition CoreMath.h:185
float pitch
Definition CoreMath.h:185
Definition CoreMath.h:102
float x
Definition CoreMath.h:103
float z
Definition CoreMath.h:103
Definition CoreMath.h:86
float y
Definition CoreMath.h:87
float w
Definition CoreMath.h:87
float z
Definition CoreMath.h:87
float x
Definition CoreMath.h:87
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:218
int32_t Start
Definition CoreMath.h:219
int32_t End
Definition CoreMath.h:219
Definition CoreMath.h:141
uint16_t roll
Definition CoreMath.h:142
uint16_t yaw
Definition CoreMath.h:142
uint16_t pitch
Definition CoreMath.h:142
Definition CoreMath.h:119
int32_t Y
Definition CoreMath.h:120
int32_t X
Definition CoreMath.h:120
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