Get the position of the falling object (react three cannon)

I am using @react-three/cannon to add physics to objects. I wanted to attach the camera to the object, but when the object falls, the camera stays in place, Ref.current.position shows the initial values. How do I know the position of a falling object?

1 Like

the objects are matrix driven, they do not use position/rotation. you could either fetch the matrix or listen to the objects whereabout:

const [ref, api] = use...(...)
const pos = useRef([0, 0, 0])
useEffect(() => api.position.subscribe(v => pos.current = v), [])

this demo BestServedBold Christmas Baubles - CodeSandbox for instance uses the current position to calculate magnetic force towards 0/0/0.

2 Likes

However, this does not work smoothly, subscription skips frames

im not aware of frame skips, i dont think thats possible because the object receive positioning through the same channel (a webworkers postmessage) the subscription data flows. but in any case, you can use the matrix and fetch pos/rot/scale from it.