Skip to content

Commit b64877c

Browse files
fix(rapier): fix automatic colliders reactive props
1 parent 04a5898 commit b64877c

3 files changed

Lines changed: 29 additions & 8 deletions

File tree

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { TresCanvas } from '@tresjs/core'
44
import { Physics, RigidBody } from '@tresjs/rapier'
55
import { ACESFilmicToneMapping, SRGBColorSpace } from 'three'
66
import { TresLeches, useControls } from '@tresjs/leches'
7+
import type { ExposedRigidBody } from '@tresjs/rapier'
78
89
const gl = {
910
clearColor: '#82DBC5',
@@ -24,9 +25,9 @@ const jump = () => {
2425
const { debug, friction, mass, restitution, density } = useControls({
2526
debug: true,
2627
friction: { value: 0.5, min: 0, max: 1, step: 0.1 },
27-
mass: { value: 1, min: 0.1, max: 100, step: 0.1 },
28-
restitution: { value: 0.5, min: 0, max: 1, step: 0.1 },
29-
density: { value: 1, min: 0.1, max: 100, step: 0.1 },
28+
mass: { value: 1, min: 0.1, max: 20, step: 0.1 },
29+
restitution: { value: 0.5, min: 0, max: 2, step: 0.1 },
30+
density: { value: 1, min: 0.1, max: 20, step: 0.1 },
3031
})
3132
</script>
3233

@@ -37,15 +38,15 @@ const { debug, friction, mass, restitution, density } = useControls({
3738
<OrbitControls />
3839

3940
<Suspense>
40-
<Physics :debug="debug">
41+
<Physics :debug>
4142
<RigidBody
4243
ref="colliderRef"
4344
collider="ball"
4445
:position="[0, 15, 0]"
45-
:friction="friction"
46-
:mass="mass"
47-
:restitution="restitution"
48-
:density="density"
46+
:friction
47+
:mass
48+
:restitution
49+
:density
4950
>
5051
<TresMesh @click="jump">
5152
<TresSphereGeometry />

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from '@tresjs/rapier'
1313
import { ACESFilmicToneMapping, SRGBColorSpace } from 'three'
1414
import { TresLeches, useControls } from '@tresjs/leches'
15+
import type { ExposedRigidBody } from '@tresjs/rapier'
1516
1617
const gl = {
1718
clearColor: '#82DBC5',

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,25 @@ makePropsWatcherRB(props, [
125125
'enabledTranslations',
126126
], instance)
127127
128+
// reactively set autoColliderProps
129+
const setAutoColliderProp = <K extends keyof ColliderProps>(prop: K, value: ColliderProps[K]) => {
130+
if (autoColliderProps.value.length === 0 || props.collider === false) { return }
131+
132+
autoColliderProps.value.forEach((_props) => {
133+
_props[prop] = value
134+
})
135+
}
136+
137+
// Automatic collider props watchers
138+
watch(() => props.friction, value => setAutoColliderProp('friction', value))
139+
watch(() => props.mass, value => setAutoColliderProp('mass', value))
140+
watch(() => props.restitution, value => setAutoColliderProp('restitution', value))
141+
watch(() => props.density, value => setAutoColliderProp('density', value))
142+
watch(() => props.activeCollision, value => setAutoColliderProp('activeCollision', value))
143+
watch(() => props.activeCollisionTypes, value => setAutoColliderProp('activeCollisionTypes', value))
144+
watch(() => props.collisionGroups, value => setAutoColliderProp('collisionGroups', value))
145+
watch(() => props.sensor, value => setAutoColliderProp('sensor', value))
146+
128147
watch([() => props.lockTranslations, instance], ([_lockTranslations, _]) => {
129148
if (!instance.value) { return }
130149
instance.value.lockTranslations(_lockTranslations, true)

0 commit comments

Comments
 (0)