summaryrefslogtreecommitdiffstats
path: root/core/math/math_funcs.h
diff options
context:
space:
mode:
Diffstat (limited to 'core/math/math_funcs.h')
-rw-r--r--core/math/math_funcs.h31
1 files changed, 25 insertions, 6 deletions
diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h
index 934c75b5d3..366ccca4cb 100644
--- a/core/math/math_funcs.h
+++ b/core/math/math_funcs.h
@@ -399,15 +399,20 @@ public:
return d;
}
- static _ALWAYS_INLINE_ double lerp_angle(double p_from, double p_to, double p_weight) {
+ static _ALWAYS_INLINE_ double angle_difference(double p_from, double p_to) {
double difference = fmod(p_to - p_from, Math_TAU);
- double distance = fmod(2.0 * difference, Math_TAU) - difference;
- return p_from + distance * p_weight;
+ return fmod(2.0 * difference, Math_TAU) - difference;
}
- static _ALWAYS_INLINE_ float lerp_angle(float p_from, float p_to, float p_weight) {
+ static _ALWAYS_INLINE_ float angle_difference(float p_from, float p_to) {
float difference = fmod(p_to - p_from, (float)Math_TAU);
- float distance = fmod(2.0f * difference, (float)Math_TAU) - difference;
- return p_from + distance * p_weight;
+ return fmod(2.0f * difference, (float)Math_TAU) - difference;
+ }
+
+ static _ALWAYS_INLINE_ double lerp_angle(double p_from, double p_to, double p_weight) {
+ return p_from + Math::angle_difference(p_from, p_to) * p_weight;
+ }
+ static _ALWAYS_INLINE_ float lerp_angle(float p_from, float p_to, float p_weight) {
+ return p_from + Math::angle_difference(p_from, p_to) * p_weight;
}
static _ALWAYS_INLINE_ double inverse_lerp(double p_from, double p_to, double p_value) {
@@ -438,6 +443,7 @@ public:
float s = CLAMP((p_s - p_from) / (p_to - p_from), 0.0f, 1.0f);
return s * s * (3.0f - 2.0f * s);
}
+
static _ALWAYS_INLINE_ double move_toward(double p_from, double p_to, double p_delta) {
return abs(p_to - p_from) <= p_delta ? p_to : p_from + SIGN(p_to - p_from) * p_delta;
}
@@ -445,6 +451,19 @@ public:
return abs(p_to - p_from) <= p_delta ? p_to : p_from + SIGN(p_to - p_from) * p_delta;
}
+ static _ALWAYS_INLINE_ double rotate_toward(double p_from, double p_to, double p_delta) {
+ double difference = Math::angle_difference(p_from, p_to);
+ double abs_difference = Math::abs(difference);
+ // When `p_delta < 0` move no further than to PI radians away from `p_to` (as PI is the max possible angle distance).
+ return p_from + CLAMP(p_delta, abs_difference - Math_PI, abs_difference) * (difference >= 0.0 ? 1.0 : -1.0);
+ }
+ static _ALWAYS_INLINE_ float rotate_toward(float p_from, float p_to, float p_delta) {
+ float difference = Math::angle_difference(p_from, p_to);
+ float abs_difference = Math::abs(difference);
+ // When `p_delta < 0` move no further than to PI radians away from `p_to` (as PI is the max possible angle distance).
+ return p_from + CLAMP(p_delta, abs_difference - (float)Math_PI, abs_difference) * (difference >= 0.0f ? 1.0f : -1.0f);
+ }
+
static _ALWAYS_INLINE_ double linear_to_db(double p_linear) {
return Math::log(p_linear) * 8.6858896380650365530225783783321;
}