diff options
| author | Rémi Verschelde <remi@verschelde.fr> | 2021-11-09 22:11:04 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-09 22:11:04 +0100 |
| commit | 06a33e590f029d4d96e32df62f3c3961e1c33e4e (patch) | |
| tree | d91ff1e2c78a31d2897a2cc49218d4f3b7535459 /core | |
| parent | efea3128c3b0d026a20efaf6bd46058c2b732519 (diff) | |
| parent | 953a7bce7edf289dd8d2a1a6c8ecd8105380c8da (diff) | |
| download | redot-engine-06a33e590f029d4d96e32df62f3c3961e1c33e4e.tar.gz | |
Merge pull request #53819 from TokageItLab/re-implement-ping-pong
Reimplement ping-pong animation and reverse playback
Diffstat (limited to 'core')
| -rw-r--r-- | core/math/math_funcs.h | 13 | ||||
| -rw-r--r-- | core/variant/variant_utility.cpp | 5 |
2 files changed, 18 insertions, 0 deletions
diff --git a/core/math/math_funcs.h b/core/math/math_funcs.h index baff10af98..b3eabd3e7a 100644 --- a/core/math/math_funcs.h +++ b/core/math/math_funcs.h @@ -291,6 +291,19 @@ public: return is_zero_approx(range) ? min : value - (range * Math::floor((value - min) / range)); } + static _ALWAYS_INLINE_ float fract(float value) { + return value - floor(value); + } + static _ALWAYS_INLINE_ double fract(double value) { + return value - floor(value); + } + static _ALWAYS_INLINE_ float pingpong(float value, float length) { + return (length != 0.0f) ? abs(fract((value - length) / (length * 2.0f)) * length * 2.0f - length) : 0.0f; + } + static _ALWAYS_INLINE_ double pingpong(double value, double length) { + return (length != 0.0) ? abs(fract((value - length) / (length * 2.0)) * length * 2.0 - length) : 0.0; + } + // double only, as these functions are mainly used by the editor and not performance-critical, static double ease(double p_x, double p_c); static int step_decimals(double p_step); diff --git a/core/variant/variant_utility.cpp b/core/variant/variant_utility.cpp index 666b582e39..e89bdd4faa 100644 --- a/core/variant/variant_utility.cpp +++ b/core/variant/variant_utility.cpp @@ -275,6 +275,10 @@ struct VariantUtilityFunctions { return Math::wrapf(value, min, max); } + static inline double pingpong(double value, double length) { + return Math::pingpong(value, length); + } + static inline Variant max(const Variant **p_args, int p_argcount, Callable::CallError &r_error) { if (p_argcount < 2) { r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; @@ -1226,6 +1230,7 @@ void Variant::_register_variant_utility_functions() { FUNCBINDR(clampf, sarray("value", "min", "max"), Variant::UTILITY_FUNC_TYPE_MATH); FUNCBINDR(nearest_po2, sarray("value"), Variant::UTILITY_FUNC_TYPE_MATH); + FUNCBINDR(pingpong, sarray("value", "length"), Variant::UTILITY_FUNC_TYPE_MATH); // Random |
