pmndrs / use-cannon

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

Unable to read postiion/rotation after update, this is now more expensive (!?) #238

Closed AlexandrosGounis closed 3 years ago

AlexandrosGounis commented 3 years ago

Commit cd7427705506fea3bb700e4a748dbee8271e67af changed matrixAutoUpdate. Consider a scenario like this:

const { camera } = useThree()

const [ref, api] = useSphere(() => ({
    mass: 1,
    args: 0.5,
    position: [20, 10, 0],
}))

...

useFrame(() => {
     camera.position.set(ref.current.position[0] - 10, 10, current.position[2]) // ref.current.position not updated anymore
})

In order to get the position now, I have to call ref.current.getWorldPosition() which appears to be more expensive and because this is inside a frameLoop it might affect performance.

What is the proper way to handle this? Can I force ref to always update the position?

idigger commented 3 years ago

Solved?

AlexandrosGounis commented 3 years ago

I wouldn't consider this an issue anymore, there is a workaround. For instance, one may subscribe to api.position and get the position from there. I tend to use regular requestAnimationFrame rather than useFrame as I have more control over it.

drcmda commented 3 years ago

the objects are driven by matrix (matrixAutoUpdate=false) so pos/rot/scale are stale. this is faster. you can extract the position either from the matrix or use getworldpos, both shouldn't be an overhead. or by subscription ofc. i don't think you need to use the more expensive getworldpos, but you can use vec.setFromMatrixPosition(ref.current.matrixWorld ) directly.

idigger commented 3 years ago

Thank you for your replies.