summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/math/math_funcs.h31
-rw-r--r--core/variant/variant_utility.cpp10
-rw-r--r--core/variant/variant_utility.h2
3 files changed, 37 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;
}
diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp
index 0a63749ac5..089a7e341c 100644
--- a/core/variant/variant_utility.cpp
+++ b/core/variant/variant_utility.cpp
@@ -451,6 +451,10 @@ double VariantUtilityFunctions::bezier_derivative(double p_start, double p_contr
return Math::bezier_derivative(p_start, p_control_1, p_control_2, p_end, p_t);
}
+double VariantUtilityFunctions::angle_difference(double from, double to) {
+ return Math::angle_difference(from, to);
+}
+
double VariantUtilityFunctions::lerp_angle(double from, double to, double weight) {
return Math::lerp_angle(from, to, weight);
}
@@ -471,6 +475,10 @@ double VariantUtilityFunctions::move_toward(double from, double to, double delta
return Math::move_toward(from, to, delta);
}
+double VariantUtilityFunctions::rotate_toward(double from, double to, double delta) {
+ return Math::rotate_toward(from, to, delta);
+}
+
double VariantUtilityFunctions::deg_to_rad(double angle_deg) {
return Math::deg_to_rad(angle_deg);
}
@@ -1653,12 +1661,14 @@ void Variant::_register_variant_utility_functions() {
FUNCBINDR(cubic_interpolate_angle_in_time, sarray("from", "to", "pre", "post", "weight", "to_t", "pre_t", "post_t"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(bezier_interpolate, sarray("start", "control_1", "control_2", "end", "t"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(bezier_derivative, sarray("start", "control_1", "control_2", "end", "t"), Variant::UTILITY_FUNC_TYPE_MATH);
+ FUNCBINDR(angle_difference, sarray("from", "to"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(lerp_angle, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(inverse_lerp, sarray("from", "to", "weight"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(remap, sarray("value", "istart", "istop", "ostart", "ostop"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(smoothstep, sarray("from", "to", "x"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(move_toward, sarray("from", "to", "delta"), Variant::UTILITY_FUNC_TYPE_MATH);
+ FUNCBINDR(rotate_toward, sarray("from", "to", "delta"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(deg_to_rad, sarray("deg"), Variant::UTILITY_FUNC_TYPE_MATH);
FUNCBINDR(rad_to_deg, sarray("rad"), Variant::UTILITY_FUNC_TYPE_MATH);
diff --git a/core/variant/variant_utility.h b/core/variant/variant_utility.h
index 66883fb140..8130f30881 100644
--- a/core/variant/variant_utility.h
+++ b/core/variant/variant_utility.h
@@ -90,11 +90,13 @@ struct VariantUtilityFunctions {
double to_t, double pre_t, double post_t);
static double bezier_interpolate(double p_start, double p_control_1, double p_control_2, double p_end, double p_t);
static double bezier_derivative(double p_start, double p_control_1, double p_control_2, double p_end, double p_t);
+ static double angle_difference(double from, double to);
static double lerp_angle(double from, double to, double weight);
static double inverse_lerp(double from, double to, double weight);
static double remap(double value, double istart, double istop, double ostart, double ostop);
static double smoothstep(double from, double to, double val);
static double move_toward(double from, double to, double delta);
+ static double rotate_toward(double from, double to, double delta);
static double deg_to_rad(double angle_deg);
static double rad_to_deg(double angle_rad);
static double linear_to_db(double linear);