Skip to main content

Class: Physics

Physics functionality plugin

Remarks

Based on the Matter.js engine.

Implements

  • Plugin

Constructors

new Physics()

new Physics(options?): Physics

Creates an instance of the physics engine.

Parameters

options?: PhysicsOptions

PhysicsOptions

Returns

Physics

Remarks

The constructor must be called early in the game's initialize() method because it adds properties to the M2Node class for physics functionality. These properties will not be available to nodes before the physics plugin is created.

Example

async initialize() {
await super.initialize();
const game = this;
const physics = new Physics({ showsPhysics: true })
await game.registerPlugin(physics);
...
}

Source

physics/src/Physics.ts:84

Properties

_game?

private optional _game: Game

Source

physics/src/Physics.ts:59


_gravity

private _gravity: Vector

Source

physics/src/Physics.ts:58


accumulatedDeltaTime

private accumulatedDeltaTime: number = 0

Source

physics/src/Physics.ts:64


applyForceQueue

private applyForceQueue: ApplyForceQueueItem[]

Source

physics/src/Physics.ts:63


bodiesDictionary

bodiesDictionary: PhysicsBodiesDictionary = {}

Source

physics/src/Physics.ts:55


cumulativeFrameSimulationTime

private cumulativeFrameSimulationTime: number = 0

Source

physics/src/Physics.ts:61


engine

engine: Engine

Source

physics/src/Physics.ts:54


eventListeners

private eventListeners: PhysicsEventListener[]

Source

physics/src/Physics.ts:62


framesSimulatedCount

private framesSimulatedCount: number = 0

Source

physics/src/Physics.ts:60


id

id: string = "physics-matter-js"

Implementation of

Plugin.id

Source

physics/src/Physics.ts:52


options

options: PhysicsOptions

Source

physics/src/Physics.ts:56


type

readonly type: "Game" = "Game"

Implementation of

Plugin.type

Source

physics/src/Physics.ts:53

Accessors

game

get game(): Game

set game(game): void

Parameters

game: Game

Returns

Game

Source

physics/src/Physics.ts:448


gravity

get gravity(): Vector

Vector that specifies the gravity to apply to all physics bodies. Default is { dx: 0, dy: 1 }

set gravity(gravity): void

Parameters

gravity: Vector

Returns

Vector

Source

physics/src/Physics.ts:463

Methods

addEventListener()

private addEventListener(type, callback, callbackOptions?): void

Parameters

type: PhysicsEventType

callback

callbackOptions?: CallbackOptions

Returns

void

Source

physics/src/Physics.ts:159


afterUpdate()

afterUpdate(game, deltaTime): void

Parameters

game: Game

deltaTime: number

Returns

void

Implementation of

Plugin.afterUpdate

Source

physics/src/Physics.ts:98


configureEventListeners()

private configureEventListeners(): void

Returns

void

Source

physics/src/Physics.ts:218


getPhysicsBodiesFromCollisionEvent()

private getPhysicsBodiesFromCollisionEvent(event): object

Returns the Physics Bodies (A & B) involved in a collision event.

Parameters

event: IEventCollision<Engine>

Matter.js collision event

Returns

object

bodyA and bodyB PhysicsBody objects

bodyA

bodyA: PhysicsBody = nodeA.physicsBody

bodyB

bodyB: PhysicsBody = nodeB.physicsBody

Remarks

the Matter.js collision event has the the bodies as Matter.Body type. This method get their corresponding wrappers as PhysicsBody type.

Source

physics/src/Physics.ts:264


initialize()

initialize(game): Promise<void>

Parameters

game: Game

Returns

Promise<void>

Implementation of

Plugin.initialize

Source

physics/src/Physics.ts:94


initializePhysicsBodies()

private initializePhysicsBodies(nodes): void

Parameters

nodes: M2Node[]

Returns

void

Source

physics/src/Physics.ts:502


logAverageFrameUpdateDuration()

private logAverageFrameUpdateDuration(): void

Returns

void

Source

physics/src/Physics.ts:513


modifyNodeProperties()

private modifyNodeProperties(): void

Returns

void

Source

physics/src/Physics.ts:288


onContactBegin()

onContactBegin(callback, options?): void

Executes a callback when physics bodies begin to contact each other.

Parameters

callback

callback function to be called when a contact begins.

options?: CallbackOptions

CallbackOptions

Returns

void

Source

physics/src/Physics.ts:190


onContactEnd()

onContactEnd(callback, options?): void

Executes a callback when physics bodies end contact with other.

Parameters

callback

callback function to be called when a contact ends

options?: CallbackOptions

CallbackOptions

Returns

void

Source

physics/src/Physics.ts:207


scheduleApplyForce()

scheduleApplyForce(item): void

Adds an applyForce() call to the queue.

Parameters

item: ApplyForceQueueItem

ApplyForceQueueItem

Returns

void

Source

physics/src/Physics.ts:155


updateNodesFromPhysicsBodies()

private updateNodesFromPhysicsBodies(nodes): void

Parameters

nodes: M2Node[]

Returns

void

Source

physics/src/Physics.ts:492