Open databasedav opened 7 months ago
fn main() {
let side = 720.;
let size = 13;
let cell = side / size as f32;
println!("{}", cell + cell + cell + cell + cell + cell + cell + cell + cell + cell + cell + cell + cell);
let cell_float = side / 13.;
println!("{}", cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float);
}
720.0001
720.0001
fn main() {
let side = 720.;
let size = 13;
// let cell = side / size as f32;
// println!("{}", cell + cell + cell + cell + cell + cell + cell + cell + cell + cell + cell + cell + cell);
let cell_float = side / 13.;
println!("{}", cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float + cell_float);
}
719.9999999999998
Bevy version
0.12.1 and 0.13.0
Relevant system information
cargo 1.76.0 (c84b36747 2024-01-18)
What you did
Discovered this issue when implementing snake with my UI library
haalka
. This version allows the grid to be resized dynamically, and I noticed that the grid "broke" with certain sizes, which one can see in the video I posted on discord:snake video
https://github.com/bevyengine/bevy/assets/31483365/36089696-d290-414f-91e9-0353ae2dc46f
What went wrong
Internally, I'm just updating the
Style.grid_template_columns
with the appropriateRepeatedGridTrack::px(GridTrackRepetition::AutoFill, cell_width)
as the "grid size", the number of cells on each axis, is changed; since the width of the parent container is fixed to720
,cell_width
is always720 / grid_size
.As can be seen in the snake video, only particular grid sizes trigger the breakage (e.g. 26 and 31), which makes me suspect that there's some float precision shenanigans going on in
taffy
or something. What happens to the cells as a result should be clear from the repro below.Additional information
I've created a minimal repro here https://github.com/databasedav/bevy/blob/grid_bug_maybe2/examples/ui/grid_bug_maybe.rs (adapted from the
grid
example)which conveniently takes the grid size as an environment variable. For example running
SIZE=12 cargo run --example grid_bug_maybe
produces the expected whileSIZE=13 cargo run --example grid_bug_maybe
produces an "off by 1"The sizes that produce this behavior that I've identified by just increasing the grid size for a bit in my snake game are (but not probably not limited to)
13, 26, 31, 51, 57, 58
, and they all break the same way using the repro.I've also noticed that changing the
SIDE
, the width in pixels of each axis, does affect the breaking, so the culprit must be the resultingcell_size
, e.g. a grid size of13
works fine with sides of740
SIDE=740 SIZE=13 cargo run --example grid_bug_maybe