From 3f434108934853c65cba1405d88ec102cc442712 Mon Sep 17 00:00:00 2001 From: Red_Epicness Date: Tue, 28 Nov 2023 15:26:11 +0100 Subject: [PATCH] Fix animation strategies IN_EXP, OUT_EXP, and IN_OUT_EXP These would not give the correct value at the extremes because it's an exponential curve, it doesn't actually ever get to the value, but for practical reason having it be 0 at 0 and 1 at 1 is much preferred. IN_OUT_EXP is fixed by implementing it in terms of IN_EXP and OUT_EXP. GitHub: #129 --- .../constraints/animation/AnimationStrategy.kt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/gg/essential/elementa/constraints/animation/AnimationStrategy.kt b/src/main/kotlin/gg/essential/elementa/constraints/animation/AnimationStrategy.kt index 15d6547b..63f2b77b 100644 --- a/src/main/kotlin/gg/essential/elementa/constraints/animation/AnimationStrategy.kt +++ b/src/main/kotlin/gg/essential/elementa/constraints/animation/AnimationStrategy.kt @@ -116,20 +116,23 @@ enum class Animations : AnimationStrategy { }, IN_EXP { override fun getValue(percentComplete: Float): Float { - return 2f.pow(10 * (percentComplete - 1)) + return if (percentComplete <= 0f) 0f else 2f.pow(10 * (percentComplete - 1)) } }, OUT_EXP { override fun getValue(percentComplete: Float): Float { - return -(2f.pow(-10 * percentComplete)) + 1 + return if (percentComplete >= 1f) 1f else -(2f.pow(-10 * percentComplete)) + 1 } }, IN_OUT_EXP { override fun getValue(percentComplete: Float): Float { - var t = percentComplete * 2 - if (t < 1) return 0.5f * 2f.pow(10 * (t - 1)) - t-- - return 0.5f * (-(2f.pow(-10 * t)) + 2) + // We don't need the extra checks, as those are now handled by the functions we call + // multiply percentComplete by 2 to normalize range before calling getValue + // then divide by two to shrink it to 0.0-0.5 + return if (percentComplete < 0.5) IN_EXP.getValue(percentComplete * 2) / 2 + // multiply percentComplete by 2 and subtract one to normalize range before calling getValue + // then divide by two to shrink it and add 0.5 to move it upwards, as a continuation of the first half + else OUT_EXP.getValue((percentComplete * 2) - 1) / 2 + 0.5f } }, IN_CIRCULAR { @@ -201,4 +204,4 @@ enum class Animations : AnimationStrategy { return OUT_BOUNCE.getValue(percentComplete * 2 - 1) * 0.5f + 0.5f } } -} \ No newline at end of file +}