From 0f1d74582c070e0cde76cf10fff79daace10ec7a Mon Sep 17 00:00:00 2001 From: Tom Aisthorpe Date: Mon, 15 Apr 2024 20:06:54 +0100 Subject: [PATCH] feat(physics): add 2d mode --- .changeset/popular-years-talk.md | 5 +++++ packages/ted/src/core/world.ts | 3 +++ packages/ted/src/index.ts | 1 + packages/ted/src/physics/rapier3d-world.ts | 22 ++++++++++++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 .changeset/popular-years-talk.md diff --git a/.changeset/popular-years-talk.md b/.changeset/popular-years-talk.md new file mode 100644 index 0000000..bcfe862 --- /dev/null +++ b/.changeset/popular-years-talk.md @@ -0,0 +1,5 @@ +--- +"@tedengine/ted": minor +--- + +Add 2D physics mode diff --git a/packages/ted/src/core/world.ts b/packages/ted/src/core/world.ts index 4a2f82d..b6d5678 100644 --- a/packages/ted/src/core/world.ts +++ b/packages/ted/src/core/world.ts @@ -46,11 +46,14 @@ export interface TCollisionListener { } export interface TWorldConfig { + mode?: TPhysicsMode; gravity: vec3; defaultCollisionClass: string; collisionClasses: TCollisionClass[]; } +export type TPhysicsMode = '2d' | '3d'; + export interface TCollisionClass { name: string; ignores?: [string]; diff --git a/packages/ted/src/index.ts b/packages/ted/src/index.ts index a95e7e3..31d25e5 100644 --- a/packages/ted/src/index.ts +++ b/packages/ted/src/index.ts @@ -39,6 +39,7 @@ export * from './core/game-state'; export { default as TGameStateManager } from './core/game-state-manager'; +export * from './core/world'; export { default as TWorld } from './core/world'; export * from './core/messages'; diff --git a/packages/ted/src/physics/rapier3d-world.ts b/packages/ted/src/physics/rapier3d-world.ts index 9b8d164..da44b9d 100644 --- a/packages/ted/src/physics/rapier3d-world.ts +++ b/packages/ted/src/physics/rapier3d-world.ts @@ -1,5 +1,6 @@ import { vec3 } from 'gl-matrix'; import type { TCollisionClass, TWorldConfig } from '../core/world'; +import { TPhysicsMode } from '../core/world'; import type { TColliderConfig } from './colliders'; import { TColliderType } from './colliders'; import { @@ -46,6 +47,8 @@ export default class TRapier3DWorld implements TPhysicsWorld { }; } = {}; + private physicsMode: TPhysicsMode = '3d'; + private objects: TRapierObject[] = []; public async create(config: TWorldConfig): Promise { @@ -62,6 +65,10 @@ export default class TRapier3DWorld implements TPhysicsWorld { this.setupCollisionClasses(config.collisionClasses); this.defaultCollisionClass = config.defaultCollisionClass; + + if (config.mode) { + this.physicsMode = config.mode; + } } private setupCollisionClasses(collisionClasses: TCollisionClass[]) { @@ -149,6 +156,15 @@ export default class TRapier3DWorld implements TPhysicsWorld { return { bodies, collisions }; } + /** + * Adds a physics body to the world. + * + * @param uuid - The unique identifier for the body. + * @param collider - The configuration for the collider shape. + * @param translation - The initial translation of the body. + * @param rotation - The initial rotation of the body. + * @param options - Additional options for the body (optional). + */ addBody( uuid: string, collider: TColliderConfig, @@ -205,6 +221,12 @@ export default class TRapier3DWorld implements TPhysicsWorld { bodyDesc.lockRotations(); } + // If we are in 2D mode, disable the Z axis, and only allow rotations around Z + if (this.physicsMode === '2d') { + bodyDesc.enabledTranslations(true, true, false); + bodyDesc.enabledRotations(false, false, true); + } + bodyDesc.setTranslation(...translation); bodyDesc.setRotation({ x: rotation[0],