Skip to content

Commit ed5ad2a

Browse files
authored
split up debug & physics context / providers (#362)
## @react-three/cannon - Access the physics context with the `usePhysicsContext` hook, which immediately gives you a clear error message when trying to access physics components or hooks outside of a Physics provider. - Renamed `context` to `physicsContext` - Added a `useDebugContext` hook for consistency - [`Provider.tsx`] Renamed to `physics-provider.tsx` - [`Debug.tsx`] Renamed to `debug-provider.tsx` (Still exported as Debug & DebugProps) - [`physics-provider.tsx`] One useState call that contains the whole context - [`physics-provider.tsx`] bodies is not a ref, no need to access current - [`setup.ts`] Removed, split into more appropriately named modules - [`worker.d.ts`] Removed, belongs in cannon-worker-api - [`package.json`] Use dependencies rather than peerDependencies - [`.eslintrc.json`] Clean up - [`.eslintrc.json`] Disallow non-null assertions ## @pmndrs/cannon-worker-api - [WorkerRayHitEvent] from & to are optional - [WorkerRayHitEvent] Omit methods from shape type - [`src/worker`] self.postMessage should be typed to ensure we match API types - [`src/worker`] Stop using non-null assertions - [`addBodies`] If the body or target does not have a uuid, don't process the event - [`addRay`] If the body does not have a uuid, don't process the event - [`package.json`] Has one dependency: `three` - [`.eslintrc.json`] Clean up - [`.eslintrc.json`] Disallow non-null assertions ## @react-three/cannon-examples - [MondayMorning] Don't use non-null assertions - [`.eslintrc.json`] Clean up - [`.eslintrc.json`] Disallow non-null assertions - [`package.json`] Only devDependencies - [`tsconfig.json`] Alphabetize ## @pmndrs/use-cannon - [`package.json`] Add .json, .jsx, & .tsx to lint-staged files - [`yarn.lock`] Updated dependencies
1 parent a28c6c8 commit ed5ad2a

File tree

27 files changed

+579
-605
lines changed

27 files changed

+579
-605
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"npm": ">=8"
2828
},
2929
"lint-staged": {
30-
"*.{js,ts}": "eslint --cache --fix",
31-
"*.{js,ts,md}": "prettier --write"
30+
"*.{js,jsx,ts,tsx}": "eslint --cache --fix",
31+
"*.{js,jsx,json,md,ts,tsx}": "prettier --write"
3232
},
3333
"scripts": {
3434
"build": "npm run build --workspaces",

packages/cannon-worker-api/.eslintrc.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"browser": true,
44
"es2021": true
55
},
6-
"extends": ["prettier", "eslint:recommended"],
6+
"extends": ["eslint:recommended", "prettier"],
77
"overrides": [
88
{
99
"extends": "plugin:@typescript-eslint/recommended",
@@ -56,7 +56,7 @@
5656
}
5757
],
5858
"@typescript-eslint/no-namespace": ["error", { "allowDeclarations": true }],
59-
"@typescript-eslint/no-non-null-assertion": "off",
59+
"@typescript-eslint/no-non-null-assertion": "error",
6060
"@typescript-eslint/no-unused-vars": ["error", { "ignoreRestSiblings": true }],
6161
"@typescript-eslint/quotes": [
6262
"error",
@@ -69,9 +69,6 @@
6969
"@typescript-eslint/semi": ["error", "never"],
7070
"typescript-enum/no-enum": "error"
7171
}
72-
},
73-
{
74-
"files": ["*.jsx", "*.js"]
7572
}
7673
],
7774
"parserOptions": {

packages/cannon-worker-api/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@
2929
"prettier-fix": "prettier --write ."
3030
},
3131
"dependencies": {
32-
"cannon-es": "^0.19.0",
33-
"events": "^3.3.0",
3432
"three": "^0.137.0"
3533
},
3634
"devDependencies": {
@@ -45,11 +43,13 @@
4543
"@types/three": "^0.137.0",
4644
"@typescript-eslint/eslint-plugin": "^5.7.0",
4745
"@typescript-eslint/parser": "^5.7.0",
46+
"cannon-es": "^0.19.0",
4847
"eslint": "^8.5.0",
4948
"eslint-config-prettier": "^8.3.0",
5049
"eslint-plugin-es": "^4.1.0",
5150
"eslint-plugin-simple-import-sort": "^7.0.0",
5251
"eslint-plugin-typescript-enum": "^2.1.0",
52+
"events": "^3.3.0",
5353
"prettier": "^2.5.1",
5454
"rollup": "^2.61.1",
5555
"rollup-plugin-web-worker-loader": "^1.6.1",

packages/cannon-worker-api/rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import pluginCommonjs from '@rollup/plugin-commonjs'
33
import pluginNodeResolve from '@rollup/plugin-node-resolve'
44
import pluginWebWorker from 'rollup-plugin-web-worker-loader'
55

6+
// These are our dependencies, everything else is in the bundle
67
const external = ['three']
78
const extensions = ['.js', '.ts', '.json']
89

packages/cannon-worker-api/src/types.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,18 @@ export type WorkerRayhitEvent = {
245245
collisionFilterGroup: number
246246
collisionFilterMask: number
247247
direction: number[]
248-
from: number[]
249-
to: number[]
248+
from?: Triplet
249+
to?: Triplet
250250
uuid: string
251251
}
252-
rayFromWorld: number[]
253-
rayToWorld: number[]
254-
shape: (Omit<Shape, 'body'> & { body: string }) | null
252+
rayFromWorld: Triplet
253+
rayToWorld: Triplet
254+
shape:
255+
| (Omit<
256+
Shape,
257+
'body' | 'updateBoundingSphereRadius' | 'volume' | 'calculateLocalInertia' | 'calculateWorldAABB'
258+
> & { body: string })
259+
| null
255260
shouldStop: boolean
256261
type: 'rayhit'
257262
}

packages/cannon-worker-api/src/worker/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ import { addBodies, addConstraint, addRay, addRaycastVehicle, addSpring, init, s
1111
import { state } from './state'
1212
import type { CannonWorkerGlobalScope } from './types'
1313

14+
// TODO: Declare this for all files in worker
1415
declare const self: CannonWorkerGlobalScope
1516

1617
const isHingeConstraint = (c: unknown): c is HingeConstraint => c instanceof HingeConstraint
1718

1819
function syncBodies() {
1920
state.bodiesNeedSyncing = true
20-
state.bodies = state.world.bodies.reduce((bodies, body) => ({ ...bodies, [body.uuid!]: body }), {})
21+
state.bodies = state.world.bodies.reduce(
22+
(bodies, body) => (body.uuid ? { ...bodies, [body.uuid]: body } : bodies),
23+
{},
24+
)
2125
}
2226

2327
const broadphases = { NaiveBroadphase, SAPBroadphase }

packages/cannon-worker-api/src/worker/operations/add-bodies.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { propsToBody } from '../../props-to-body'
22
import type { CannonMessageMap } from '../../types'
33
import type { CreateMaterial } from '../material'
44
import type { State } from '../state'
5-
import type { CannonCollideEvent } from '../types'
5+
import type { CannonCollideEvent, CannonWorkerGlobalScope } from '../types'
6+
7+
declare const self: CannonWorkerGlobalScope
68

79
export const addBodies = (
810
state: State,
@@ -20,11 +22,14 @@ export const addBodies = (
2022

2123
if (props[i].onCollide)
2224
body.addEventListener('collide', ({ type, body, target, contact }: CannonCollideEvent) => {
25+
if (!body.uuid || !target.uuid) return
26+
2327
const { ni, ri, rj, bi, bj, id } = contact
2428
const contactPoint = bi.position.vadd(ri)
2529
const contactNormal = bi === body ? ni : ni.scale(-1)
30+
2631
self.postMessage({
27-
body: body.uuid!,
32+
body: body.uuid,
2833
collisionFilters: {
2934
bodyFilterGroup: body.collisionFilterGroup,
3035
bodyFilterMask: body.collisionFilterMask,
@@ -47,7 +52,7 @@ export const addBodies = (
4752
rj: rj.toArray(),
4853
},
4954
op: 'event',
50-
target: target.uuid!,
55+
target: target.uuid,
5156
type,
5257
})
5358
})

packages/cannon-worker-api/src/worker/operations/add-ray.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { Ray, RAY_MODES, RaycastResult } from 'cannon-es'
44
import type { CannonMessageMap } from '../../types'
55
import type { State } from '../state'
66
import { tripletToVec3 } from '../triplet-to-vec3'
7-
import type { WithUUID } from '../types'
7+
import type { CannonWorkerGlobalScope, WithUUID } from '../types'
8+
9+
declare const self: CannonWorkerGlobalScope
810

911
function toUppercase<T extends string>(str: T): Uppercase<T> {
1012
return str.toUpperCase() as Uppercase<T>
@@ -31,8 +33,10 @@ export const addRay = (
3133

3234
const bodyUUID = (body as WithUUID<Body>).uuid
3335

36+
if (!bodyUUID) return
37+
3438
self.postMessage({
35-
body: bodyUUID || null,
39+
body: bodyUUID,
3640
hitNormalWorld: hitNormalWorld.toArray(),
3741
hitPointWorld: hitPointWorld.toArray(),
3842
op: 'event',

packages/cannon-worker-api/src/worker/operations/init.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@ import { GSSolver, NaiveBroadphase, SAPBroadphase, SplitSolver } from 'cannon-es
33

44
import type { CannonMessageProps } from '../../types'
55
import type { State } from '../state'
6-
import type { WithUUID } from '../types'
6+
import type { CannonWorkerGlobalScope, WithUUID } from '../types'
7+
8+
declare const self: CannonWorkerGlobalScope
79

810
type TwoBodies = {
911
bodyA?: WithUUID<Body>
1012
bodyB?: WithUUID<Body>
1113
}
1214

1315
function emitBeginContact({ bodyA, bodyB }: TwoBodies) {
14-
if (!bodyA || !bodyB) return
16+
if (!bodyA?.uuid || !bodyB?.uuid) return
1517
self.postMessage({ bodyA: bodyA.uuid, bodyB: bodyB.uuid, op: 'event', type: 'collideBegin' })
1618
}
1719

1820
function emitEndContact({ bodyA, bodyB }: TwoBodies) {
19-
if (!bodyA || !bodyB) return
21+
if (!bodyA?.uuid || !bodyB?.uuid) return
2022
self.postMessage({ bodyA: bodyA.uuid, bodyB: bodyB.uuid, op: 'event', type: 'collideEnd' })
2123
}
2224

packages/cannon-worker-api/src/worker/operations/step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { CannonMessageMap, Observation, PropValue, WorkerFrameMessage } fro
44
import type { State } from '../state'
55
import type { CannonWorkerGlobalScope } from '../types'
66

7-
declare let self: CannonWorkerGlobalScope
7+
declare const self: CannonWorkerGlobalScope
88

99
const isQorV = (v: unknown): v is Quaternion | Vec3 => v instanceof Quaternion || v instanceof Vec3
1010

0 commit comments

Comments
 (0)