Skip to content

Commit 782c7b0

Browse files
committed
feat(rapier): refactor adn expose context
- Use `createInjectionState` for rapier context - Updated RigidBody and collider components to use the new context structure. - Fixed collider removal logic to ensure proper context handling in various components. - Adjusted gravity settings in Physics component to utilize the new context structure.
1 parent b7bce6c commit 782c7b0

18 files changed

Lines changed: 223 additions & 98 deletions

File tree

‎apps/playground/src/pages/rapier/Colliders.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const gl = {
2020

2121
<Suspense>
2222
<Physics debug>
23-
<RigidBody>
23+
<RigidBody :collider="false">
2424
<BallCollider :args="[1, 1, 1]" :position="[2, 10, 0]" />
2525
<CapsuleCollider :args="[1, 1, 1]" :position="[-2, 10, 0]" />
2626
<ConeCollider :args="[1, 1, 1]" :position="[0, 10, 2]" />
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<script setup lang="ts">
2+
import { OrbitControls } from '@tresjs/cientos'
3+
import { TresCanvas } from '@tresjs/core'
4+
import { Physics, RigidBody } from '@tresjs/rapier'
5+
import { ACESFilmicToneMapping, SRGBColorSpace } from 'three'
6+
import { ref, watchEffect } from 'vue'
7+
8+
const gl = {
9+
clearColor: '#82DBC5',
10+
shadows: true,
11+
alpha: false,
12+
outputColorSpace: SRGBColorSpace,
13+
toneMapping: ACESFilmicToneMapping,
14+
}
15+
16+
const physicsRef = ref()
17+
18+
watchEffect(() => {
19+
if (physicsRef.value) {
20+
console.log('Physics exposed context:', physicsRef.value)
21+
}
22+
})
23+
</script>
24+
25+
<template>
26+
<TresCanvas v-bind="gl">
27+
<TresPerspectiveCamera :position="[15, 15, 15]" :look-at="[0, 0, 0]" />
28+
<OrbitControls />
29+
30+
<Suspense>
31+
<Physics ref="physicsRef" debug :gravity="[0, -9.81, 0]">
32+
<RigidBody>
33+
<TresMesh :position="[0, 8, 0]">
34+
<TresBoxGeometry />
35+
<TresMeshNormalMaterial />
36+
</TresMesh>
37+
</RigidBody>
38+
39+
<RigidBody type="fixed">
40+
<TresMesh>
41+
<TresPlaneGeometry :args="[20, 20, 20]" :rotate-x="-Math.PI / 2" />
42+
<TresMeshBasicMaterial color="#f4f4f4" />
43+
</TresMesh>
44+
</RigidBody>
45+
</Physics>
46+
</Suspense>
47+
</TresCanvas>
48+
</template>

‎apps/playground/src/router/routes/rapier/index.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ export const rapierRoutes = [
4949
name: 'Advanced Joints',
5050
component: () => import('@/pages/rapier/JointsAdvancedDemo.vue'),
5151
},
52+
{
53+
path: '/rapier/expose',
54+
name: 'Expose',
55+
component: () => import('@/pages/rapier/ExposeDemo.vue'),
56+
},
5257
]

‎packages/rapier/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
"vue": ">=3.4.0"
5555
},
5656
"dependencies": {
57-
"@dimforge/rapier3d-compat": "^0.19.3"
57+
"@dimforge/rapier3d-compat": "^0.19.3",
58+
"@vueuse/core": "catalog:utils"
5859
},
5960
"devDependencies": {
6061
"@tresjs/core": "workspace:*",

‎packages/rapier/src/components/Debug.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const lineSegmentsRef = ref<LineSegments | null>(null)
1313
onBeforeRender(() => {
1414
if (!world || !lineSegmentsRef.value?.geometry?.boundingSphere) { return }
1515
16-
const buffers = world.debugRender()
16+
const buffers = world.value.debugRender()
1717
1818
lineSegmentsRef.value.geometry.setAttribute(
1919
'position',

‎packages/rapier/src/components/InstancedRigidBody.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ onUnmounted(() => {
116116
if (!bodiesContexts.value) { return }
117117
118118
bodiesContexts.value.forEach((context) => {
119-
world.removeRigidBody(context.rigidBody)
119+
world.value.removeRigidBody(context.rigidBody)
120120
121121
context.colliders.forEach((collider) => {
122-
world.removeCollider(collider.collider, false)
122+
world.value.removeCollider(collider.collider, false)
123123
})
124124
})
125125

‎packages/rapier/src/components/Physics.vue‎

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,24 @@ const props = withDefaults(
2424
},
2525
)
2626
27-
const { world, isPaused } = await useRapierContextProvider()
27+
const context = useRapierContextProvider()!
28+
defineExpose(context)
29+
await context.init()
30+
const { world, isPaused } = context
31+
2832
2933
const setGravity = (gravity: PhysicsProps['gravity']) => {
3034
// If gravity is something like [0, -9.8, 0]
3135
if (Array.isArray(gravity)) {
32-
world.gravity.x = gravity[0]
33-
world.gravity.y = gravity[1]
34-
world.gravity.z = gravity[2]
36+
world.value.gravity.x = gravity[0]
37+
world.value.gravity.y = gravity[1]
38+
world.value.gravity.z = gravity[2]
3539
}
3640
else {
3741
const coordinates = gravity as VectorCoordinates
38-
world.gravity.x = coordinates.x
39-
world.gravity.y = coordinates.y
40-
world.gravity.z = coordinates.z
42+
world.value.gravity.x = coordinates.x
43+
world.value.gravity.y = coordinates.y
44+
world.value.gravity.z = coordinates.z
4145
}
4246
}
4347
@@ -51,15 +55,15 @@ watch(() => props.gravity, (gravity) => {
5155
const { onBeforeRender } = useLoop()
5256
5357
onBeforeRender(() => {
54-
if (!world || isPaused) { return }
58+
if (!world.value || isPaused.value) { return }
5559
if (typeof props.timestep === 'number') {
56-
world.timestep = props.timestep
60+
world.value.timestep = props.timestep
5761
}
5862
59-
world.step(eventQueue)
63+
world.value.step(eventQueue)
6064
eventQueue.drainCollisionEvents((handle1, handle2, started) => {
61-
const source1 = getSourceFromColliderHandle(world, handle1)
62-
const source2 = getSourceFromColliderHandle(world, handle2)
65+
const source1 = getSourceFromColliderHandle(world.value, handle1)
66+
const source2 = getSourceFromColliderHandle(world.value, handle2)
6367
const group1 = get3DGroupFromSource(source1, scene)
6468
const group2 = get3DGroupFromSource(source2, scene)
6569
@@ -76,7 +80,7 @@ onBeforeRender(() => {
7680
emitIntersection(
7781
{ object: group2, context: source2 },
7882
{ object: group1, context: source1 },
79-
started && world.intersectionPair(source1.collider, source2.collider),
83+
started && world.value.intersectionPair(source1.collider, source2.collider),
8084
)
8185
})
8286
})

‎packages/rapier/src/components/RigidBody.vue‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ onUpdated(() => {
152152
onUnmounted(() => {
153153
if (!bodyContext.value) { return }
154154
155-
world.removeRigidBody(bodyContext.value.rigidBody)
155+
world.value.removeRigidBody(bodyContext.value.rigidBody)
156156
157157
bodyContext.value.colliders.forEach((collider) => {
158-
world.removeCollider(collider.collider, false)
158+
world.value.removeCollider(collider.collider, false)
159159
})
160160
161161
bodyContext.value = undefined

‎packages/rapier/src/components/colliders/BaseCollider.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ watch([() => props.activeCollision, colliderInfos], ([_activeCollision]) => {
9292
onUnmounted(() => {
9393
if (!bodyContext.value || !colliderInfos.value?.collider) { return }
9494
95-
world.removeCollider(colliderInfos.value.collider, false)
95+
world.value.removeCollider(colliderInfos.value.collider, false)
9696
9797
colliderInfos.value = undefined
9898
})

‎packages/rapier/src/components/joints/BaseJoint.vue‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const joints = shallowRef<ImpulseJoint>()
2222
2323
const dispose = () => {
2424
if (joints.value) {
25-
world.removeImpulseJoint(joints.value, wakeUpOnChanges)
25+
world.value.removeImpulseJoint(joints.value, wakeUpOnChanges)
2626
joints.value = undefined
2727
}
2828
}
@@ -31,8 +31,8 @@ const setup = (bodies: JointProps['bodies'], params: JointProps['params']) => {
3131
dispose()
3232
3333
if (
34-
!(bodies?.[0] instanceof rapier.RigidBody)
35-
|| !(bodies?.[1] instanceof rapier.RigidBody)
34+
!(bodies?.[0] instanceof rapier.value.RigidBody)
35+
|| !(bodies?.[1] instanceof rapier.value.RigidBody)
3636
|| !Array.isArray(params)
3737
) {
3838
return
@@ -49,7 +49,7 @@ const setup = (bodies: JointProps['bodies'], params: JointProps['params']) => {
4949
&& (Array.isArray(params[2]) && params[2].length >= 3)
5050
&& (Array.isArray(params[3]) && params[3].length >= 4)
5151
) {
52-
jointParams = rapier.JointData.fixed(
52+
jointParams = rapier.value.JointData.fixed(
5353
new Vector3(...params[0] as VectorArray),
5454
new Quaternion(...params[1] as QuaternionArray),
5555
new Vector3(...params[2] as VectorArray),
@@ -68,7 +68,7 @@ const setup = (bodies: JointProps['bodies'], params: JointProps['params']) => {
6868
&& (Array.isArray(params[2]) && params[2].length >= 3)
6969
&& typeof params[3] === 'number'
7070
) {
71-
jointParams = rapier.JointData.generic(
71+
jointParams = rapier.value.JointData.generic(
7272
new Vector3(...params[0] as VectorArray),
7373
new Vector3(...params[1] as VectorArray),
7474
new Vector3(...params[2] as VectorArray),
@@ -86,7 +86,7 @@ const setup = (bodies: JointProps['bodies'], params: JointProps['params']) => {
8686
&& (Array.isArray(params[1]) && params[1].length >= 3)
8787
&& (Array.isArray(params[2]) && params[2].length >= 3)
8888
) {
89-
jointParams = rapier.JointData.prismatic(
89+
jointParams = rapier.value.JointData.prismatic(
9090
new Vector3(...params[0] as VectorArray),
9191
new Vector3(...params[1] as VectorArray),
9292
new Vector3(...params[1] as VectorArray),
@@ -103,7 +103,7 @@ const setup = (bodies: JointProps['bodies'], params: JointProps['params']) => {
103103
&& (Array.isArray(params[1]) && params[1].length >= 3)
104104
&& (Array.isArray(params[2]) && params[2].length >= 3)
105105
) {
106-
jointParams = rapier.JointData.revolute(
106+
jointParams = rapier.value.JointData.revolute(
107107
new Vector3(...params[0] as VectorArray),
108108
new Vector3(...params[1] as VectorArray),
109109
new Vector3(...params[2] as VectorArray),
@@ -120,7 +120,7 @@ const setup = (bodies: JointProps['bodies'], params: JointProps['params']) => {
120120
&& (Array.isArray(params[1]) && params[1].length >= 3)
121121
&& (Array.isArray(params[2]) && params[2].length >= 4)
122122
) {
123-
jointParams = rapier.JointData.rope(
123+
jointParams = rapier.value.JointData.rope(
124124
params[0],
125125
new Vector3(...params[1] as VectorArray),
126126
new Quaternion(...params[2] as QuaternionArray),
@@ -136,7 +136,7 @@ const setup = (bodies: JointProps['bodies'], params: JointProps['params']) => {
136136
&& (Array.isArray(params[0]) && params[0].length >= 3)
137137
&& (Array.isArray(params[1]) && params[1].length >= 3)
138138
) {
139-
jointParams = rapier.JointData.spherical(
139+
jointParams = rapier.value.JointData.spherical(
140140
new Vector3(...params[0] as VectorArray),
141141
new Vector3(...params[1] as VectorArray),
142142
)
@@ -154,7 +154,7 @@ const setup = (bodies: JointProps['bodies'], params: JointProps['params']) => {
154154
&& (Array.isArray(params[3]) && params[3].length >= 3)
155155
&& (Array.isArray(params[4]) && params[4].length >= 3)
156156
) {
157-
jointParams = rapier.JointData.spring(
157+
jointParams = rapier.value.JointData.spring(
158158
params[0],
159159
params[1],
160160
params[2],
@@ -174,7 +174,7 @@ const setup = (bodies: JointProps['bodies'], params: JointProps['params']) => {
174174
throw new Error(`Unsupported joint type. If you think this is a bug or the "${type}" type should be implemented, please open an issue.`)
175175
}
176176
177-
joints.value = world.createImpulseJoint(jointParams, bodies[0], bodies[1], wakeUpOnChanges)
177+
joints.value = world.value.createImpulseJoint(jointParams, bodies[0], bodies[1], wakeUpOnChanges)
178178
}
179179
180180
onUpdated(() => setup(bodies, params))

0 commit comments

Comments
 (0)