Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[math] Add SmoothStep and SmootherStep easing functions #16957

Merged
merged 1 commit into from
Dec 24, 2024

Conversation

scottmcm
Copy link
Contributor

@scottmcm scottmcm commented Dec 24, 2024

Objective

Almost all of the *InOut easing functions are not actually smooth (SineInOut is the one exception).

Because they're defined piecewise, they jump from accelerating upwards to accelerating downwards, causing infinite jerk at t=½.

Solution

This PR adds the well-known smoothstep, as well as its higher-degree version smootherstep, as easing functions.

Mathematically, these are the classic Hermite interpolation results:

  • for smoothstep, the cubic with velocity zero at both ends
  • for smootherstep, the quintic with velocity zero and acceleration zero at both ends

And because they're simple polynomials, there's no branching and thus they don't have the acceleration jump in the middle.

I also added some more information and cross-linking to the documentation for these and some of the other easing functions, to help clarify why one might want to use these over other existing ones. In particular, I suspect that if people are willing to pay for a quintic they might prefer SmootherStep to QuinticInOut.

For consistency with how everything else has triples, I added Smooth(er)Step{In,Out} as well, in case people want to run the In and Out versions separately for some reason. Qualitatively they're not hugely different from Quadratic{In,Out} or Cubic{In,Out}, though, so could be removed if you'd rather. They're low cost to keep, though, and convenient for testing.

Testing

These are simple polynomials, so their coefficients can be read directly from the Horner's method implementation and compared to the reference materials. The tests from #16910 were updated to also test these 6 new easing functions, ensuring basic behaviour, plus one was updated to better check that the InOut versions of things match their rescaled In and Out versions.

Even small changes like

-    (((2.5 + (-1.875 + 0.375*t) * t) * t) * t) * t
+    (((2.5 + (-1.85 + 0.375*t) * t) * t) * t) * t

are caught by multiple tests this way.

If you want to confirm them visually, here are the 6 new ones graphed: https://www.desmos.com/calculator/2d3ofujhry
smooth-and-smoother-step


Migration Guide

This version of bevy marks EaseFunction as #[non_exhaustive] to that future changes to add more easing functions will be non-breaking. If you were exhaustively matching that enum -- which you probably weren't -- you'll need to add a catch-all (_ =>) arm to cover unknown easing functions.

@BenjaminBrienen BenjaminBrienen added C-Feature A new feature, making something new possible A-Math Fundamental domain-agnostic mathematical operations D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Dec 24, 2024
@@ -127,6 +127,7 @@ where
/// Curve functions over the [unit interval], commonly used for easing transitions.
///
/// [unit interval]: `Interval::UNIT`
#[non_exhaustive]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that adding something to an enum is technically a breaking change, I figured I'd do the broader breaking change of marking this #[non_exhaustive] so that any future additions to the enum will be non-breaking.

@mockersf mockersf added this pull request to the merge queue Dec 24, 2024
Merged via the queue into bevyengine:main with commit f966534 Dec 24, 2024
28 checks passed
@IQuick143 IQuick143 added the M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide label Dec 24, 2024
@scottmcm scottmcm deleted the hermite-easing branch December 25, 2024 03:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples M-Needs-Migration-Guide A breaking change to Bevy's public API that needs to be noted in a migration guide S-Needs-Review Needs reviewer attention (from anyone!) to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants