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

Lack of ability to inspect mutable queries via &self receiver in 0.6 breaks Rapier integration #2744

Closed
pcwalton opened this issue Aug 29, 2021 · 7 comments
Labels
A-ECS Entities, components, systems, and events A-Physics Collisions, kinematics, forces and more C-Usability A targeted quality-of-life change that makes Bevy easier to use

Comments

@pcwalton
Copy link
Contributor

Bevy version

0.6 prerelease, f6b42b8

Details

The Rapier physics library abstracts over scene graphs with traits called ComponentSet and ComponentSetMut, where ComponentSetMut requires that ComponentSet be implemented. ComponentSet allows immutable lookup of physics-related properties with a fn get(&self, ...) -> Option<&T>, while ComponentSetMut allows mutation of those properties with fn set(&mut self, ...). The bevy_rapier crate targeting Bevy 0.5 implements this trait on a QuerySet that performs immutable and mutable versions of the query, and takes advantage of the fact that fn q0(&self) and fn q0_mut(&mut self) are separate methods. But in Bevy 0.6 as of #2605, the fn q0(&self) method has been removed in favor of the mutable version. This means that it's not possible to safely simultaneously implement ComponentSet and ComponentSetMut anymore on a query.

The best safe workaround I can think of is to copy all the physics-related data for every entity out to a temporary collection every frame and then copy it back in after the physics code has run. This is obviously not shippable :) Even the Bevy 0.5 workaround isn't great, because it performs the query twice.

It'd be very nice if temporary immutable access to mutable queries were possible, by borrowing the mutable query. I'm not familiar enough with all the lifetimes involved to be able to propose a concrete API here, though.

@pcwalton pcwalton added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Aug 29, 2021
@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events and removed S-Needs-Triage This issue needs to be labelled labels Aug 29, 2021
@alice-i-cecile
Copy link
Member

@cart I'd prefer not to break a major plugin with the launch. Should we add this to the 0.6 milestone?

@alice-i-cecile
Copy link
Member

#2305 tackles a related issue; perhaps there's techniques we can share?

@pcwalton
Copy link
Contributor Author

#2305 tackles a related issue; perhaps there's techniques we can share?

Oh, interesting. Would it be helpful to see if I can successfully port Rapier on top of that PR?

@alice-i-cecile
Copy link
Member

Oh, interesting. Would it be helpful to see if I can successfully port Rapier on top of that PR?

Yes! I would start by resolving the merge conflicts and submitting them as a PR to @Guvante's branch.

In general, Bevy needs to be able to convert mutable accesses into immutable ones. The reverse is obviously illegal, but the weakening of access restrictions should always be allowed.

There's a number of places where this comes to mind: queries, query iteration, resource access and entity access to start. It's probably not correct to handle them all in one PR, but the broader context is helpful to bear in mind as you investigate to see if you can identify common techniques or a unified API.

@alice-i-cecile alice-i-cecile added S-Needs-Design This issue requires design work to think about how it would best be accomplished C-Usability A targeted quality-of-life change that makes Bevy easier to use A-Physics Collisions, kinematics, forces and more and removed C-Bug An unexpected or incorrect behavior labels Aug 29, 2021
pcwalton added a commit to pcwalton/bevy that referenced this issue Aug 30, 2021
The biggest change here is that doing the same query twice, one mutably and one
immutably, is no longer needed, once bevyengine#2305 lands. Before that
lands, however, this upgrade is actually impossible to do safely, since the
`q0`/`q0_mut` distinction is gone and therefore it's impossible to implement
the Rapier component traits on Bevy queries. (This is filed upstream as
bevyengine#2744.) Therefore, this upgrade has to wait until that Bevy PR
lands.

Other minor changes:

* `Light` has been renamed to `PointLight`.

* `App::build()` is now `App::new()`, and `AppBuilder` is now `App`.

* `glam` has been upgraded upstream.
@pcwalton
Copy link
Contributor Author

Yes! I would start by resolving the merge conflicts and submitting them as a PR to @Guvante's branch.

Done: Guvante#1 Though it's probably easier for @Guvante to just do this via a rebase, since it was trivial.

I'm happy to report that bevy_rapier works with #2305. I've submitted the changes needed as a PR: dimforge/bevy_rapier#98.

pcwalton added a commit to pcwalton/bevy_rapier that referenced this issue Aug 30, 2021
The biggest change here is that doing the same query twice, one mutably and one
immutably, is no longer needed, once bevyengine/bevy#2305 lands. Before that
lands, however, this upgrade is actually impossible to do safely, since the
`q0`/`q0_mut` distinction is gone and therefore it's impossible to implement
the Rapier component traits on Bevy queries. (This is filed upstream as
bevyengine/bevy#2744.) Therefore, this upgrade has to wait until that Bevy PR
lands.

Other minor changes:

* `Light` has been renamed to `PointLight`.

* `App::build()` is now `App::new()`, and `AppBuilder` is now `App`.

* `glam` has been upgraded upstream.
pcwalton added a commit to pcwalton/bevy_rapier that referenced this issue Aug 30, 2021
The biggest change here is that doing the same query twice, one mutably and one
immutably, is no longer needed, once bevyengine/bevy#2305 lands. Before that
lands, however, this upgrade is actually impossible to do safely, since the
`q0`/`q0_mut` distinction is gone and therefore it's impossible to implement
the Rapier component traits on Bevy queries. (This is filed upstream as
bevyengine/bevy#2744.) Therefore, this upgrade has to wait until that Bevy PR
lands.

Other minor changes:

* `Light` has been renamed to `PointLight`.

* `App::build()` is now `App::new()`, and `AppBuilder` is now `App`.

* `glam` has been upgraded upstream.
@cart
Copy link
Member

cart commented Aug 30, 2021

Brilliant. Glad to hear that the new apis compose so well. I agree that this is something that should be included in the 0.6 release, so I just added #2305 to the 0.6 milestone.

@alice-i-cecile alice-i-cecile removed the S-Needs-Design This issue requires design work to think about how it would best be accomplished label Aug 30, 2021
deontologician pushed a commit to deontologician/bevy_rapier that referenced this issue Oct 18, 2021
The biggest change here is that doing the same query twice, one mutably and one
immutably, is no longer needed, once bevyengine/bevy#2305 lands. Before that
lands, however, this upgrade is actually impossible to do safely, since the
`q0`/`q0_mut` distinction is gone and therefore it's impossible to implement
the Rapier component traits on Bevy queries. (This is filed upstream as
bevyengine/bevy#2744.) Therefore, this upgrade has to wait until that Bevy PR
lands.

Other minor changes:

* `Light` has been renamed to `PointLight`.

* `App::build()` is now `App::new()`, and `AppBuilder` is now `App`.

* `glam` has been upgraded upstream.
@NiklasEi
Copy link
Member

NiklasEi commented Dec 2, 2021

Can this issue be closed now that #2305 is merged?

sebcrozet pushed a commit to dimforge/bevy_rapier that referenced this issue Jan 9, 2022
The biggest change here is that doing the same query twice, one mutably and one
immutably, is no longer needed, once bevyengine/bevy#2305 lands. Before that
lands, however, this upgrade is actually impossible to do safely, since the
`q0`/`q0_mut` distinction is gone and therefore it's impossible to implement
the Rapier component traits on Bevy queries. (This is filed upstream as
bevyengine/bevy#2744.) Therefore, this upgrade has to wait until that Bevy PR
lands.

Other minor changes:

* `Light` has been renamed to `PointLight`.

* `App::build()` is now `App::new()`, and `AppBuilder` is now `App`.

* `glam` has been upgraded upstream.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events A-Physics Collisions, kinematics, forces and more C-Usability A targeted quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

No branches or pull requests

4 participants