summaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorChaosus <chaosus89@gmail.com>2019-07-14 07:30:45 +0300
committerChaosus <chaosus89@gmail.com>2019-07-20 12:59:41 +0300
commit6694c119d069d8ff8dc5290d38d2d33625f07807 (patch)
tree085f1fd73f885c29d417993d1c1a8ff0a3380bd9 /core/math
parentc317a3ce16a35b21d85b250a0e810526bb89db38 (diff)
Added lerp_angles built-in function
Co-authored-by: Xrayez <https://github.com/Xrayez> Co-authored-by: DleanJeans <https://github.com/DleanJeans>
Diffstat (limited to 'core/math')
-rw-r--r--core/math/expression.cpp9
-rw-r--r--core/math/expression.h1
-rw-r--r--core/math/math_funcs.h11
3 files changed, 21 insertions, 0 deletions
diff --git a/core/math/expression.cpp b/core/math/expression.cpp
index 2786229cf3..4552278c17 100644
--- a/core/math/expression.cpp
+++ b/core/math/expression.cpp
@@ -68,6 +68,7 @@ const char *Expression::func_name[Expression::FUNC_MAX] = {
"step_decimals",
"stepify",
"lerp",
+ "lerp_angle",
"inverse_lerp",
"range_lerp",
"smoothstep",
@@ -190,6 +191,7 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) {
case COLORN:
return 2;
case MATH_LERP:
+ case MATH_LERP_ANGLE:
case MATH_INVERSE_LERP:
case MATH_SMOOTHSTEP:
case MATH_MOVE_TOWARD:
@@ -395,6 +397,13 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant
VALIDATE_ARG_NUM(2);
*r_return = Math::lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
} break;
+ case MATH_LERP_ANGLE: {
+
+ VALIDATE_ARG_NUM(0);
+ VALIDATE_ARG_NUM(1);
+ VALIDATE_ARG_NUM(2);
+ *r_return = Math::lerp_angle((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
+ } break;
case MATH_INVERSE_LERP: {
VALIDATE_ARG_NUM(0);
diff --git a/core/math/expression.h b/core/math/expression.h
index 03a2bb70e6..833220592c 100644
--- a/core/math/expression.h
+++ b/core/math/expression.h
@@ -67,6 +67,7 @@ public:
MATH_STEP_DECIMALS,
MATH_STEPIFY,
MATH_LERP,
+ MATH_LERP_ANGLE,
MATH_INVERSE_LERP,
MATH_RANGE_LERP,
MATH_SMOOTHSTEP,
diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h
index b6398712e4..b8b5151802 100644
--- a/core/math/math_funcs.h
+++ b/core/math/math_funcs.h
@@ -215,6 +215,17 @@ public:
static _ALWAYS_INLINE_ double lerp(double p_from, double p_to, double p_weight) { return p_from + (p_to - p_from) * p_weight; }
static _ALWAYS_INLINE_ float lerp(float p_from, float p_to, float p_weight) { return p_from + (p_to - p_from) * p_weight; }
+ static _ALWAYS_INLINE_ double lerp_angle(double p_from, double p_to, double p_weight) {
+ double difference = fmod(p_to - p_from, Math_TAU);
+ double distance = fmod(2.0 * difference, Math_TAU) - difference;
+ return p_from + distance * p_weight;
+ }
+ static _ALWAYS_INLINE_ float lerp_angle(float p_from, float p_to, float p_weight) {
+ 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;
+ }
+
static _ALWAYS_INLINE_ double inverse_lerp(double p_from, double p_to, double p_value) { return (p_value - p_from) / (p_to - p_from); }
static _ALWAYS_INLINE_ float inverse_lerp(float p_from, float p_to, float p_value) { return (p_value - p_from) / (p_to - p_from); }