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
Returns
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
Properties
_game?
private
optional
_game:Game
Source
_gravity
private
_gravity:Vector
Source
accumulatedDeltaTime
private
accumulatedDeltaTime:number
=0
Source
applyForceQueue
private
applyForceQueue:ApplyForceQueueItem
[]
Source
bodiesDictionary
bodiesDictionary:
PhysicsBodiesDictionary
={}
Source
cumulativeFrameSimulationTime
private
cumulativeFrameSimulationTime:number
=0
Source
engine
engine:
Engine
Source
eventListeners
private
eventListeners:PhysicsEventListener
[]
Source
framesSimulatedCount
private
framesSimulatedCount:number
=0
Source
id
id:
string
="physics-matter-js"
Implementation of
Plugin.id
Source
options
options:
PhysicsOptions
Source
type
readonly
type:"Game"
="Game"
Implementation of
Plugin.type
Source
Accessors
game
get
game():Game
set
game(game
):void
Parameters
• game: Game
Returns
Game
Source
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
Source
Methods
addEventListener()
private
addEventListener(type
,callback
,callbackOptions
?):void
Parameters
• type: PhysicsEventType
• callback
• callbackOptions?: CallbackOptions
Returns
void
Source
afterUpdate()
afterUpdate(
game
,deltaTime
):void
Parameters
• game: Game
• deltaTime: number
Returns
void
Implementation of
Plugin.afterUpdate
Source
configureEventListeners()
private
configureEventListeners():void
Returns
void
Source
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
initialize()
initialize(
game
):Promise
<void
>
Parameters
• game: Game
Returns
Promise
<void
>
Implementation of
Plugin.initialize
Source
initializePhysicsBodies()
private
initializePhysicsBodies(nodes
):void
Parameters
• nodes: M2Node
[]
Returns
void
Source
logAverageFrameUpdateDuration()
private
logAverageFrameUpdateDuration():void
Returns
void
Source
modifyNodeProperties()
private
modifyNodeProperties():void
Returns
void
Source
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
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
scheduleApplyForce()
scheduleApplyForce(
item
):void
Adds an applyForce() call to the queue.
Parameters
• item: ApplyForceQueueItem
ApplyForceQueueItem
Returns
void
Source
updateNodesFromPhysicsBodies()
private
updateNodesFromPhysicsBodies(nodes
):void
Parameters
• nodes: M2Node
[]
Returns
void