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);
...
}

Defined in

m2c2kit/packages/physics/src/Physics.ts:84

Properties

bodiesDictionary

bodiesDictionary: PhysicsBodiesDictionary = {}

Defined in

m2c2kit/packages/physics/src/Physics.ts:55


engine

engine: Engine

Defined in

m2c2kit/packages/physics/src/Physics.ts:54


id

id: string = "physics-matter-js"

Short identifier of the plugin.

Implementation of

Plugin.id

Defined in

m2c2kit/packages/physics/src/Physics.ts:52


options

options: PhysicsOptions

Defined in

m2c2kit/packages/physics/src/Physics.ts:56


type

readonly type: "Game" = "Game"

What kind of m2c2kit object does the plugin work with?

Implementation of

Plugin.type

Defined in

m2c2kit/packages/physics/src/Physics.ts:53

Accessors

game

Get Signature

get game(): Game

Returns

Game

Set Signature

set game(game): void

Parameters

game: Game

Returns

void

Defined in

m2c2kit/packages/physics/src/Physics.ts:448


gravity

Get Signature

get gravity(): Vector

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

Returns

Vector

Set Signature

set gravity(gravity): void

Parameters

gravity: Vector

Returns

void

Defined in

m2c2kit/packages/physics/src/Physics.ts:463

Methods

afterUpdate()

afterUpdate(game, deltaTime): void

Plugin code run after the frame update, but before the frame draw.

Parameters

game: Game

deltaTime: number

Returns

void

Implementation of

Plugin.afterUpdate

Defined in

m2c2kit/packages/physics/src/Physics.ts:98


initialize()

initialize(game): Promise<void>

Initialization code run when the plugin is registered with the game.

Parameters

game: Game

Returns

Promise<void>

Implementation of

Plugin.initialize

Defined in

m2c2kit/packages/physics/src/Physics.ts:94


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

Defined in

m2c2kit/packages/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

Defined in

m2c2kit/packages/physics/src/Physics.ts:207


scheduleApplyForce()

scheduleApplyForce(item): void

Adds an applyForce() call to the queue.

Parameters

item: ApplyForceQueueItem

ApplyForceQueueItem

Returns

void

Defined in

m2c2kit/packages/physics/src/Physics.ts:155