pmndrs / use-cannon

👋💣 physics based hooks for @react-three/fiber
https://cannon.pmnd.rs
2.73k stars 153 forks source link

Cannot set `quaternion` in argsFn of `useBox` (and other useBody hooks) #389

Closed mz8i closed 10 months ago

mz8i commented 2 years ago

When using one of the hooks like useBox or useTrimesh, one can set rotation inside the object returned from argsFn, but a quaternion property is ignored (the body's rotation will always be reset to zero when dependencies of the useX hook change).

A brief check in the code suggests that the problem is in https://github.com/pmndrs/use-cannon/blob/90dfc122945877b91f8d58700e3363e828a52d1b/packages/cannon-worker-api/src/props-to-body.js#L61 - propsToBody in the worker code only extracts rotation from props, but not quaternion.

The workaround right now is simply to use rotation rather than quaternion, or convert quaternions to Euler angles when saving the rotation externally to later restore it. But as the API allows working only with quaternions, I think it would be appropriate to also allow users to set the body's quaternion in the argsFn of the hooks.

isaac-mason commented 11 months ago

Thanks for working on this @Soham1803

The body props type has both a rotation (euler) and a quaternion prop.

We should update props-to-body.js to support the quaternion prop as well as rotation, rather than treating rotation as a quaternion.

Soham1803 commented 11 months ago

Won't these lead to conflicts if we have two different props handling the same property of the body?

isaac-mason commented 11 months ago

We'd just need to prefer one if both are provided, we can document the behaviour.

Soham1803 commented 11 months ago

Ok I got it. I hope this #437 solves it. I requested a pull for a new branch. I noticed I had unnecessary changes in my yarn.lock file pushed in the previous branch.

isaac-mason commented 11 months ago

Thanks @Soham1803 🙂 will get this released shortly

Soham1803 commented 11 months ago

Well, thanks to you too @isaac-mason for your guidance. 😇

isaac-mason commented 10 months ago

The fix has been released, closing this issue