react-three-rapier
react-three-rapier copied to clipboard
Changing rigid body type on react update doesn't seem to work
I have a rigid body:
<RigidBody
position={props.position}
scale={[1, 1, 1]}
type={props.physicsType}
ref={el => { rigidBodies[props.index] = el; ref.current = el; }}>
...
If I change props.physicsType
to "dynamic" (from "kinematicPosition"), the RB does not become dynamic, however if I initialize props.physicsType
with the value "dynamic" it works.
I've also tried calling addForce
with wake=true, just in case the objects were sleeping and pushing the objects with another object, but nothing works.
The React component is definitely re-rendering when the value changes and has the correct/expected props values.
Is this a bug?
Seems related:
https://stackoverflow.com/questions/74647736/react-three-rapier-doesnt-update-rigidbodies
Also: https://github.com/pmndrs/react-three-rapier/discussions/188
Actually, my issue was solved by including the physics type in the key:
<RigidBody
key={props.physicsType + props.index}
After thinking about this a bit more, I think this is a hack/workaround - this is just forcing react to create a new tracking object when the state changes, but really props.index
alone (which is a stable identifier) should be enough, no?
reopening
Indeed, this is intended to work! In fact there is a Demo of this working here:
https://react-three-rapier.pmnd.rs/dynamic-type-changes 🤔
However there is an odd situation that the RigidBody does not always go back from kinematicPosition
to dynamic
, unless the body is forced to collide with something. I believe that is a Rapier
bug - but it's an odd one!
@dimforge/[email protected]
had an update related to this.
Fix bug that made dynamic rigid-bodies behave like kinematic bodies after being disabled and then re-enabled.
Is this still an issue in @react-three/[email protected]
?
This appears to still be happening. I can work around it with
rigidbody?.setEnabled(false);
rigidbody?.setEnabled(true);
whenever I'm changing the type. This is with @react-three/rapier
at 0.15.1