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

Preserve collisions between inactive entities, add sensor example #266

Merged
merged 13 commits into from
Dec 28, 2023

Conversation

TeamDman
Copy link
Contributor

Objective

Fixes #264

Code_OpvVDqEdIr.mp4

Changelog

This section is optional. If this was a trivial fix, or has no externally-visible impact, you can delete this section.

  • Added example
  • Added collision preservation logic

Other

5 tests are failing when I run them, but they fail without my changes so idk

Details
     Running unittests ../../src/lib.rs (target\debug\deps\bevy_xpbd_3d-f7a1d1ec81464a89.exe)

running 10 tests
test components::tests::coefficient_combine_works ... ok
test components::tests::restitution_clamping_works ... ok
test components::world_queries::tests::mass_properties_add_assign_works ... ok
test components::world_queries::tests::mass_properties_sub_assign_works ... ok
test components::world_queries::tests::mass_properties_add_sub_works ... ok
test tests::no_ambiguity_errors ... FAILED
test tests::body_with_velocity_moves ... FAILED
test tests::body_with_velocity_moves_on_first_frame ... FAILED
test tests::it_loads_plugin_without_errors ... FAILED
test tests::cubes_simulation_is_locally_deterministic ... FAILED

failures:

---- tests::no_ambiguity_errors stdout ----
thread 'tests::no_ambiguity_errors' panicked at C:\Users\TeamD\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.12.0\src\system\system_param.rs:451:17:
Resource requested by bevy_xpbd_3d::plugins::prepare::init_async_scene_colliders does not exist: bevy_asset::assets::Assets<bevy_render::mesh::mesh::Mesh>
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Encountered a panic in system `bevy_xpbd_3d::plugins::prepare::init_async_scene_colliders`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!

---- tests::body_with_velocity_moves stdout ----
thread 'tests::body_with_velocity_moves' panicked at C:\Users\TeamD\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.12.0\src\system\system_param.rs:451:17:
Resource requested by bevy_xpbd_3d::plugins::prepare::init_async_scene_colliders does not exist: bevy_asset::assets::Assets<bevy_render::mesh::mesh::Mesh>
Encountered a panic in system `bevy_xpbd_3d::plugins::prepare::init_async_scene_colliders`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!

---- tests::body_with_velocity_moves_on_first_frame stdout ----
thread 'tests::body_with_velocity_moves_on_first_frame' panicked at C:\Users\TeamD\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.12.0\src\system\system_param.rs:451:17:
Resource requested by bevy_xpbd_3d::plugins::prepare::init_async_scene_colliders does not exist: bevy_asset::assets::Assets<bevy_render::mesh::mesh::Mesh>
Encountered a panic in system `bevy_xpbd_3d::plugins::prepare::init_async_scene_colliders`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!

---- tests::it_loads_plugin_without_errors stdout ----
thread 'tests::it_loads_plugin_without_errors' panicked at C:\Users\TeamD\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.12.0\src\system\system_param.rs:451:17:
Resource requested by bevy_xpbd_3d::plugins::prepare::init_async_scene_colliders does not exist: bevy_asset::assets::Assets<bevy_render::mesh::mesh::Mesh>
Encountered a panic in system `bevy_xpbd_3d::plugins::prepare::init_async_scene_colliders`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!

---- tests::cubes_simulation_is_locally_deterministic stdout ----
thread 'tests::cubes_simulation_is_locally_deterministic' panicked at C:\Users\TeamD\.cargo\registry\src\index.crates.io-6f17d22bba15001f\bevy_ecs-0.12.0\src\system\system_param.rs:451:17:
Resource requested by bevy_xpbd_3d::plugins::prepare::init_async_scene_colliders does not exist: bevy_asset::assets::Assets<bevy_render::mesh::mesh::Mesh>
Encountered a panic in system `bevy_xpbd_3d::plugins::prepare::init_async_scene_colliders`!
Encountered a panic in system `bevy_app::main_schedule::Main::run_main`!


failures:
    tests::body_with_velocity_moves
    tests::body_with_velocity_moves_on_first_frame
    tests::cubes_simulation_is_locally_deterministic
    tests::it_loads_plugin_without_errors
    tests::no_ambiguity_errors

test result: FAILED. 5 passed; 5 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

.map(Option::unwrap)
.collect_vec();

collisions.extend(existing_unmoving_collisions);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This shouldn't introduce duplicate collisions since broad_collision_pairs doesn't include pairs of inactive entities, where "inactive" is determined by the same is_changed checks

https://github.com/Jondolf/bevy_xpbd/blob/088facfe3761235f0b297bc1493927a12a6fe21b/src/plugins/collision/broad_phase.rs#L192

https://github.com/Jondolf/bevy_xpbd/blob/088facfe3761235f0b297bc1493927a12a6fe21b/src/plugins/collision/broad_phase.rs#L259-L262

@Jondolf Jondolf added bugfix A-Collision Relates to the broad phase, narrow phase, colliders, or other collision functionality labels Nov 29, 2023
@Jondolf
Copy link
Owner

Jondolf commented Nov 29, 2023

Thanks! I'll review and test properly when I have time, hopefully tomorrow or at least by the end of the week.

Tests are failing with just cargo test because of #191. It should be a very quick fix so I'll do that now

@TeamDman
Copy link
Contributor Author

TeamDman commented Nov 29, 2023

Looks like my fix has a problem where the collisions aren't pruned if the position of the character is updated directly without using the velocity

cursor-hero_LZl9QWoVlg.mp4

@TeamDman
Copy link
Contributor Author

Updated the new example to use WASD for velocity-based movement and arrow keys for position-based movement.

Code_lIQYSL5VbW.mp4

Looks like it's better now :D

Copy link
Owner

@Jondolf Jondolf left a comment

Choose a reason for hiding this comment

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

Hi, sorry I'm properly reviewing this so late.

The approach here should be fine for now, but I think it might be a little hacky. Ideally, the broad phase would just preserve the collisions automatically and the narrow phase wouldn't need so much get_many. This can be attempted in a future PR though, I'm content getting this merged as soon as possible since it's been sitting for a while.

I'd also like the sensor example to be more minimal and focused; having both velocity and position based movement and other velocity stuff is useful for debugging this issue, but it's not really useful from a user learning perspective. I generally try to roughly follow Bevy's example guidelines for examples showcasing a specific feature, like sensors in this case. But to be fair, some of our examples already don't follow it very well, and it can be improved in future PRs, so it's not a blocker here.

There's some linting and compilation issues that still need to be resolved though. I left review comments for these.

for &(entity1, entity2) in broad_collision_pairs
.0
.iter()
.chain(existing_stationary_collisions)
Copy link
Owner

Choose a reason for hiding this comment

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

This causes ownership issues because existing_stationary_collisions borrows collisions but you also insert to it inside the loop.

// we want to preserve collisions between entities that are both stationary
// they are not included in [`BroadCollisionPairs`], so we need to ensure they are still checked
let existing_stationary_collisions = collisions.0.keys().filter(|&&(e1, e2)| {
if let Ok([bundle1, bundle2]) = bodies.get_many([e1, e2]) {
Copy link
Owner

Choose a reason for hiding this comment

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

Ideally, the broad phase would automatically preserve these collisions and we wouldn't need to do so much get_many. This can definitely be attempted in a follow-up PR though

src/plugins/collision/narrow_phase.rs Outdated Show resolved Hide resolved
crates/bevy_xpbd_2d/examples/sensor.rs Outdated Show resolved Hide resolved
crates/bevy_xpbd_2d/examples/sensor.rs Outdated Show resolved Hide resolved
Copy link
Owner

@Jondolf Jondolf left a comment

Choose a reason for hiding this comment

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

I fixed the rest of the errors myself and cleaned up the code a bit to reduce duplication. Merging now! I'll probably try to find a less hacky approach at some point though, and also simplify the sensor example a bit

@Jondolf Jondolf merged commit 4103b56 into Jondolf:main Dec 28, 2023
4 checks passed
@Jondolf Jondolf added C-Bug Something isn't working and removed bugfix labels Jul 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Collision Relates to the broad phase, narrow phase, colliders, or other collision functionality C-Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Velocity decaying to zero causes sensors to stop colliding
2 participants