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..23c6f23 100644 --- a/packages/ted/src/physics/rapier3d-world.ts +++ b/packages/ted/src/physics/rapier3d-world.ts @@ -1,5 +1,9 @@ import { vec3 } from 'gl-matrix'; -import type { TCollisionClass, TWorldConfig } from '../core/world'; +import type { + TCollisionClass, + TWorldConfig, + TPhysicsMode, +} from '../core/world'; import type { TColliderConfig } from './colliders'; import { TColliderType } from './colliders'; import { @@ -46,6 +50,8 @@ export default class TRapier3DWorld implements TPhysicsWorld { }; } = {}; + private physicsMode: TPhysicsMode = '3d'; + private objects: TRapierObject[] = []; public async create(config: TWorldConfig): Promise { @@ -62,6 +68,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 +159,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 +224,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],