Open jacky0707 opened 2 years ago
hi, You have to get the position from API by subscribing it like you did with velocity :
const pos = useRef(new THREE.Vector3(0, 0, 0));
useEffect(
() =>
api.position.subscribe((v) => {
return (pos.current = new THREE.Vector3(v[0], v[1], v[2]));
}),
[]
);
and then try to apply to the camera camera.position.copy(pos)
get the position from API by subscribing it like you did with velocity :
const pos = useRef(new THREE.Vector3(0, 0, 0));
this help me fix this issue, thank you very much.
however I still have some queries point on this issue,
current I'm able to move with the sphere, but ref.current.position
still didn't update value when ref
is from
const [ref, api] = useSphere(() => ({
mass: 1,
type: 'Dynamic',
position: [0, 10, 0],
}))
same code as above works when I'm using
"@react-three/cannon": "^2.2.1",
"@react-three/drei": "^6.0.5",
"@react-three/fiber": "^7.0.1",
If in recent version we'll need to subscribe position from api
, in what case will we use ref
from useSphere
?
I ended up subscribing and setting the position
and quaternion
components manually:
const [bodyRef, physicsApi] = useCompoundBody(/* ... */)
useEffect(() => {
return physicsApi.position.subscribe((v) => {
bodyRef.current.position.fromArray(v);
});
}, [physicsApi, bodyRef]);
useEffect(() => {
return physicsApi.quaternion.subscribe((v) => {
bodyRef.current.quaternion.fromArray(v);
});
}, [physicsApi, bodyRef]);
Then I could do stuff like this successfully:
otherObject.position.copy(bodyRef.current.position);
otherObject.quaternion.copy(bodyRef.current.quaternion);
🤔 Does using existing bodyRef.current.position
instead of a new pos
vector as proposed above have any disadvantages?
👋 Hi there, I was learning react-three-fiber with https://youtu.be/ZnXKmODEFHA?t=961
When I'm try to use
camera.position.copy(ref.current.position)
to move with the sphere I just created, I foundref.current.position
doesn't update.Here's my code:
and my dependency: