pmndrs / react-three-rapier

🤺 Rapier physics in React
https://react-three-rapier.pmnd.rs
MIT License
1.08k stars 60 forks source link

Changing rigid body type on react update doesn't seem to work #314

Open jcowles opened 1 year ago

jcowles commented 1 year ago

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?

jcowles commented 1 year ago

Seems related:

https://stackoverflow.com/questions/74647736/react-three-rapier-doesnt-update-rigidbodies

Also: https://github.com/pmndrs/react-three-rapier/discussions/188

jcowles commented 1 year ago

Actually, my issue was solved by including the physics type in the key:

<RigidBody
    key={props.physicsType + props.index}
jcowles commented 1 year ago

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

wiledal commented 1 year ago

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!

wiledal commented 1 year ago

@dimforge/rapier3d-compat@0.12.2 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/rapier@0.14.0?

gregfagan commented 1 year ago

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