persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
MathManager.cpp
Go to the documentation of this file.
1#include "MathManager.hpp"
2#include "core/logger.hpp"
3
4ae::q20_12_t MathManager::div(ae::q20_12_t num, ae::q20_12_t den)
5{
6 if (den.raw_value() == 0)
7 {
8 logMessage(LogSeverity::ERROR, "Division by zero");
9 return ae::q20_12_t{0};
10 }
11 return ae::q20_12_t::from_raw_value(divf32(num.raw_value(), den.raw_value()));
12}
13
14ae::q20_12_t MathManager::sqrt(ae::q20_12_t v)
15{
16 if (v.raw_value() < 0)
17 {
18 logMessage(LogSeverity::WARNING, "Square root of negative value");
19 return ae::q20_12_t{0};
20 }
21 uint32_t raw = sqrtf32(static_cast<uint32_t>(v.raw_value()));
22 return ae::q20_12_t::from_raw_value(static_cast<int32_t>(raw));
23}
24
25ae::q20_12_t MathManager::mod(ae::q20_12_t num, ae::q20_12_t den)
26{
27 if (den.raw_value() == 0)
28 {
29 logMessage(LogSeverity::ERROR, "Modulo by zero");
30 return ae::q20_12_t{0};
31 }
32 return ae::q20_12_t::from_raw_value(mod32(num.raw_value(), den.raw_value()));
33}
34
35ae::angle16_t MathManager::radiansToAngle(ae::q20_12_t radians)
36{
37 return static_cast<ae::angle16_t>((static_cast<int32_t>(radians.raw_value()) * 5215) / 4096);
38}
39
40ae::angle16_t MathManager::degreesToCyclicAngle(ae::q20_12_t degrees)
41{
42 return static_cast<ae::angle16_t>((static_cast<int64_t>(degrees.raw_value()) * DEGREES_IN_CIRCLE / 360) >> 12);
43}
44
45ae::q20_12_t MathManager::angleToRadians(ae::angle16_t angle)
46{
47 int32_t raw_rad = (static_cast<int32_t>(angle) * 51472) >> 16;
48 return ae::q20_12_t::from_raw_value(raw_rad);
49}
50
51ae::q4_12_t MathManager::sin(ae::q20_12_t radians)
52{
53 return ae::q4_12_t::from_raw_value(sinLerp(radiansToAngle(radians)));
54}
55
56ae::q4_12_t MathManager::sin(ae::angle16_t angle)
57{
58 return ae::q4_12_t::from_raw_value(sinLerp(angle));
59}
60
61ae::q4_12_t MathManager::cos(ae::q20_12_t radians)
62{
63 return ae::q4_12_t::from_raw_value(cosLerp(radiansToAngle(radians)));
64}
65
66ae::q4_12_t MathManager::cos(ae::angle16_t angle)
67{
68 return ae::q4_12_t::from_raw_value(cosLerp(angle));
69}
70
71ae::q20_12_t MathManager::tan(ae::q20_12_t radians)
72{
73 return ae::q20_12_t::from_raw_value(tanLerp(radiansToAngle(radians)));
74}
75
76ae::angle16_t MathManager::asin(ae::q4_12_t ratio)
77{
78 return asinLerp(ratio.raw_value());
79}
80
81ae::angle16_t MathManager::acos(ae::q4_12_t ratio)
82{
83 return acosLerp(ratio.raw_value());
84}
85
86ae::q20_12_t MathManager::dot(
87 ae::q20_12_t x1, ae::q20_12_t y1, ae::q20_12_t z1, ae::q20_12_t x2, ae::q20_12_t y2, ae::q20_12_t z2)
88{
89 // libnds dotf32 handles the 64-bit accumulation internally and returns Q20.12
90 s32 vec1[3] = {x1.raw_value(), y1.raw_value(), z1.raw_value()};
91 s32 vec2[3] = {x2.raw_value(), y2.raw_value(), z2.raw_value()};
92 return ae::q20_12_t::from_raw_value(dotf32(vec1, vec2));
93}
94
95ae::q20_12_t MathManager::lengthSq(ae::q20_12_t x, ae::q20_12_t y, ae::q20_12_t z)
96{
97 return dot(x, y, z, x, y, z);
98}
99
100ae::q20_12_t MathManager::length(ae::q20_12_t x, ae::q20_12_t y, ae::q20_12_t z)
101{
102 return ae::q20_12_t::from_raw_value(sqrtf32(lengthSq(x, y, z).raw_value()));
103}
104
105void MathManager::normalize(ae::q20_12_t& x, ae::q20_12_t& y, ae::q20_12_t& z)
106{
107 s32 vec[3] = {x.raw_value(), y.raw_value(), z.raw_value()};
108
109 normalizef32(vec);
110
111 x = ae::q20_12_t::from_raw_value(vec[0]);
112 y = ae::q20_12_t::from_raw_value(vec[1]);
113 z = ae::q20_12_t::from_raw_value(vec[2]);
114}
115
117{
118 uint32_t r = static_cast<uint32_t>(rand()) % 4096;
119 return ae::q20_12_t::from_raw_value(static_cast<int32_t>(r));
120}
121
122ae::q20_12_t MathManager::atan2(ae::q20_12_t y, ae::q20_12_t x)
123{
124 if (y.raw_value() == 0 && x.raw_value() == 0)
125 {
126 return ae::q20_12_t{0}; // undefined direction, avoid UB in atan2_f32
127 }
128 return ae::q20_12_t::from_raw_value(atan2_f32(y.raw_value(), x.raw_value()));
129}
130
131ae::q20_12_t MathManager::atan(ae::q20_12_t y)
132{
133 return atan2(y, ae::q20_12_t{1});
134}
135
136u32 MathManager::secondsToSamples(ae::q20_12_t seconds, u32 sampleRate)
137{
138 u32 wholeSeconds = static_cast<u32>(seconds);
139 ae::q20_12_t fractionalSeconds = seconds - ae::q20_12_t{wholeSeconds};
140 u32 fractionalSamples = static_cast<u32>(fractionalSeconds * sampleRate);
141 return (wholeSeconds * sampleRate) + fractionalSamples;
142}
143
144ae::q20_12_t MathManager::clamp(ae::q20_12_t v, ae::q20_12_t lo, ae::q20_12_t hi)
145{
146 if (v < lo)
147 {
148 return lo;
149 }
150 if (v > hi)
151 {
152 return hi;
153 }
154 return v;
155}
156
157ae::q20_12_t MathManager::lerp(ae::q20_12_t a, ae::q20_12_t b, ae::q20_12_t t)
158{
159 return a + (b - a) * t;
160}
161
162int MathManager::lerp(int a, int b, ae::q20_12_t t)
163{
164 const ae::q20_12_t delta = ae::q20_12_t{b - a} * t;
165 return a + static_cast<int>(delta);
166}
167
168ae::q20_12_t MathManager::ease(EaseMode mode, ae::q20_12_t t)
169{
170 t = clamp(t, ae::q20_12_t{0}, ae::q20_12_t{1});
171
172 switch (mode)
173 {
174 case EaseMode::InQuad:
175 return t * t;
177 return t * (ae::q20_12_t{2} - t);
179 {
180 if (t < ae::q20_12_t::from_raw_value(2048)) // 0.5
181 {
182 return ae::q20_12_t{2} * t * t;
183 }
184 // -1 + (4 - 2t) * t
185 return ae::q20_12_t{-1} + (ae::q20_12_t{4} - ae::q20_12_t{2} * t) * t;
186 }
188 return t * t * t;
190 {
191 const ae::q20_12_t f = t - ae::q20_12_t{1};
192 return f * f * f + ae::q20_12_t{1};
193 }
194 case EaseMode::InBack:
195 {
196 // s ≈ 1.70158 → raw 1.70158 * 4096 ≈ 6970
197 const ae::q20_12_t s = ae::q20_12_t::from_raw_value(6970);
198 return t * t * ((s + ae::q20_12_t{1}) * t - s);
199 }
201 {
202 const ae::q20_12_t s = ae::q20_12_t::from_raw_value(6970);
203 const ae::q20_12_t f = t - ae::q20_12_t{1};
204 return f * f * ((s + ae::q20_12_t{1}) * f + s) + ae::q20_12_t{1};
205 }
206 case EaseMode::Linear:
207 default:
208 return t;
209 }
210}
ae::q20_12_t clamp(ae::q20_12_t v, ae::q20_12_t lo, ae::q20_12_t hi)
Clamps a Q20.12 value to [lo, hi].
ae::q20_12_t lengthSq(ae::q20_12_t x, ae::q20_12_t y, ae::q20_12_t z)
Computes the squared length of a Q20.12 3D vector.
ae::q4_12_t cos(ae::angle16_t angle)
Cosine of a cyclic angle.
ae::q4_12_t sin(ae::angle16_t angle)
Sine of a cyclic angle.
ae::q20_12_t sqrt(ae::q20_12_t v)
Computes the square root of a Q20.12 value.
ae::q20_12_t ease(EaseMode mode, ae::q20_12_t t)
Applies an easing curve to a normalized time value.
ae::q20_12_t angleToRadians(ae::angle16_t angle)
Converts a cyclic angle to radians (Q20.12).
ae::angle16_t asin(ae::q4_12_t ratio)
Computes the arcsine of a ratio.
ae::angle16_t degreesToCyclicAngle(ae::q20_12_t degrees)
Converts an angle in degrees (Q20.12) to a cyclic angle.
ae::angle16_t acos(ae::q4_12_t ratio)
Computes the arccosine of a ratio.
ae::angle16_t radiansToAngle(ae::q20_12_t radians)
Converts an angle in radians (Q20.12).
void normalize(ae::q20_12_t &x, ae::q20_12_t &y, ae::q20_12_t &z)
Normalizes a Q20.12 3D vector in place.
ae::q20_12_t atan2(ae::q20_12_t y, ae::q20_12_t x)
Computes the angle (in radians) between the positive x-axis and (x, y).
ae::q20_12_t length(ae::q20_12_t x, ae::q20_12_t y, ae::q20_12_t z)
Computes the length of a Q20.12 3D vector.
ae::q20_12_t div(ae::q20_12_t num, ae::q20_12_t den)
Divides two Q20.12 values.
ae::q20_12_t tan(ae::q20_12_t radians)
Tangent, accepting radians (Q20.12).
ae::q20_12_t lerp(ae::q20_12_t a, ae::q20_12_t b, ae::q20_12_t t)
Linearly interpolates two Q20.12 values.
ae::q20_12_t atan(ae::q20_12_t y)
Computes the arctangent of a single ratio.
ae::q20_12_t randFrac()
Returns a uniformly distributed random fraction from 0 to 1.
u32 secondsToSamples(ae::q20_12_t seconds, u32 sampleRate)
Converts a Q20.12 seconds value to a sample count, avoiding overflow when seconds * sampleRate exceed...
EaseMode
Easing curves for fixed-point interpolation (t in [0, 1]).
ae::q20_12_t mod(ae::q20_12_t num, ae::q20_12_t den)
Computes the modulo of two Q20.12 values.
ae::q20_12_t dot(ae::q20_12_t x1, ae::q20_12_t y1, ae::q20_12_t z1, ae::q20_12_t x2, ae::q20_12_t y2, ae::q20_12_t z2)
Computes the dot product of two Q20.12 3D vectors.
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
Util to handle error logging.