-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app): 105 First stage of Collision event
- Loading branch information
1 parent
1e2d7e3
commit 0bfbb5b
Showing
8 changed files
with
235 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<script setup lang="ts"> | ||
import { OrbitControls } from '@tresjs/cientos' | ||
import { TresCanvas } from '@tresjs/core' | ||
import { BallCollider, Physics, RigidBody } from '@tresjs/rapier' | ||
import { ACESFilmicToneMapping, SRGBColorSpace } from 'three' | ||
import { shallowRef } from 'vue' | ||
const gl = { | ||
clearColor: '#82DBC5', | ||
shadows: true, | ||
alpha: false, | ||
outputColorSpace: SRGBColorSpace, | ||
toneMapping: ACESFilmicToneMapping, | ||
} | ||
const rigidTorusRef = shallowRef() | ||
const ballRef = shallowRef() | ||
const jump = () => { | ||
if (!rigidTorusRef.value) { return } | ||
rigidTorusRef.value.instance.applyImpulse({ x: 0, y: 5, z: 0 }, true) | ||
} | ||
const jumpBall = () => { | ||
if (!ballRef.value) { return } | ||
ballRef.value.instance.applyImpulse({ x: 0, y: 5, z: 0 }, true) | ||
} | ||
const onCollisionEnter = (event: any) => { | ||
// eslint-disable-next-line no-console | ||
console.log('jaime ~ ENTER', event) | ||
} | ||
const onCollisionEnterBall = (event: any) => { | ||
// eslint-disable-next-line no-console | ||
console.log('jaime ~ ENTER', event.source.rapierGroup.collider.collisionGroups()) | ||
} | ||
const onCollisionExit = (event: any) => { | ||
// eslint-disable-next-line no-console | ||
console.log('jaime ~ EXIT', event) | ||
} | ||
</script> | ||
|
||
<template> | ||
<TresCanvas v-bind="gl" window-size> | ||
<TresPerspectiveCamera :position="[0, 11, 30]" :look-at="[0, 0, 0]" /> | ||
<OrbitControls /> | ||
|
||
<Suspense> | ||
<Physics debug> | ||
<RigidBody | ||
ref="rigidTorusRef" | ||
name="rigidTorus" | ||
enableCcd | ||
activeCollision | ||
@collision-enter="onCollisionEnter" | ||
@collision-exit="onCollisionExit" | ||
> | ||
<TresMesh :position="[0, 8, 0]" @click="jump"> | ||
<TresTorusGeometry /> | ||
<TresMeshNormalMaterial /> | ||
</TresMesh> | ||
<TresMesh :position="[-5, 7, 0]"> | ||
<TresSphereGeometry /> | ||
<TresMeshNormalMaterial /> | ||
</TresMesh> | ||
</RigidBody> | ||
|
||
<RigidBody | ||
@collision-enter="onCollisionEnterBall" | ||
> | ||
<BallCollider | ||
ref="ballRef" | ||
activeCollision | ||
:args="[1, 1, 1]" | ||
:position="[8, 15, 0]" | ||
@click="jumpBall" | ||
/> | ||
</RigidBody> | ||
|
||
<RigidBody type="fixed" name="fixedFloor"> | ||
<TresMesh :position="[0, 0, 0]"> | ||
<TresPlaneGeometry :args="[20, 20, 20]" :rotate-x="-Math.PI / 2" /> | ||
<TresMeshBasicMaterial color="#f4f4f4" /> | ||
</TresMesh> | ||
</RigidBody> | ||
</Physics> | ||
</Suspense> | ||
</TresCanvas> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { Collider, ColliderHandle, RigidBody, World } from '@dimforge/rapier3d-compat' | ||
import type { Group, Scene } from 'three' | ||
import type { Ref } from 'vue' | ||
|
||
interface CollisionSource { | ||
collider: Collider | ||
rigidBody: RigidBody | undefined | ||
}; | ||
|
||
interface sourceTarget { | ||
group: Group | ||
rapierGroup: CollisionSource | ||
} | ||
|
||
export const getSourceFromColliderHandle = (world: World, handle: ColliderHandle) => { | ||
const collider = world.getCollider(handle) | ||
const rigidBodyHandle = collider?.parent()?.handle | ||
const rigidBody | ||
= rigidBodyHandle !== undefined | ||
? world.getRigidBody(rigidBodyHandle) | ||
: undefined | ||
const source: CollisionSource = { | ||
collider, | ||
rigidBody, | ||
} | ||
|
||
return source | ||
} | ||
|
||
export const get3DGroupFromSource = (source: CollisionSource, scene: Ref<Scene>) => { | ||
const uuid = (source.rigidBody?.userData as { uuid?: string })?.uuid | ||
const currentRigidBodyNode = scene.value.getObjectByProperty('uuid', uuid) | ||
|
||
return currentRigidBodyNode | ||
} | ||
|
||
export const collisionEmisor = ( | ||
source: sourceTarget, | ||
target: sourceTarget, | ||
started: boolean, | ||
) => { | ||
const collisionType = started ? 'enter' : 'exit' | ||
source.group?.__vnode?.ctx.emit(`collision-${collisionType}`, { source, target }) | ||
} |