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#include <libultraship.h>
5
12
18struct FVector {
19 float x, y, z;
20
21#ifdef __cplusplus
22 // Operator to add two FVector objects
23 FVector operator+(const FVector& other) const {
24 return FVector(x + other.x, y + other.y, z + other.z);
25 }
26
27 // Operator to subtract two FVector objects
28 FVector operator-(const FVector& other) const {
29 return FVector(x - other.x, y - other.y, z - other.z);
30 }
31
32 // Operator to multiply a FVector by a scalar (float)
33 FVector operator*(float scalar) const {
34 return FVector(x * scalar, y * scalar, z * scalar);
35 }
36
37 float Dot(const FVector& other) const {
38 return x * other.x + y * other.y + z * other.z;
39 }
40
41
42 FVector Cross(const FVector& other) const {
43 return FVector(
44 y * other.z - z * other.y,
45 z * other.x - x * other.z,
46 x * other.y - y * other.x
47 );
48 }
49
50 float Magnitude() const {
51 return std::sqrt(x * x + y * y + z * z);
52 }
53
54 FVector Normalize() const {
55 float len = std::sqrt(x * x + y * y + z * z);
56 if (len > 0.0001f) {
57 return FVector(
58 x / len, y / len, z / len
59 );
60 }
61 return FVector(0, 0, 0);
62 }
63
64 FVector() : x(0), y(0), z(0) {}
65 FVector(float x, float y, float z) : x(x), y(y), z(z) {}
66#endif // __cplusplus
67};
68
69struct FVector4 {
70 float x, y, z, w;
71
72#ifdef __cplusplus
73
74 FVector4() : x(0), y(0), z(0), w(0) {}
75 FVector4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
76#endif // __cplusplus
77};
78
85struct FVector2D {
86 float x, z;
87
88#ifdef __cplusplus
89 FVector2D& operator=(const FVector2D& other) {
90 x = other.x;
91 z = other.z;
92 return *this;
93 }
94
95 FVector2D() : x(0), z(0) {}
96 FVector2D(float x, float z) : x(x), z(z) {}
97#endif // __cplusplus
98};
99
100// Sets integer X Z coordinates
101typedef struct IVector2D {
102 int32_t X, Y;
103
104#ifdef __cplusplus
105 IVector2D() : X(0), Y(0) {} // Default constructor
106
107 IVector2D(int32_t x, int32_t y) : X(x), Y(y) {} // Constructor to initialize with values
108
109
110 IVector2D& operator=(const IVector2D& other) {
111 X = other.X;
112 Y = other.Y;
113 return *this;
114 }
115#endif // __cplusplus
117
123struct IRotator {
124 uint16_t pitch, yaw, roll;
125
126#ifdef __cplusplus
127 IRotator& operator=(const IRotator& other) {
128 pitch = other.pitch;
129 yaw = other.yaw;
130 roll = other.roll;
131 return *this;
132 }
133
134 [[nodiscard]] void Set(uint16_t p, uint16_t y, uint16_t r) {
135 pitch = p;
136 yaw = y;
137 roll = r;
138 }
139
140 IRotator() : pitch(0), yaw(0), roll(0) {}
141 IRotator(float p, float y, float r) {
142 pitch = p * (UINT16_MAX / 360);
143 yaw = y * (UINT16_MAX / 360);
144 roll = r * (UINT16_MAX / 360);
145 }
146
147 // Convert to radians as FVector
148 [[nodiscard]] FVector ToRadians() const {
149 float scale = 2.0f * M_PI / 65536.0f;
150 return FVector(
151 pitch * scale,
152 yaw * scale,
153 roll * scale
154 );
155 }
156#endif // __cplusplus
157};
158
164struct FRotator {
165 float pitch, yaw, roll;
166
167#ifdef __cplusplus
168 FRotator& operator=(const FRotator& other) {
169 pitch = other.pitch;
170 yaw = other.yaw;
171 roll = other.roll;
172 return *this;
173 }
174
175 // Convert to binary rotator 0 --> INT16_MAX
176 [[nodiscard]] IRotator ToBinary() const {
177 return IRotator(
178 static_cast<uint16_t>(pitch * (UINT16_MAX / 360)),
179 static_cast<uint16_t>(yaw * (UINT16_MAX / 360)),
180 static_cast<uint16_t>(roll * (UINT16_MAX / 360))
181 );
182 }
183
184 FRotator() : pitch(0), yaw(0), roll(0) {}
185 FRotator(float p, float y, float r) : pitch(p), yaw(y), roll(r) {}
186 FRotator(IRotator rot) {
187 pitch = static_cast<float>(rot.pitch * (360 / UINT16_MAX));
188 yaw = static_cast<float>(rot.yaw * (360 / UINT16_MAX));
189 roll = static_cast<float>(rot.roll * (360 / UINT16_MAX));
190 }
191#endif // __cplusplus
192};
193
198struct IPathSpan {
199 int Start, End;
200
201#ifdef __cplusplus
202 // Default Constructor
203 IPathSpan() : Start(0), End(0) {}
204
205 // Parameterized Constructor
206 IPathSpan(int InStart, int InEnd)
207 : Start(InStart), End(InEnd) {}
208
209 // Copy Assignment Operator
210 IPathSpan& operator=(const IPathSpan& Other) {
211 if (this != &Other) { // Avoid self-assignment
212 Start = Other.Start;
213 End = Other.End;
214 }
215 return *this;
216 }
217
218 // Equality Operator
219 bool operator==(const IPathSpan& Other) const {
220 return Start == Other.Start && End == Other.End;
221 }
222
223 // Inequality Operator
224 bool operator!=(const IPathSpan& Other) const {
225 return !(*this == Other);
226 }
227#endif // __cplusplus
228};
229
230#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:164
float yaw
Definition CoreMath.h:165
float roll
Definition CoreMath.h:165
float pitch
Definition CoreMath.h:165
Definition CoreMath.h:85
float x
Definition CoreMath.h:86
float z
Definition CoreMath.h:86
Definition CoreMath.h:69
float y
Definition CoreMath.h:70
float w
Definition CoreMath.h:70
float z
Definition CoreMath.h:70
float x
Definition CoreMath.h:70
Definition CoreMath.h:18
float x
Definition CoreMath.h:19
float z
Definition CoreMath.h:19
float y
Definition CoreMath.h:19
Definition CoreMath.h:198
int Start
Definition CoreMath.h:199
int End
Definition CoreMath.h:199
Definition CoreMath.h:123
uint16_t roll
Definition CoreMath.h:124
uint16_t yaw
Definition CoreMath.h:124
uint16_t pitch
Definition CoreMath.h:124
Definition CoreMath.h:101
int32_t Y
Definition CoreMath.h:102
int32_t X
Definition CoreMath.h:102