Skip to content

Commit

Permalink
Fix rounding in steps easing function (#17743)
Browse files Browse the repository at this point in the history
# Objective

While working on #17742, I noticed that the `Steps` easing function
looked a bit suspicious.


![image](https://github.com/user-attachments/assets/be8f07e4-2079-461f-8c23-56d4b689aed9)

Comparing to the options available in
[css](https://developer.mozilla.org/en-US/docs/Web/CSS/easing-function/steps#description):


![image](https://github.com/user-attachments/assets/2c351519-c87f-483f-b5ff-63a9ee7b7b51)

It is "off the charts," so probably not what users are expecting.

## Solution

Use `floor` when rounding to match the default behavior (jump-end, top
right) in css.

<img width="100" alt="image"
src="https://github.com/user-attachments/assets/1ec46270-72f2-4227-87e4-03de881548ab"
/>


## Testing

I had to modify an existing test that was testing against the old
behavior. This function and test were introduced in #14788 and I didn't
see any discussion about the rounding there.

`cargo run --example easing_functions`

## Migration Guide

<!-- Note to editors: this should be adjusted if 17744 is addressed, and
possibly combined with the notes from the PR that fixes it. -->

`EaseFunction::Steps` now behaves like css's default, "jump-end." If you
were relying on the old behavior, we plan on providing it. See
#17744.
  • Loading branch information
rparrett authored Feb 8, 2025
1 parent a9de516 commit f3d8eb8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/bevy_math/src/curve/easing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ mod easing_functions {

#[inline]
pub(crate) fn steps(num_steps: usize, t: f32) -> f32 {
ops::round(t * num_steps as f32) / num_steps.max(1) as f32
ops::floor(t * num_steps as f32) / num_steps.max(1) as f32
}

#[inline]
Expand Down
14 changes: 6 additions & 8 deletions crates/bevy_math/src/curve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,14 +1064,12 @@ mod tests {
let curve = EasingCurve::new(start, end, EaseFunction::Steps(4));
[
(0.0, start),
(0.124, start),
(0.125, Vec2::new(0.25, 0.5)),
(0.374, Vec2::new(0.25, 0.5)),
(0.375, Vec2::new(0.5, 1.0)),
(0.624, Vec2::new(0.5, 1.0)),
(0.625, Vec2::new(0.75, 1.5)),
(0.874, Vec2::new(0.75, 1.5)),
(0.875, end),
(0.249, start),
(0.250, Vec2::new(0.25, 0.5)),
(0.499, Vec2::new(0.25, 0.5)),
(0.500, Vec2::new(0.5, 1.0)),
(0.749, Vec2::new(0.5, 1.0)),
(0.750, Vec2::new(0.75, 1.5)),
(1.0, end),
]
.into_iter()
Expand Down

0 comments on commit f3d8eb8

Please sign in to comment.