Support Transform
for moving and positioning bodies
#96
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the only way to move and position bodies after initialization is to use the
Position
andRotation
components. Using these separate components has many benefits over usingTransform
for physics, as discussed in #16.However, if we want Bevy XPBD to feel as integrated into Bevy as possible, it would make sense to allow using
Transform
directly for moving bodies, as it is what Bevy and the rest of its ecosystem typically uses. This would also allow third party crates to move bodies in a more backend-agnostic way without having to have explicit support for Bevy XPBD.This PR adds a new synchronization system for
SyncPlugin
that updatesPosition
andRotation
based on changes inGlobalTransform
. This does not replace the separate components, but rather adds another way to position bodies in a more familiar and native way.The new behaviour is entirely optional, and there is a new
SyncConfig
resource for disabling syncing features.Caveats
(fixed)Transform
changes are only handled if they happen before physicsUpdate: I fixed this by changing the
GlobalTransform
change detection a bit and running transform propagation before and after each physics frame.Transform
s can now be modified at any time, and everything should work normally. The problem described below no longer applies.To only detect user changes in
GlobalTransform
, I use aPreviousGlobalTransform
component that stores the global transform from the very beginning of each frame. If the transform has changed before physics is run,transform_to_position
is run.This avoids unnecessary runs of the system and circumvents some change detection problems, but it also means that
Transform
updates are only handled if they happen before physics is run.Additional transform propagation
To handle hierarchies and to have
GlobalTransform
change detection working properly, I need to run a total of three copies of Bevy's transform propagation: one before physics, one right after physics, and a final one afterposition_to_transform
syncing.I don't think it's particularly expensive, but it would be nice to find a way to avoid running it so many times. However, I'm not sure if it's avoidable if we want to handle the
Transform
syncing in a robust manner.Best practices for positioning bodies
There are now two ways to position bodies, either using
Transform
or usingPosition
andRotation
. The best practises are unclear here; should people use transforms, which is considered to be a more native way, or should they continue to usePosition
andRotation
, which is what the engine itself uses?For now, I think it's enough to just mention both ways in the docs and let people make their own conclusions. Later on, we can maybe make clearer recommendations, and maybe even revisit #16 in some capacity.