-
-
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.
refactor(architecture): better structure separation
### Description - Move `vue` components at `./src/components/` - Move all types/interfaces at `./src/types` - Add `RigidBodyType` type (containing the supported rigidBodyDesc types) - Add `RigidBodyProps` interface - Add `InjectableRapierContext` interface - Add `PhysicsProps` interface - Add `ColliderShape` type - Move helper/utils at `./src/utils/` - Rename `createColliderDesc` to `createColliderDesc`, and return a `ColliderDesc` - Rename `createRigidBody` to `createRigidBodyDesc` and return a `RigidBodyDesc` - Add a `./src/constants/` sub folder Co-Authored-By: Alvaro Saburido <alvaro.saburido@gmail.com>
- Loading branch information
1 parent
6cca9d3
commit 9441602
Showing
15 changed files
with
241 additions
and
175 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
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,52 @@ | ||
<script setup lang="ts"> | ||
import { shallowRef, watch } from 'vue' | ||
import type { Collider, RigidBody } from '@dimforge/rapier3d-compat' | ||
import { type TresObject, useLoop } from '@tresjs/core' | ||
import { useRapierContext } from '../composables/useRapier' | ||
import { createRigidBodyDesc } from '../utils/rigid-body.util' | ||
import { createColliderDesc } from '../utils/collider.util' | ||
import type { RigidBodyProps } from '../types/rigid-body.type' | ||
const props = withDefaults(defineProps<Partial<RigidBodyProps>>(), { | ||
type: 'dynamic', | ||
collider: 'cuboid', | ||
}) | ||
const { world } = await useRapierContext() | ||
const { onBeforeRender } = useLoop() | ||
const rigidRef = shallowRef<TresObject>() | ||
const rigidBody = shallowRef<RigidBody>() | ||
const collider = shallowRef<Collider>() | ||
watch(rigidRef, (value) => { | ||
if (!value) { return } | ||
const rigidBodyDesc = createRigidBodyDesc(value.children[0], props.type) | ||
const colliderDesc = createColliderDesc(value.children[0], props.collider) | ||
if (!rigidBodyDesc || !colliderDesc) { | ||
throw new Error('Invalid #RigidBody properties detected. Unable to construct the physics body') | ||
} | ||
rigidBody.value = world.createRigidBody(rigidBodyDesc) | ||
collider.value = world.createCollider(colliderDesc, rigidBody.value) | ||
}) | ||
onBeforeRender(() => { | ||
if (!rigidBody.value) { return } | ||
const position = rigidBody.value.translation() | ||
rigidRef.value?.children[0].position.set(position.x, position.y, position.z) | ||
const rotation = rigidBody.value.rotation() | ||
rigidRef.value?.children[0].quaternion.set(rotation.x, rotation.y, rotation.z, rotation.w) | ||
}) | ||
</script> | ||
|
||
<template> | ||
<TresGroup ref="rigidRef"> | ||
<slot></slot> | ||
</TresGroup> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Vector3 } from 'three' | ||
|
||
export const VECTOR_ZERO = new Vector3() |
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 @@ | ||
export const GRAVITY = { x: 0, y: -9.81, z: 0 } |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import Physics from './Physics.vue' | ||
import RigidBody from './RigidBody.vue' | ||
import Physics from '../components/Physics.vue' | ||
import RigidBody from '../components/RigidBody.vue' | ||
import { useRapier } from './../composables/useRapier' | ||
|
||
export { Physics, RigidBody, useRapier } |
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import type { InjectionKey, ShallowRef } from 'vue' | ||
|
||
export interface RapierContext {} | ||
import type { InjectableRapierContext } from '../types/rapier.type' | ||
|
||
export const rapierInjectionKey: InjectionKey< | ||
ShallowRef<RapierContext | null> | ||
ShallowRef<InjectableRapierContext | null> | ||
> = Symbol('tresrapier') |
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,8 @@ | ||
export type ColliderShape = | ||
| 'cuboid' | ||
| 'ball' | ||
| 'capsule' | ||
| 'cone' | ||
| 'cylinder' | ||
| 'trimesh' | ||
| 'heightfield' |
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 @@ | ||
export interface PhysicsProps { debug: boolean } |
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,42 @@ | ||
import type Rapier from '@dimforge/rapier3d-compat' | ||
import type { World } from '@dimforge/rapier3d-compat' | ||
|
||
export interface RapierContext { | ||
/** | ||
* @description Rapier instance. | ||
* | ||
* @docs https://rapier.rs/docs/api/javascript/JavaScript3D/ | ||
*/ | ||
rapier: typeof Rapier | ||
/** | ||
* @description Rapier physics world | ||
*/ | ||
world: World | ||
/** | ||
* @description If the physics simulation is paused. | ||
*/ | ||
isPaused: boolean | ||
/** | ||
* @description If the debugging mode enabled. | ||
*/ | ||
isDebug: boolean | ||
/** | ||
* @description Set the physics world. | ||
* | ||
* @param world New physics world. | ||
*/ | ||
setWorld: (world: World) => void | ||
/** | ||
* @description Step the physics world. | ||
* | ||
* @param timestep The timestep length, in seconds. | ||
* | ||
* @example | ||
* ```ts | ||
* step(1/60) | ||
* ``` | ||
*/ | ||
step: (timestep?: number) => void | ||
} | ||
|
||
export interface InjectableRapierContext {} |
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,12 @@ | ||
import type { ColliderShape } from './collider.type' | ||
|
||
export type RigidBodyType = | ||
| 'dynamic' | ||
| 'kinematic' | ||
| 'kinematicVelocity' | ||
| 'fixed' | ||
|
||
export interface RigidBodyProps { | ||
type: RigidBodyType | ||
collider: ColliderShape | ||
} |
Oops, something went wrong.