Skip to content

Commit

Permalink
feat(physics): add 2d mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaisthorpe committed Apr 15, 2024
1 parent ca20748 commit a4cc0bb
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-years-talk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tedengine/ted": minor
---

Add 2D physics mode
8 changes: 8 additions & 0 deletions packages/ted/src/core/world.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ export interface TCollisionListener {
}

export interface TWorldConfig {
mode?: TPhysicsMode;
gravity: vec3;
defaultCollisionClass: string;
collisionClasses: TCollisionClass[];
}

export enum TPhysicsMode {
// 2D only locks translations and rotations
// It does not ignore z position
TwoD = '2d',
ThreeD = '3d',
}

export interface TCollisionClass {
name: string;
ignores?: [string];
Expand Down
1 change: 1 addition & 0 deletions packages/ted/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
22 changes: 22 additions & 0 deletions packages/ted/src/physics/rapier3d-world.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -46,6 +47,8 @@ export default class TRapier3DWorld implements TPhysicsWorld {
};
} = {};

private physicsMode: TPhysicsMode = TPhysicsMode.ThreeD;

private objects: TRapierObject[] = [];

public async create(config: TWorldConfig): Promise<void> {
Expand All @@ -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[]) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 === TPhysicsMode.TwoD) {
bodyDesc.enabledTranslations(true, true, false);
bodyDesc.enabledRotations(false, false, true);
}

bodyDesc.setTranslation(...translation);
bodyDesc.setRotation({
x: rotation[0],
Expand Down

0 comments on commit a4cc0bb

Please sign in to comment.