Rotate kinematic body based on current rotation #114
-
First of all, thanks for an excellent package! Now for my question. Is there a straight forward way to rotate a kinematic body based on its current rotation? The way that I have come up with looks like this, but it feels unnecessary complicated just because I don't have access to the body itself (only the mesh): function Box(props) {
const [ref, api] = useBox(() => ({
mass: 0,
type: 'Kinematic',
position: [0, 0, 0],
...props,
}))
useFrame(() => {
var multQuat = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0, 1, 0), 0.01)
var newQuat = ref.current.quaternion.clone().multiply(multQuat)
var newEuler = new THREE.Euler().setFromQuaternion(newQuat)
api.rotation.set(newEuler.x, newEuler.y, newEuler.z)
})
return (
<mesh ref={ref} position={[0, 0, 0]}>
<boxBufferGeometry attach="geometry" />
<meshStandardMaterial attach="material" color="#ff77aa" />
</mesh>
)
} The reason why this is not working for me: useFrame(() => {
let euler = ref.current.rotation.clone()
euler.y = euler.y + 0.01
api.rotation.set(euler.x, euler.y, euler.z)
}) is that the rotation (euler) is not "wraping" (not sure about the correct term here) on the mesh rotation, meaning that x, y and z can only have a value between -PI / 2 and PI / 2. If I had direct access to the body I would be able to simplify my example to: useFrame(() => {
let newEuler = new CANNON.Vec3()
ref.current.body.quaternion.toEuler(newEuler)
newEuler.y = newEuler.y + 0.01
api.rotation.set(newEuler.x, newEuler.y, newEuler.z)
}) Maybe there is a simple solution that I don't see, and if that is the case please enlighten me. However, if this indeed is something that isn't easily done using the current API I would propose there being the possibility to pass in a function to |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @AdamRamberg, it looks like you probably want to subscribe to the current rotation of the physics body, right? const rotation = useRef([ 0, 0, 0 ])
useEffect(() => api.rotation.subscribe((r) => (rotation.current = r)), [])
useFrame() => {
//...
rotation.current[1] += 0.01
api.rotation.set(...rotation)
}) |
Beta Was this translation helpful? Give feedback.
Hey @AdamRamberg, it looks like you probably want to subscribe to the current rotation of the physics body, right?