Skip to content

Commit 90dfc12

Browse files
committed
@pmndrs/use-cannon - v2.3.0 - 2022-04-18
1 parent c2fb439 commit 90dfc12

33 files changed

+492
-362
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# @pmndrs/use-cannon Changelog
22

3+
## v2.3.0 - 2022-04-18
4+
5+
- [@react-three/cannon] v6.3.0
6+
- [@react-three/cannon-examples] v2.3.0
7+
38
## v2.2.0 - 2022-04-08
49

510
- [@react-three/cannon] v6.2.0

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pmndrs/use-cannon",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"description": "monorepo for @pmndrs/use-cannon",
55
"keywords": [
66
"cannon",

packages/react-three-cannon-examples/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @react-three/cannon-examples Changelog
22

3+
## v2.3.0 - 2022-04-18
4+
5+
- Use accurate ref types for all hooks (@bjornstar)
6+
- Prefer `PropsWithChildren` to `FC` (@bjornstar)
7+
- Update `@types/react` to v18 (@bjornstar)
8+
- Update `@react-three/drei` & `@react-three/fiber` (@bjornstar)
9+
- Update `styled-components` (@bjornstar)
10+
311
## v2.2.0 - 2022-04-08
412

513
- Use `react-dom/client` for react 18 features

packages/react-three-cannon-examples/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@react-three/cannon-examples",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"description": "Examples for @react-three/cannon",
55
"private": true,
66
"scripts": {
@@ -21,13 +21,13 @@
2121
],
2222
"devDependencies": {
2323
"@react-three/cannon": "^6.2.0",
24-
"@react-three/drei": "^9.0.1",
25-
"@react-three/fiber": "^8.0.4",
24+
"@react-three/drei": "^9.4.3",
25+
"@react-three/fiber": "^8.0.11",
2626
"@types/lodash-es": "^4.17.6",
27-
"@types/react": "^17.0.43",
27+
"@types/react": "^18.0.5",
2828
"@types/react-dom": "^17.0.14",
2929
"@types/react-router-dom": "^5.3.3",
30-
"@types/styled-components": "^5.1.24",
30+
"@types/styled-components": "^5.1.25",
3131
"@types/three": "^0.139.0",
3232
"@typescript-eslint/eslint-plugin": "^5.17.0",
3333
"@typescript-eslint/parser": "^5.17.0",

packages/react-three-cannon-examples/src/App.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { HashRouter as Router, Link, Route, Routes, useMatch } from 'react-route
33
import styled from 'styled-components'
44

55
import { demoList, demos, isDemo } from './demos'
6-
import { Global } from './styles'
7-
import { Page as PageImpl } from './styles'
6+
import { GlobalStyle, PageStyle } from './styles'
87

9-
const Page = styled(PageImpl)`
8+
const Page = styled(PageStyle)`
109
padding: 0px;
1110
1211
& > h1 {
@@ -71,7 +70,8 @@ function Demos() {
7170
export default function App() {
7271
return (
7372
<Router>
74-
<Global />
73+
{/* @ts-expect-error Not sure how to fix the type here */}
74+
<GlobalStyle />
7575
<Intro />
7676
</Router>
7777
)

packages/react-three-cannon-examples/src/demos/MondayMorning/index.tsx

Lines changed: 77 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ import {
99
usePointToPointConstraint,
1010
useSphere,
1111
} from '@react-three/cannon'
12-
import type { BoxBufferGeometryProps, MeshProps, MeshStandardMaterialProps } from '@react-three/fiber'
12+
import type {
13+
BoxBufferGeometryProps,
14+
MeshProps,
15+
MeshStandardMaterialProps,
16+
ThreeEvent,
17+
} from '@react-three/fiber'
1318
import { Canvas, useFrame, useLoader } from '@react-three/fiber'
14-
import type { FC, ReactNode, RefObject } from 'react'
19+
import type { ReactNode, RefObject } from 'react'
1520
import {
1621
createContext,
1722
createRef,
@@ -23,7 +28,7 @@ import {
2328
useMemo,
2429
useRef,
2530
} from 'react'
26-
import type { Material, Mesh, Object3D } from 'three'
31+
import type { Group, Material, Mesh, Object3D, SpotLight } from 'three'
2732
import type { GLTF } from 'three-stdlib/loaders/GLTFLoader'
2833
import { GLTFLoader } from 'three-stdlib/loaders/GLTFLoader'
2934

@@ -32,16 +37,17 @@ import { createRagdoll } from './createConfig'
3237

3338
const { joints, shapes } = createRagdoll(4.8, Math.PI / 16, Math.PI / 16, 0)
3439
const context = createContext<RefObject<Object3D>>(createRef<Object3D>())
35-
const cursor = createRef<Object3D>()
40+
const cursor = createRef<Mesh>()
3641

3742
const double = ([x, y, z]: Readonly<Triplet>): Triplet => [x * 2, y * 2, z * 2]
3843

3944
function useDragConstraint(child: RefObject<Object3D>) {
4045
const [, , api] = usePointToPointConstraint(cursor, child, { pivotA: [0, 0, 0], pivotB: [0, 0, 0] })
4146
// TODO: make it so we can start the constraint with it disabled
4247
useEffect(() => void api.disable(), [])
43-
const onPointerDown = useCallback((e) => {
48+
const onPointerDown = useCallback((e: ThreeEvent<PointerEvent>) => {
4449
e.stopPropagation()
50+
//@ts-expect-error Investigate proper types here.
4551
e.target.setPointerCapture(e.pointerId)
4652
api.enable()
4753
}, [])
@@ -53,7 +59,7 @@ type BoxProps = Omit<MeshProps, 'args'> &
5359
Pick<BoxBufferGeometryProps, 'args'> &
5460
Pick<MeshStandardMaterialProps, 'color' | 'opacity' | 'transparent'>
5561

56-
const Box = forwardRef<Object3D | undefined, BoxProps>(
62+
const Box = forwardRef<Mesh, BoxProps>(
5763
({ args = [1, 1, 1], children, color = 'white', opacity = 1, transparent = false, ...props }, ref) => {
5864
return (
5965
<mesh castShadow receiveShadow ref={ref} {...props}>
@@ -71,11 +77,14 @@ type BodyPartProps = BoxProps & {
7177
render?: ReactNode
7278
}
7379

74-
const BodyPart: FC<BodyPartProps> = ({ children, config = {}, name, render, ...props }) => {
80+
function BodyPart({ children, config = {}, name, render, ...props }: BodyPartProps): JSX.Element {
7581
const { color, args, mass, position } = shapes[name]
7682
const scale = useMemo<Triplet>(() => double(args), [args])
7783
const parent = useContext(context)
78-
const [ref] = useBox(() => ({ args: [...args], linearDamping: 0.99, mass, position: [...position] }))
84+
const [ref] = useBox(
85+
() => ({ args: [...args], linearDamping: 0.99, mass, position: [...position] }),
86+
useRef<Mesh>(null),
87+
)
7988
useConeTwistConstraint(ref, parent, config)
8089
const bind = useDragConstraint(ref)
8190
return (
@@ -89,8 +98,8 @@ const BodyPart: FC<BodyPartProps> = ({ children, config = {}, name, render, ...p
8998
}
9099

91100
function Ragdoll(props: Pick<MeshProps, 'position'>) {
92-
const mouth = useRef<Object3D>(null)
93-
const eyes = useRef<Object3D>(null)
101+
const mouth = useRef<Mesh>(null)
102+
const eyes = useRef<Group>(null)
94103

95104
useFrame(({ clock }) => {
96105
if (!eyes.current || !mouth.current) return
@@ -151,7 +160,7 @@ function Ragdoll(props: Pick<MeshProps, 'position'>) {
151160
}
152161

153162
function Plane(props: PlaneProps) {
154-
const [ref] = usePlane(() => ({ ...props }))
163+
const [ref] = usePlane(() => ({ ...props }), useRef<Mesh>(null))
155164
return (
156165
<mesh ref={ref} receiveShadow>
157166
<planeBufferGeometry args={[1000, 1000]} />
@@ -161,19 +170,22 @@ function Plane(props: PlaneProps) {
161170
}
162171

163172
function Chair() {
164-
const [ref] = useCompoundBody(() => ({
165-
mass: 1,
166-
position: [-6, 0, 0],
167-
shapes: [
168-
{ args: [1.5, 1.5, 0.25], mass: 1, position: [0, 0, 0], type: 'Box' },
169-
{ args: [1.5, 0.25, 1.5], mass: 1, position: [0, -1.75, 1.25], type: 'Box' },
170-
{ args: [0.25, 1.5, 0.25], mass: 10, position: [5 + -6.25, -3.5, 0], type: 'Box' },
171-
{ args: [0.25, 1.5, 0.25], mass: 10, position: [5 + -3.75, -3.5, 0], type: 'Box' },
172-
{ args: [0.25, 1.5, 0.25], mass: 10, position: [5 + -6.25, -3.5, 2.5], type: 'Box' },
173-
{ args: [0.25, 1.5, 0.25], mass: 10, position: [5 + -3.75, -3.5, 2.5], type: 'Box' },
174-
],
175-
type: 'Dynamic',
176-
}))
173+
const [ref] = useCompoundBody(
174+
() => ({
175+
mass: 1,
176+
position: [-6, 0, 0],
177+
shapes: [
178+
{ args: [1.5, 1.5, 0.25], mass: 1, position: [0, 0, 0], type: 'Box' },
179+
{ args: [1.5, 0.25, 1.5], mass: 1, position: [0, -1.75, 1.25], type: 'Box' },
180+
{ args: [0.25, 1.5, 0.25], mass: 10, position: [5 + -6.25, -3.5, 0], type: 'Box' },
181+
{ args: [0.25, 1.5, 0.25], mass: 10, position: [5 + -3.75, -3.5, 0], type: 'Box' },
182+
{ args: [0.25, 1.5, 0.25], mass: 10, position: [5 + -6.25, -3.5, 2.5], type: 'Box' },
183+
{ args: [0.25, 1.5, 0.25], mass: 10, position: [5 + -3.75, -3.5, 2.5], type: 'Box' },
184+
],
185+
type: 'Dynamic',
186+
}),
187+
useRef<Group>(null),
188+
)
177189
const bind = useDragConstraint(ref)
178190
return (
179191
<group ref={ref} {...bind}>
@@ -200,12 +212,15 @@ interface CupGLTF extends GLTF {
200212

201213
function Mug() {
202214
const { nodes, materials } = useLoader(GLTFLoader, '/cup.glb') as CupGLTF
203-
const [ref] = useCylinder(() => ({
204-
args: [0.6, 0.6, 1, 16],
205-
mass: 1,
206-
position: [9, 0, 0],
207-
rotation: [Math.PI / 2, 0, 0],
208-
}))
215+
const [ref] = useCylinder(
216+
() => ({
217+
args: [0.6, 0.6, 1, 16],
218+
mass: 1,
219+
position: [9, 0, 0],
220+
rotation: [Math.PI / 2, 0, 0],
221+
}),
222+
useRef<Group>(null),
223+
)
209224
const bind = useDragConstraint(ref)
210225
return (
211226
<group ref={ref} {...bind} dispose={null}>
@@ -228,11 +243,26 @@ function Mug() {
228243
}
229244

230245
function Table() {
231-
const [seat] = useBox(() => ({ args: [2.5, 0.25, 2.5], position: [9, -0.8, 0], type: 'Static' }))
232-
const [leg1] = useBox(() => ({ args: [0.25, 2, 0.25], position: [7.2, -3, 1.8], type: 'Static' }))
233-
const [leg2] = useBox(() => ({ args: [0.25, 2, 0.25], position: [10.8, -3, 1.8], type: 'Static' }))
234-
const [leg3] = useBox(() => ({ args: [0.25, 2, 0.25], position: [7.2, -3, -1.8], type: 'Static' }))
235-
const [leg4] = useBox(() => ({ args: [0.25, 2, 0.25], position: [10.8, -3, -1.8], type: 'Static' }))
246+
const [seat] = useBox(
247+
() => ({ args: [2.5, 0.25, 2.5], position: [9, -0.8, 0], type: 'Static' }),
248+
useRef<Mesh>(null),
249+
)
250+
const [leg1] = useBox(
251+
() => ({ args: [0.25, 2, 0.25], position: [7.2, -3, 1.8], type: 'Static' }),
252+
useRef<Mesh>(null),
253+
)
254+
const [leg2] = useBox(
255+
() => ({ args: [0.25, 2, 0.25], position: [10.8, -3, 1.8], type: 'Static' }),
256+
useRef<Mesh>(null),
257+
)
258+
const [leg3] = useBox(
259+
() => ({ args: [0.25, 2, 0.25], position: [7.2, -3, -1.8], type: 'Static' }),
260+
useRef<Mesh>(null),
261+
)
262+
const [leg4] = useBox(
263+
() => ({ args: [0.25, 2, 0.25], position: [10.8, -3, -1.8], type: 'Static' }),
264+
useRef<Mesh>(null),
265+
)
236266
return (
237267
<>
238268
<Box scale={[5, 0.5, 5]} ref={seat} />
@@ -248,15 +278,18 @@ function Table() {
248278
}
249279

250280
const Lamp = () => {
251-
const light = useRef()
252-
const [fixed] = useSphere(() => ({ args: [1], position: [0, 16, 0], type: 'Static' }))
253-
const [lamp] = useBox(() => ({
254-
angulardamping: 1.99,
255-
args: [1, 0, 5],
256-
linearDamping: 0.9,
257-
mass: 1,
258-
position: [0, 16, 0],
259-
}))
281+
const light = useRef<SpotLight>(null)
282+
const [fixed] = useSphere(() => ({ args: [1], position: [0, 16, 0], type: 'Static' }), useRef<Mesh>(null))
283+
const [lamp] = useBox(
284+
() => ({
285+
angulardamping: 1.99,
286+
args: [1, 0, 5],
287+
linearDamping: 0.9,
288+
mass: 1,
289+
position: [0, 16, 0],
290+
}),
291+
useRef<Mesh>(null),
292+
)
260293
usePointToPointConstraint(fixed, lamp, { pivotA: [0, 0, 0], pivotB: [0, 2, 0] })
261294
const bind = useDragConstraint(lamp)
262295
return (

packages/react-three-cannon-examples/src/demos/Pingpong/Text.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { GroupProps } from '@react-three/fiber'
2-
import { forwardRef, useMemo } from 'react'
3-
import type { Object3D } from 'three'
2+
import { useMemo } from 'react'
43
import { TextGeometry } from 'three/examples/jsm/geometries/TextGeometry'
54
import { FontLoader } from 'three/examples/jsm/loaders/FontLoader'
65

@@ -16,10 +15,10 @@ type TextProps = GroupProps & {
1615
count: string
1716
}
1817

19-
const Text = forwardRef<Object3D, TextProps>(({ color = 'white', count, ...props }, ref) => {
18+
export default function Text({ color = 'white', count, ...props }: TextProps): JSX.Element {
2019
const array = useMemo(() => [...count], [count])
2120
return (
22-
<group ref={ref} {...props} dispose={null}>
21+
<group {...props} dispose={null}>
2322
{array.map((char, index) => (
2423
<mesh
2524
position={[-(array.length / 2) * 3.5 + index * 3.5, 0, 0]}
@@ -31,6 +30,4 @@ const Text = forwardRef<Object3D, TextProps>(({ color = 'white', count, ...props
3130
))}
3231
</group>
3332
)
34-
})
35-
36-
export default Text
33+
}

packages/react-three-cannon-examples/src/demos/Pingpong/index.tsx

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Canvas, useFrame, useLoader } from '@react-three/fiber'
33
import lerp from 'lerp'
44
import clamp from 'lodash-es/clamp'
55
import { Suspense, useRef } from 'react'
6-
import type { Loader, Material, Mesh, Object3D, Skeleton } from 'three'
6+
import type { Group, Loader, Material, Mesh, Object3D, Skeleton } from 'three'
77
import { TextureLoader } from 'three'
88
import { DRACOLoader } from 'three-stdlib/loaders/DRACOLoader'
99
import type { GLTF } from 'three-stdlib/loaders/GLTFLoader'
@@ -61,12 +61,15 @@ function Paddle() {
6161
const { pong } = useStore((state) => state.api)
6262
const welcome = useStore((state) => state.welcome)
6363
const count = useStore((state) => state.count)
64-
const model = useRef<Object3D>(null)
65-
const [ref, api] = useBox(() => ({
66-
args: [3.4, 1, 3],
67-
onCollide: (e) => pong(e.contact.impactVelocity),
68-
type: 'Kinematic',
69-
}))
64+
const model = useRef<Group>(null)
65+
const [ref, api] = useBox(
66+
() => ({
67+
args: [3.4, 1, 3],
68+
onCollide: (e) => pong(e.contact.impactVelocity),
69+
type: 'Kinematic',
70+
}),
71+
useRef<Mesh>(null),
72+
)
7073
const values = useRef([0, 0])
7174
useFrame((state) => {
7275
values.current[0] = lerp(values.current[0], (state.mouse.x * Math.PI) / 5, 0.2)
@@ -110,7 +113,7 @@ function Paddle() {
110113

111114
function Ball() {
112115
const map = useLoader(TextureLoader, earthImg)
113-
const [ref] = useSphere(() => ({ args: [0.5], mass: 1, position: [0, 5, 0] }))
116+
const [ref] = useSphere(() => ({ args: [0.5], mass: 1, position: [0, 5, 0] }), useRef<Mesh>(null))
114117
return (
115118
<mesh castShadow ref={ref}>
116119
<sphereBufferGeometry args={[0.5, 64, 64]} />
@@ -121,12 +124,15 @@ function Ball() {
121124

122125
function ContactGround() {
123126
const { reset } = useStore((state) => state.api)
124-
const [ref] = usePlane(() => ({
125-
onCollide: () => reset(true),
126-
position: [0, -10, 0],
127-
rotation: [-Math.PI / 2, 0, 0],
128-
type: 'Static',
129-
}))
127+
const [ref] = usePlane(
128+
() => ({
129+
onCollide: () => reset(true),
130+
position: [0, -10, 0],
131+
rotation: [-Math.PI / 2, 0, 0],
132+
type: 'Static',
133+
}),
134+
useRef<Mesh>(null),
135+
)
130136
return <mesh ref={ref} />
131137
}
132138

0 commit comments

Comments
 (0)