bevyengine / bevyengine/bevy

Unexpected AabbCast2d aabb_collision_at with small max distance value

Open
#17,703 3 comments 0 reactions 0 assignees View on GitHub
A-Math C-Bug D-Straightforward S-Needs-Investigation
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 16h
Merged PRs (30d)
171

Description

## Bevy version
0.15.1

## \[Optional\] Relevant system information
* Arch Linux
* rustc 1.84.1 (e71f9a9a9 2025-01-27)

## What you did
I have some code that uses raycast2d over short distances that I expect to be similar to if I translated the aabb2 then did an intersection check.

## What went wrong
I am getting some unexpected results with these raycasts. I get an unexpected value of a collision at distance 0.0 for two Aabb2ds that should be sufficiently far apart.

Here's a small unit test I wrote to do the raycast some other ways that do pass, then the last call I expected to be a None but it's a Some(0.0). The numbers were pulled directly from a live run of the program when the raycast2d returned an unexpected answer.

```rust
#[test]
fn test_debug() {
let movement_amount = 0.974444;

let window = Aabb2d {
min: Vec2::new(-26.197449, -21.058212),
max: Vec2::new(323.80255, 278.94177),
};
let side = Aabb2d {
min: Vec2::new(472.0, -320.0),
max: Vec2::new(473.0, 320.0),
};

assert_eq!(false, window.intersects(&side));
assert_eq!(false, side.intersects(&window));

let mut window_new = window.clone();
window_new.translate_by(Vec2::new(movement_amount, 0.0));
assert_eq!(false, window_new.intersects(&side));
assert_eq!(false, side.intersects(&window_new));

let window_new_new = Aabb2d {
min: window.min + Vec2::new(movement_amount, 0.0),
max: window.max + Vec2::new(movement_amount, 0.0),
};

assert_eq!(false, window_new_new.intersects(&side));
assert_eq!(false, side.intersects(&window_new_new));

let ray_direction = Dir2::new(Vec2::new(1.0, 0.0)).unwrap();
let ray_towards_boundary_intersections = Ray2d::new(window.center(), ray_direction);
let bounds_check_ray_cast =
AabbCast2d::from_ray(window, ray_towards_boundary_intersections, movement_amount);

let res = bounds_check_ray_cast.aabb_collision_at(side);
assert_eq!(None, res);
}
```

```
---- collisions::tests::test_debug stdout ----
thread 'collisions::tests::test_debug' panicked at scroll_2d/src/collisions/tests.rs:234:5:
assertion `left == right` failed
left: None
right: Some(0.0)
```

Contributor guide

Open the contributing guide

Research direction

Start with the reproducing test at scroll_2d/src/collisions/tests.rs:234 and trace AabbCast2d::from_ray into aabb_collision_at. Verify why the short max distance produces Some(0.0) for the separated Aabb2ds, then add or update a regression test so the case returns None.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.