summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/math/expression.cpp9
-rw-r--r--core/math/expression.h1
-rw-r--r--core/math/math_funcs.h11
-rw-r--r--modules/gdscript/doc_classes/@GDScript.xml23
-rw-r--r--modules/gdscript/gdscript_functions.cpp13
-rw-r--r--modules/gdscript/gdscript_functions.h1
-rw-r--r--modules/mono/glue/Managed/Files/Mathf.cs6
-rw-r--r--modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml5
-rw-r--r--modules/visual_script/visual_script_builtin_funcs.cpp24
-rw-r--r--modules/visual_script/visual_script_builtin_funcs.h1
10 files changed, 84 insertions, 10 deletions
diff --git a/core/math/expression.cpp b/core/math/expression.cpp
index 25a40c70bf..15eea1d308 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); }
diff --git a/modules/gdscript/doc_classes/@GDScript.xml b/modules/gdscript/doc_classes/@GDScript.xml
index a21b6a2907..2a26df4714 100644
--- a/modules/gdscript/doc_classes/@GDScript.xml
+++ b/modules/gdscript/doc_classes/@GDScript.xml
@@ -604,6 +604,29 @@
[/codeblock]
</description>
</method>
+ <method name="lerp_angle">
+ <return type="float">
+ </return>
+ <argument index="0" name="from" type="float">
+ </argument>
+ <argument index="1" name="to" type="float">
+ </argument>
+ <argument index="2" name="weight" type="float">
+ </argument>
+ <description>
+ Linearly interpolates between two angles (in radians) by a normalized value.
+ Similar to [method lerp] but interpolate correctly when the angles wrap around [constant @GDScript.TAU].
+ [codeblock]
+ extends Sprite
+ var elapsed = 0.0
+ func _process(delta):
+ var min_angle = deg2rad(0.0)
+ var max_angle = deg2rad(90.0)
+ rotation = lerp_angle(min_angle, max_angle, elapsed)
+ elapsed += delta
+ [/codeblock]
+ </description>
+ </method>
<method name="linear2db">
<return type="float">
</return>
diff --git a/modules/gdscript/gdscript_functions.cpp b/modules/gdscript/gdscript_functions.cpp
index 8c33d6613c..f5f245b25f 100644
--- a/modules/gdscript/gdscript_functions.cpp
+++ b/modules/gdscript/gdscript_functions.cpp
@@ -76,6 +76,7 @@ const char *GDScriptFunctions::get_func_name(Function p_func) {
"step_decimals",
"stepify",
"lerp",
+ "lerp_angle",
"inverse_lerp",
"range_lerp",
"smoothstep",
@@ -383,6 +384,13 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
} break;
}
} break;
+ case MATH_LERP_ANGLE: {
+ VALIDATE_ARG_COUNT(3);
+ VALIDATE_ARG_NUM(0);
+ VALIDATE_ARG_NUM(1);
+ VALIDATE_ARG_NUM(2);
+ r_ret = Math::lerp_angle((double)*p_args[0], (double)*p_args[1], (double)*p_args[2]);
+ } break;
case MATH_INVERSE_LERP: {
VALIDATE_ARG_COUNT(3);
VALIDATE_ARG_NUM(0);
@@ -1676,6 +1684,11 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
mi.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
return mi;
} break;
+ case MATH_LERP_ANGLE: {
+ MethodInfo mi("lerp_angle", PropertyInfo(Variant::REAL, "from"), PropertyInfo(Variant::REAL, "to"), PropertyInfo(Variant::REAL, "weight"));
+ mi.return_val.type = Variant::REAL;
+ return mi;
+ } break;
case MATH_INVERSE_LERP: {
MethodInfo mi("inverse_lerp", PropertyInfo(Variant::REAL, "from"), PropertyInfo(Variant::REAL, "to"), PropertyInfo(Variant::REAL, "weight"));
mi.return_val.type = Variant::REAL;
diff --git a/modules/gdscript/gdscript_functions.h b/modules/gdscript/gdscript_functions.h
index d52b9118d3..8f7ba76d2c 100644
--- a/modules/gdscript/gdscript_functions.h
+++ b/modules/gdscript/gdscript_functions.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/modules/mono/glue/Managed/Files/Mathf.cs b/modules/mono/glue/Managed/Files/Mathf.cs
index 2d8c63fe7f..6c1a51fcf9 100644
--- a/modules/mono/glue/Managed/Files/Mathf.cs
+++ b/modules/mono/glue/Managed/Files/Mathf.cs
@@ -185,6 +185,12 @@ namespace Godot
return from + (to - from) * weight;
}
+ public static real_t LerpAngle(real_t from, real_t to, real_t weight) {
+ real_t difference = (to - from) % Mathf.Tau;
+ real_t distance = ((2 * difference) % Mathf.Tau) - difference;
+ return from + distance * weight;
+ }
+
public static real_t Log(real_t s)
{
return (real_t)Math.Log(s);
diff --git a/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml b/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml
index 470a3a5e35..29e9cc31e5 100644
--- a/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml
+++ b/modules/visual_script/doc_classes/VisualScriptBuiltinFunc.xml
@@ -212,8 +212,11 @@
var t = clamp((weight - from) / (to - from), 0.0, 1.0)
return t * t * (3.0 - 2.0 * t)
[/codeblock]
+ <constant name="MATH_POSMOD" value="65" enum="BuiltinFunc">
</constant>
- <constant name="FUNC_MAX" value="65" enum="BuiltinFunc">
+ <constant name="MATH_LERP_ANGLE" value="66" enum="BuiltinFunc">
+ </constant>
+ <constant name="FUNC_MAX" value="67" enum="BuiltinFunc">
Represents the size of the [enum BuiltinFunc] enum.
</constant>
</constants>
diff --git a/modules/visual_script/visual_script_builtin_funcs.cpp b/modules/visual_script/visual_script_builtin_funcs.cpp
index 6c9c6b0fc8..f1ccb64e70 100644
--- a/modules/visual_script/visual_script_builtin_funcs.cpp
+++ b/modules/visual_script/visual_script_builtin_funcs.cpp
@@ -105,6 +105,7 @@ const char *VisualScriptBuiltinFunc::func_name[VisualScriptBuiltinFunc::FUNC_MAX
"color_named",
"smoothstep",
"posmod",
+ "lerp_angle",
};
VisualScriptBuiltinFunc::BuiltinFunc VisualScriptBuiltinFunc::find_function(const String &p_string) {
@@ -207,6 +208,7 @@ int VisualScriptBuiltinFunc::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:
@@ -318,7 +320,9 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
return PropertyInfo(Variant::REAL, "steps");
} break;
case MATH_LERP:
- case MATH_INVERSE_LERP: {
+ case MATH_LERP_ANGLE:
+ case MATH_INVERSE_LERP:
+ case MATH_SMOOTHSTEP: {
if (p_idx == 0)
return PropertyInfo(Variant::REAL, "from");
else if (p_idx == 1)
@@ -338,14 +342,6 @@ PropertyInfo VisualScriptBuiltinFunc::get_input_value_port_info(int p_idx) const
else
return PropertyInfo(Variant::REAL, "ostop");
} break;
- case MATH_SMOOTHSTEP: {
- if (p_idx == 0)
- return PropertyInfo(Variant::REAL, "from");
- else if (p_idx == 1)
- return PropertyInfo(Variant::REAL, "to");
- else
- return PropertyInfo(Variant::REAL, "weight");
- } break;
case MATH_MOVE_TOWARD: {
if (p_idx == 0)
return PropertyInfo(Variant::REAL, "from");
@@ -532,6 +528,7 @@ PropertyInfo VisualScriptBuiltinFunc::get_output_value_port_info(int p_idx) cons
} break;
case MATH_STEPIFY:
case MATH_LERP:
+ case MATH_LERP_ANGLE:
case MATH_INVERSE_LERP:
case MATH_RANGE_LERP:
case MATH_SMOOTHSTEP:
@@ -856,6 +853,13 @@ void VisualScriptBuiltinFunc::exec_func(BuiltinFunc p_func, const Variant **p_in
VALIDATE_ARG_NUM(2);
*r_return = Math::lerp((double)*p_inputs[0], (double)*p_inputs[1], (double)*p_inputs[2]);
} break;
+ case VisualScriptBuiltinFunc::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 VisualScriptBuiltinFunc::MATH_INVERSE_LERP: {
VALIDATE_ARG_NUM(0);
@@ -1369,6 +1373,7 @@ void VisualScriptBuiltinFunc::_bind_methods() {
BIND_ENUM_CONSTANT(BYTES_TO_VAR);
BIND_ENUM_CONSTANT(COLORN);
BIND_ENUM_CONSTANT(MATH_SMOOTHSTEP);
+ BIND_ENUM_CONSTANT(MATH_LERP_ANGLE);
BIND_ENUM_CONSTANT(FUNC_MAX);
}
@@ -1422,6 +1427,7 @@ void register_visual_script_builtin_func_node() {
VisualScriptLanguage::singleton->add_register_func("functions/built_in/decimals", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_DECIMALS>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/stepify", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_STEPIFY>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/lerp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LERP>);
+ VisualScriptLanguage::singleton->add_register_func("functions/built_in/lerp_angle", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_LERP_ANGLE>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/inverse_lerp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_INVERSE_LERP>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/range_lerp", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_RANGE_LERP>);
VisualScriptLanguage::singleton->add_register_func("functions/built_in/smoothstep", create_builtin_func_node<VisualScriptBuiltinFunc::MATH_SMOOTHSTEP>);
diff --git a/modules/visual_script/visual_script_builtin_funcs.h b/modules/visual_script/visual_script_builtin_funcs.h
index 7821e0eb2d..cf475d675d 100644
--- a/modules/visual_script/visual_script_builtin_funcs.h
+++ b/modules/visual_script/visual_script_builtin_funcs.h
@@ -105,6 +105,7 @@ public:
COLORN,
MATH_SMOOTHSTEP,
MATH_POSMOD,
+ MATH_LERP_ANGLE,
FUNC_MAX
};