Skip to content

Commit 760f93f

Browse files
authored
feat: update cannon-es + add isTrigger atomic prop + add Triggers demo (#193)
1 parent dfd1f81 commit 760f93f

File tree

7 files changed

+81
-8
lines changed

7 files changed

+81
-8
lines changed

examples/src/demos/Triggers.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Physics, useBox, usePlane, useSphere } from '@react-three/cannon'
2+
import { Canvas } from '@react-three/fiber'
3+
import { OrbitControls } from '@react-three/drei'
4+
5+
function BoxTrigger({ onCollide, size, position }) {
6+
const [ref] = useBox(() => ({ isTrigger: true, args: size, position, onCollide }))
7+
return (
8+
<mesh castShadow ref={ref} position={position}>
9+
<boxBufferGeometry args={size} />
10+
<meshStandardMaterial wireframe color="green" />
11+
</mesh>
12+
)
13+
}
14+
15+
function Ball() {
16+
const [ref] = useSphere(() => ({
17+
mass: 1,
18+
position: [0, 10, 0],
19+
args: 1,
20+
}))
21+
return (
22+
<mesh castShadow receiveShadow ref={ref}>
23+
<sphereBufferGeometry args={[1, 16, 16]} />
24+
<meshLambertMaterial color="white" />
25+
</mesh>
26+
)
27+
}
28+
29+
function Plane(props) {
30+
const [ref] = usePlane(() => ({ type: 'Static', ...props }))
31+
return (
32+
<mesh ref={ref} receiveShadow>
33+
<planeBufferGeometry args={[100, 100]} />
34+
<shadowMaterial color="#171717" />
35+
</mesh>
36+
)
37+
}
38+
39+
export default () => {
40+
return (
41+
<Canvas shadows camera={{ position: [-10, 15, 5], material: { restitution: 10 }, fov: 50 }}>
42+
<color attach="background" args={['#171720']} />
43+
<ambientLight intensity={0.3} />
44+
<pointLight
45+
castShadow
46+
intensity={0.8}
47+
position={[100, 100, 100]}
48+
shadow-mapSize-width={2048}
49+
shadow-mapSize-height={2048}
50+
/>
51+
<OrbitControls />
52+
<Physics>
53+
<BoxTrigger
54+
onCollide={(e) => {
55+
console.log('Collision event on BoxTrigger', e)
56+
}}
57+
position={[0, 5, 0]}
58+
size={[4, 1, 4]}
59+
/>
60+
<Ball />
61+
<Plane rotation={[-Math.PI / 2, 0, 0]} />
62+
</Physics>
63+
</Canvas>
64+
)
65+
}

examples/src/demos/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const HingeMotor = { descr: '', tags: [], Component: lazy(() => import('./HingeM
1616
const CompoundBody = { descr: '', tags: [], Component: lazy(() => import('./CompoundBody')), bright: false }
1717
const Raycast = { descr: '', tags: [], Component: lazy(() => import('./Raycast')), bright: false }
1818
const Vehicle = { descr: '', tags: [], Component: lazy(() => import('./RaycastVehicle')), bright: false }
19+
const Triggers = { descr: '', tags: [], Component: lazy(() => import('./Triggers')), bright: false }
1920
const Trimesh = { descr: '', tags: [], Component: lazy(() => import('./Trimesh')), bright: false }
2021
const Heightfield = { descr: '', tags: [], Component: lazy(() => import('./Heightfield')), bright: false }
2122

@@ -31,6 +32,7 @@ export {
3132
CompoundBody,
3233
Raycast,
3334
Vehicle,
35+
Triggers,
3436
Trimesh,
3537
Heightfield,
3638
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"babel-eslint": "^10.1.0",
5757
"babel-loader": "^8.2.2",
5858
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
59-
"cannon-es": "^0.17.0",
59+
"cannon-es": "^0.17.1",
6060
"husky": "^6.0.0",
6161
"lebab": "^3.1.1",
6262
"prettier": "^2.2.1",

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ type AtomicProps = {
262262
collisionFilterGroup?: number
263263
collisionFilterMask?: number
264264
fixedRotation?: boolean
265+
isTrigger?: boolean
265266
}
266267
267268
type BodyProps = AtomicProps & {

src/hooks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type AtomicProps = {
1818
collisionResponse?: number
1919
fixedRotation?: boolean
2020
userData?: object
21+
isTrigger?: boolean
2122
}
2223

2324
export type BodyProps = AtomicProps & {
@@ -276,6 +277,7 @@ function useBody(
276277
collisionResponse: makeAtomic('collisionResponse', index),
277278
fixedRotation: makeAtomic('fixedRotation', index),
278279
userData: makeAtomic('userData', index),
280+
isTrigger: makeAtomic('isTrigger', index),
279281
// Apply functions
280282
applyForce(force: number[], worldPoint: number[]) {
281283
post('applyForce', index, [force, worldPoint])

src/worker.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,9 @@ self.onmessage = (e) => {
292292
case 'setFixedRotation':
293293
bodies[uuid].fixedRotation = props
294294
break
295+
case 'setIsTrigger':
296+
bodies[uuid].isTrigger = props
297+
break
295298
case 'applyForce':
296299
bodies[uuid].applyForce(new Vec3(...props[0]), new Vec3(...props[1]))
297300
break

yarn.lock

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,10 +1821,10 @@ caniuse-lite@^1.0.30001181:
18211821
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001183.tgz#7a57ba9d6584119bb5f2bc76d3cc47ba9356b3e2"
18221822
integrity sha512-7JkwTEE1hlRKETbCFd8HDZeLiQIUcl8rC6JgNjvHCNaxOeNmQ9V4LvQXRUsKIV2CC73qKxljwVhToaA3kLRqTw==
18231823

1824-
cannon-es@^0.17.0:
1825-
version "0.17.0"
1826-
resolved "https://registry.yarnpkg.com/cannon-es/-/cannon-es-0.17.0.tgz#8ddf149000d3fbb1343410b2bc40880597c2cfd5"
1827-
integrity sha512-62ASeop8hqV2E/an8ChQUOmZHO0t/h1cVFqwZ3SOEhvwZEVk4uDqPIxN5ghW3dE9RERF6SKuQ865pdvy4vD+XA==
1824+
cannon-es@^0.17.1:
1825+
version "0.17.1"
1826+
resolved "https://registry.yarnpkg.com/cannon-es/-/cannon-es-0.17.1.tgz#39d25d37e40e1a0765c96753be6c257069b214b7"
1827+
integrity sha512-MU2ylhpsCJJcZhKdTDpd7PXn1VK5nUQZo2QiHRu67527Q78mTE1XipeaU8RyTQQRxBiC+b9exPykg2+nVnMoRA==
18281828

18291829
chalk@^2.0.0:
18301830
version "2.4.2"
@@ -3628,9 +3628,9 @@ posix-character-classes@^0.1.0:
36283628
integrity sha1-AerA/jta9xoqbAL+q7jB/vfgDqs=
36293629

36303630
prettier@^2.2.1:
3631-
version "2.2.1"
3632-
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
3633-
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==
3631+
version "2.3.0"
3632+
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.3.0.tgz#b6a5bf1284026ae640f17f7ff5658a7567fc0d18"
3633+
integrity sha512-kXtO4s0Lz/DW/IJ9QdWhAf7/NmPWQXkFr/r/WkR3vyI+0v8amTDxiaQSLzs8NBlytfLWX/7uQUMIW677yLKl4w==
36343634

36353635
pretty-format@^26.6.2:
36363636
version "26.6.2"

0 commit comments

Comments
 (0)