From 780e21bcacd66de9bde038ae9cd31d80f700f16a Mon Sep 17 00:00:00 2001 From: Rakka Rage Date: Wed, 10 May 2023 01:03:36 -0400 Subject: [PATCH] Add TRANS_SPRING to Tween --- doc/classes/Tween.xml | 3 +++ scene/animation/easing_equations.h | 29 +++++++++++++++++++++++++++++ scene/animation/tween.cpp | 2 ++ scene/animation/tween.h | 1 + 4 files changed, 35 insertions(+) diff --git a/doc/classes/Tween.xml b/doc/classes/Tween.xml index 7e313c3d4c96..51efbd31d8cb 100644 --- a/doc/classes/Tween.xml +++ b/doc/classes/Tween.xml @@ -501,6 +501,9 @@ The animation is interpolated backing out at ends. + + The animation is interpolated like a spring towards the end. + The interpolation starts slowly and speeds up towards the end. diff --git a/scene/animation/easing_equations.h b/scene/animation/easing_equations.h index 1b9c378b4fe9..91501917443b 100644 --- a/scene/animation/easing_equations.h +++ b/scene/animation/easing_equations.h @@ -413,4 +413,33 @@ static real_t out_in(real_t t, real_t b, real_t c, real_t d) { } }; // namespace back +namespace spring { +static real_t out(real_t t, real_t b, real_t c, real_t d) { + t /= d; + real_t s = 1.0 - t; + t = (sin(t * Math_PI * (0.2 + 2.5 * t * t * t)) * pow(s, 2.2) + t) * (1.0 + (1.2 * s)); + return c * t + b; +} + +static real_t in(real_t t, real_t b, real_t c, real_t d) { + return c - out(d - t, 0, c, d) + b; +} + +static real_t in_out(real_t t, real_t b, real_t c, real_t d) { + if (t < d / 2) { + return in(t * 2, b, c / 2, d); + } + real_t h = c / 2; + return out(t * 2 - d, b + h, h, d); +} + +static real_t out_in(real_t t, real_t b, real_t c, real_t d) { + if (t < d / 2) { + return out(t * 2, b, c / 2, d); + } + real_t h = c / 2; + return in(t * 2 - d, b + h, h, d); +} +}; // namespace spring + #endif // EASING_EQUATIONS_H diff --git a/scene/animation/tween.cpp b/scene/animation/tween.cpp index 8266cfbd4334..96e5da5a400e 100644 --- a/scene/animation/tween.cpp +++ b/scene/animation/tween.cpp @@ -50,6 +50,7 @@ Tween::interpolater Tween::interpolaters[Tween::TRANS_MAX][Tween::EASE_MAX] = { { &circ::in, &circ::out, &circ::in_out, &circ::out_in }, { &bounce::in, &bounce::out, &bounce::in_out, &bounce::out_in }, { &back::in, &back::out, &back::in_out, &back::out_in }, + { &spring::in, &spring::out, &spring::in_out, &spring::out_in }, }; void Tweener::set_tween(const Ref &p_tween) { @@ -483,6 +484,7 @@ void Tween::_bind_methods() { BIND_ENUM_CONSTANT(TRANS_CIRC); BIND_ENUM_CONSTANT(TRANS_BOUNCE); BIND_ENUM_CONSTANT(TRANS_BACK); + BIND_ENUM_CONSTANT(TRANS_SPRING); BIND_ENUM_CONSTANT(EASE_IN); BIND_ENUM_CONSTANT(EASE_OUT); diff --git a/scene/animation/tween.h b/scene/animation/tween.h index 67823da040e4..10c7a272ef16 100644 --- a/scene/animation/tween.h +++ b/scene/animation/tween.h @@ -87,6 +87,7 @@ class Tween : public RefCounted { TRANS_CIRC, TRANS_BOUNCE, TRANS_BACK, + TRANS_SPRING, TRANS_MAX };