Skip to content

Commit

Permalink
Expose WakeUp & Sleep API (#230)
Browse files Browse the repository at this point in the history
* Expose Sleep / WakeUp in API

Note: I'd prefer "wake", even though it does not match cannon-es, as it's both shorter and drops the unnecessary word ("wake" being a functional verb in English alone without "up", given we're dealing in 3D spaces and international users, it may be confusing).

* Integrate sleep/wake into demo

* Update zustand usage.
  • Loading branch information
stockhuman authored Jun 28, 2021
1 parent 4753c70 commit 5154e49
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
20 changes: 16 additions & 4 deletions examples/src/demos/SphereDebug.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import React from 'react'
import React, { useState } from 'react'
import { Canvas } from '@react-three/fiber'
import { Debug, Physics, useSphere, usePlane } from '@react-three/cannon'

function ScalableBall() {
const [ref] = useSphere(() => ({
const [ref, api] = useSphere(() => ({
mass: 1,
args: 1,
position: [0, 5, 0],
}))
const [sleeping, setSleeping] = useState(false)

// Very quick demo to test forced sleep states. Catch ball mid-air to stop it.
const toggle = () => {
if (sleeping) {
setSleeping(false)
api.wakeUp()
} else {
setSleeping(true)
api.sleep()
}
}

return (
<mesh castShadow receiveShadow ref={ref}>
<mesh castShadow receiveShadow ref={ref} onClick={toggle}>
<sphereGeometry args={[1, 32, 32]} />
<meshStandardMaterial color="blue" transparent opacity={0.5} />
</mesh>
Expand All @@ -33,7 +45,7 @@ export default function App() {
<color attach="background" args={['#a6d1f6']} />
<hemisphereLight />
<directionalLight position={[5, 10, 5]} castShadow />
<Physics>
<Physics allowSleep>
<Debug scale={1.1}>
<Plane />
<ScalableBall />
Expand Down
2 changes: 1 addition & 1 deletion examples/src/demos/Trimesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Physics, useSphere, useTrimesh } from '@react-three/cannon'
import { OrbitControls, TorusKnot, useGLTF } from '@react-three/drei'
import create from 'zustand'

const [useStore] = create((set) => ({
const useStore = create((set) => ({
isPaused: false,
pause: () => set({ isPaused: true }),
play: () => set({ isPaused: false }),
Expand Down
9 changes: 9 additions & 0 deletions src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ export interface WorkerApi extends WorkerProps<AtomicProps> {
applyImpulse: (impulse: Triplet, worldPoint: Triplet) => void
applyLocalForce: (force: Triplet, localPoint: Triplet) => void
applyLocalImpulse: (impulse: Triplet, localPoint: Triplet) => void
wakeUp: () => void
sleep: () => void
}

interface PublicApi extends WorkerApi {
Expand Down Expand Up @@ -342,6 +344,13 @@ function useBody<B extends BodyProps<unknown>>(
applyLocalImpulse(impulse: Triplet, localPoint: Triplet) {
post(ref, worker, 'applyLocalImpulse', index, [impulse, localPoint])
},
// force particular sleep state
wakeUp() {
post(ref, worker, 'wakeUp', index)
},
sleep() {
post(ref, worker, 'sleep', index)
},
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,5 +491,14 @@ self.onmessage = (e) => {
state.vehicles[uuid].setBrake(brake, wheelIndex)
break
}

case 'wakeUp': {
state.bodies[uuid].wakeUp()
break
}
case 'sleep': {
state.bodies[uuid].sleep()
break
}
}
}

0 comments on commit 5154e49

Please sign in to comment.