phaser
Version:
A fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers from the team at Phaser Studio Inc.
1,571 lines (1,364 loc) • 193 kB
TypeScript
// Type definitions specifically for Matter.js as used by Phaser 3
//
// Definitions by: Ivane Gegia <https://twitter.com/ivanegegia>,
// David Asmuth <https://github.com/piranha771>,
// Piotr Pietrzak <https://github.com/hasparus>,
// Richard Davey <rich@photonstorm.com>
/// <reference types="./phaser" />
declare namespace MatterJS {
// --------------------------------------------------------------
// Interfaces
// --------------------------------------------------------------
interface IChamfer {
radius?: number | Array<number>;
quality?: number;
qualityMin?: number;
qualityMax?: number;
}
interface IChamferableBodyDefinition extends IBodyDefinition {
chamfer?: IChamfer;
}
interface IBodyDefinition {
/**
* A `Number` specifying the angle of the body, in radians.
*
* @property angle
* @type number
* @default 0
*/
angle?: number;
/**
* A `Number` that _measures_ the current angular speed of the body after the last `Body.update`. It is read-only and always positive (it's the magnitude of `body.angularVelocity`).
*
* @readOnly
* @property angularSpeed
* @type number
* @default 0
*/
angularSpeed?: number;
/**
* A `Number` that _measures_ the current angular velocity of the body after the last `Body.update`. It is read-only.
* If you need to modify a body's angular velocity directly, you should apply a torque or simply change the body's `angle` (as the engine uses position-Verlet integration).
*
* @readOnly
* @property angularVelocity
* @type number
* @default 0
*/
angularVelocity?: number;
/**
* A `Number` that _measures_ the area of the body's convex hull, calculated at creation by `Body.create`.
*
* @property area
* @type string
* @default
*/
area?: number;
/**
* An array of unique axis vectors (edge normals) used for collision detection.
* These are automatically calculated from the given convex hull (`vertices` array) in `Body.create`.
* They are constantly updated by `Body.update` during the simulation.
*
* @property axes
* @type vector[]
*/
axes?: Array<Vector>;
/**
* A `Bounds` object that defines the AABB region for the body.
* It is automatically calculated from the given convex hull (`vertices` array) in `Body.create` and constantly updated by `Body.update` during simulation.
*
* @property bounds
* @type bounds
*/
bounds?: IBound;
/**
* A `Number` that defines the density of the body, that is its mass per unit area.
* If you pass the density via `Body.create` the `mass` property is automatically calculated for you based on the size (area) of the object.
* This is generally preferable to simply setting mass and allows for more intuitive definition of materials (e.g. rock has a higher density than wood).
*
* @property density
* @type number
* @default 0.001
*/
density?: number;
/**
* A `Vector` that specifies the force to apply in the current step. It is zeroed after every `Body.update`. See also `Body.applyForce`.
*
* @property force
* @type vector
* @default { x: 0, y: 0 }
*/
force?: Vector;
/**
* A `Number` that defines the friction of the body. The value is always positive and is in the range `(0, 1)`.
* A value of `0` means that the body may slide indefinitely.
* A value of `1` means the body may come to a stop almost instantly after a force is applied.
*
* The effects of the value may be non-linear.
* High values may be unstable depending on the body.
* The engine uses a Coulomb friction model including static and kinetic friction.
* Note that collision response is based on _pairs_ of bodies, and that `friction` values are _combined_ with the following formula:
*
* Math.min(bodyA.friction, bodyB.friction)
*
* @property friction
* @type number
* @default 0.1
*/
friction?: number;
/**
* A `Number` that defines the air friction of the body (air resistance).
* A value of `0` means the body will never slow as it moves through space.
* The higher the value, the faster a body slows when moving through space.
* The effects of the value are non-linear.
*
* @property frictionAir
* @type number
* @default 0.01
*/
frictionAir?: number;
/**
* A `Number` that defines the moment of inertia (i.e. second moment of area) of the body.
* It is automatically calculated from the given convex hull (`vertices` array) and density in `Body.create`.
* If you modify this value, you must also modify the `body.inverseInertia` property (`1 / inertia`).
*
* @property inertia
* @type number
*/
inertia?: number;
/**
* A `Number` that defines the inverse moment of inertia of the body (`1 / inertia`).
* If you modify this value, you must also modify the `body.inertia` property.
*
* @property inverseInertia
* @type number
*/
inverseInertia?: number;
/**
* A `Number` that defines the inverse mass of the body (`1 / mass`).
* If you modify this value, you must also modify the `body.mass` property.
*
* @property inverseMass
* @type number
*/
inverseMass?: number;
/**
* A flag that indicates whether a body is a sensor. Sensor triggers collision events, but doesn't react with colliding body physically.
*
* @property isSensor
* @type boolean
* @default false
*/
isSensor?: boolean;
/**
* A flag that indicates whether the body is considered sleeping. A sleeping body acts similar to a static body, except it is only temporary and can be awoken.
* If you need to set a body as sleeping, you should use `Sleeping.set` as this requires more than just setting this flag.
*
* @property isSleeping
* @type boolean
* @default false
*/
isSleeping?: boolean;
/**
* A flag that indicates whether a body is considered static. A static body can never change position or angle and is completely fixed.
* If you need to set a body as static after its creation, you should use `Body.setStatic` as this requires more than just setting this flag.
*
* @property isStatic
* @type boolean
* @default false
*/
isStatic?: boolean;
/**
* An arbitrary `String` name to help the user identify and manage bodies.
*
* @property label
* @type string
* @default "Body"
*/
label?: string;
/**
* A `Number` that defines the mass of the body, although it may be more appropriate to specify the `density` property instead.
* If you modify this value, you must also modify the `body.inverseMass` property (`1 / mass`).
*
* @property mass
* @type number
*/
mass?: number;
/**
* A `Number` that _measures_ the amount of movement a body currently has (a combination of `speed` and `angularSpeed`). It is read-only and always positive.
* It is used and updated by the `Matter.Sleeping` module during simulation to decide if a body has come to rest.
*
* @readOnly
* @property motion
* @type number
* @default 0
*/
motion?: number;
/**
* A `Vector` that specifies the current world-space position of the body.
*
* @property position
* @type vector
*/
position?: Vector;
/**
* An `Object` that defines the rendering properties to be consumed by the module `Matter.Render`.
*
* @property render
* @type object
*/
render?: IBodyRenderOptions;
/**
* A `Number` that defines the restitution (elasticity) of the body. The value is always positive and is in the range `(0, 1)`.
* A value of `0` means collisions may be perfectly inelastic and no bouncing may occur.
* A value of `0.8` means the body may bounce back with approximately 80% of its kinetic energy.
* Note that collision response is based on _pairs_ of bodies, and that `restitution` values are _combined_ with the following formula:
*
* Math.max(bodyA.restitution, bodyB.restitution)
*
* @property restitution
* @type number
* @default 0
*/
restitution?: number;
/**
* A `Number` that defines the number of updates in which this body must have near-zero velocity before it is set as sleeping by the `Matter.Sleeping` module (if sleeping is enabled by the engine).
*
* @property sleepThreshold
* @type number
* @default 60
*/
sleepThreshold?: number;
/**
* A `Number` that specifies a tolerance on how far a body is allowed to 'sink' or rotate into other bodies.
* Avoid changing this value unless you understand the purpose of `slop` in physics engines.
* The default should generally suffice, although very large bodies may require larger values for stable stacking.
*
* @property slop
* @type number
* @default 0.05
*/
slop?: number;
/**
* A `Number` that _measures_ the current speed of the body after the last `Body.update`. It is read-only and always positive (it's the magnitude of `body.velocity`).
*
* @readOnly
* @property speed
* @type number
* @default 0
*/
speed?: number;
/**
* A `Number` that allows per-body time scaling, e.g. a force-field where bodies inside are in slow-motion, while others are at full speed.
*
* @property timeScale
* @type number
* @default 1
*/
timeScale?: number;
/**
* A `Number` that specifies the torque (turning force) to apply in the current step. It is zeroed after every `Body.update`.
*
* @property torque
* @type number
* @default 0
*/
torque?: number;
/**
* A `String` denoting the type of object.
*
* @property type
* @type string
* @default "body"
*/
type?: string;
/**
* A `Vector` that _measures_ the current velocity of the body after the last `Body.update`. It is read-only.
* If you need to modify a body's velocity directly, you should either apply a force or simply change the body's `position` (as the engine uses position-Verlet integration).
*
* @readOnly
* @property velocity
* @type vector
* @default { x: 0, y: 0 }
*/
velocity?: Vector;
/**
* An array of `Vector` objects that specify the convex hull of the rigid body.
* These should be provided about the origin `(0, 0)`. E.g.
*
* [{ x: 0, y: 0 }, { x: 25, y: 50 }, { x: 50, y: 0 }]
*
* When passed via `Body.create`, the vertices are translated relative to `body.position` (i.e. world-space, and constantly updated by `Body.update` during simulation).
* The `Vector` objects are also augmented with additional properties required for efficient collision detection.
*
* Other properties such as `inertia` and `bounds` are automatically calculated from the passed vertices (unless provided via `options`).
* Concave hulls are not currently supported. The module `Matter.Vertices` contains useful methods for working with vertices.
*
* @property vertices
* @type vector[]
*/
vertices?: Array<Vector>;
/**
* An array of bodies that make up this body.
* The first body in the array must always be a self reference to the current body instance.
* All bodies in the `parts` array together form a single rigid compound body.
* Parts are allowed to overlap, have gaps or holes or even form concave bodies.
* Parts themselves should never be added to a `World`, only the parent body should be.
* Use `Body.setParts` when setting parts to ensure correct updates of all properties.
*
* @property parts
* @type body[]
*/
parts?: Array<BodyType>;
/**
* A self reference if the body is _not_ a part of another body.
* Otherwise this is a reference to the body that this is a part of.
* See `body.parts`.
*
* @property parent
* @type body
*/
parent?: BodyType;
/**
* A `Number` that defines the static friction of the body (in the Coulomb friction model).
* A value of `0` means the body will never 'stick' when it is nearly stationary and only dynamic `friction` is used.
* The higher the value (e.g. `10`), the more force it will take to initially get the body moving when nearly stationary.
* This value is multiplied with the `friction` property to make it easier to change `friction` and maintain an appropriate amount of static friction.
*
* @property frictionStatic
* @type number
* @default 0.5
*/
frictionStatic?: number;
/**
* An `Object` that specifies the collision filtering properties of this body.
*
* Collisions between two bodies will obey the following rules:
* - If the two bodies have the same non-zero value of `collisionFilter.group`,
* they will always collide if the value is positive, and they will never collide
* if the value is negative.
* - If the two bodies have different values of `collisionFilter.group` or if one
* (or both) of the bodies has a value of 0, then the category/mask rules apply as follows:
*
* Each body belongs to a collision category, given by `collisionFilter.category`. This
* value is used as a bit field and the category should have only one bit set, meaning that
* the value of this property is a power of two in the range [1, 2^31]. Thus, there are 32
* different collision categories available.
*
* Each body also defines a collision bitmask, given by `collisionFilter.mask` which specifies
* the categories it collides with (the value is the bitwise AND value of all these categories).
*
* Using the category/mask rules, two bodies `A` and `B` collide if each includes the other's
* category in its mask, i.e. `(categoryA & maskB) !== 0` and `(categoryB & maskA) !== 0`
* are both true.
*
* @property collisionFilter
* @type object
*/
collisionFilter?: ICollisionFilter;
/**
* A reference to the Phaser Game Object this body belongs to, if any.
*
* @property gameObject
* @type Phaser.GameObjects.GameObject
*/
gameObject?: Phaser.GameObjects.GameObject;
/**
* Scale the influence of World gravity when applied to this body.
*
* @property gravityScale
* @type vector
* @default { x: 1, y: 1 }
*/
gravityScale?: Vector;
/**
* Will this Body ignore World gravity during the Engine update?
*
* @property ignoreGravity
* @type boolean
* @default false
*/
ignoreGravity?: boolean;
/**
* Will this Body ignore Phaser Pointer input events?
*
* @property ignorePointer
* @type boolean
* @default false
*/
ignorePointer?: boolean;
/**
* A callback that is invoked when this Body starts colliding with any other Body.
*
* You can register callbacks by providing a function of type `( pair: Matter.Pair) => void`.
*
* @property onCollideCallback
* @type function
* @default null
*/
onCollideCallback?: Function;
/**
* A callback that is invoked when this Body stops colliding with any other Body.
*
* You can register callbacks by providing a function of type `( pair: Matter.Pair) => void`.
*
* @property onCollideEndCallback
* @type function
* @default null
*/
onCollideEndCallback?: Function;
/**
* A callback that is invoked for the duration that this Body is colliding with any other Body.
*
* You can register callbacks by providing a function of type `( pair: Matter.Pair) => void`.
*
* @property onCollideActiveCallback
* @type function
* @default null
*/
onCollideActiveCallback?: Function;
/**
* A collision callback dictionary _(body id -> function)_ used by the `Body.setOnCollideWith` function.
*
* @property onCollideWith
* @type {Object.<number, Function>}
* @default {}
*/
onCollideWith?: Record<number, Function>;
}
interface IBodyRenderOptions {
/**
* A flag that indicates if the body should be rendered.
*
* @property visible
* @type boolean
* @default true
*/
visible?: boolean;
/**
* Sets the opacity. 1.0 is fully opaque. 0.0 is fully translucent.
*
* @property opacity
* @type number
* @default 1
*/
opacity?: number;
/**
* An `Object` that defines the sprite properties to use when rendering, if any.
*
* @property sprite
* @type object
*/
sprite?: IBodyRenderOptionsSprite;
/**
* A hex color value that defines the fill color to use when rendering the body.
*
* @property fillColor
* @type number
*/
fillColor?: number;
/**
* A value that defines the fill opacity to use when rendering the body.
*
* @property fillOpacity
* @type number
*/
fillOpacity?: number;
/**
* A hex color value that defines the line color to use when rendering the body.
*
* @property lineColor
* @type number
*/
lineColor?: number;
/**
* A value that defines the line opacity to use when rendering the body.
*
* @property lineOpacity
* @type number
*/
lineOpacity?: number;
/**
* A `Number` that defines the line width to use when rendering the body outline.
*
* @property lineThickness
* @type number
*/
lineThickness?: number;
}
interface IBodyRenderOptionsSprite {
/**
* A `Number` that defines the scaling in the x-axis for the sprite, if any.
*
* @property xOffset
* @type number
* @default 0
*/
xOffset: number;
/**
* A `Number` that defines the scaling in the y-axis for the sprite, if any.
*
* @property yOffset
* @type number
* @default 0
*/
yOffset: number;
}
interface IBound {
min: { x: number, y: number }
max: { x: number, y: number }
}
interface ICompositeDefinition {
/**
* An array of `Body` that are _direct_ children of this composite.
* To add or remove bodies you should use `Composite.add` and `Composite.remove` methods rather than directly modifying this property.
* If you wish to recursively find all descendants, you should use the `Composite.allBodies` method.
*
* @property bodies
* @type body[]
* @default []
*/
bodies?: Array<BodyType>;
/**
* An array of `Composite` that are _direct_ children of this composite.
* To add or remove composites you should use `Composite.add` and `Composite.remove` methods rather than directly modifying this property.
* If you wish to recursively find all descendants, you should use the `Composite.allComposites` method.
*
* @property composites
* @type composite[]
* @default []
*/
composites?: Array<CompositeType>;
/**
* An array of `Constraint` that are _direct_ children of this composite.
* To add or remove constraints you should use `Composite.add` and `Composite.remove` methods rather than directly modifying this property.
* If you wish to recursively find all descendants, you should use the `Composite.allConstraints` method.
*
* @property constraints
* @type constraint[]
* @default []
*/
constraints?: Array<ConstraintType>;
/**
* An integer `Number` uniquely identifying number generated in `Composite.create` by `Common.nextId`.
*
* @property id
* @type number
*/
id?: number;
/**
* A flag that specifies whether the composite has been modified during the current step.
* Most `Matter.Composite` methods will automatically set this flag to `true` to inform the engine of changes to be handled.
* If you need to change it manually, you should use the `Composite.setModified` method.
*
* @property isModified
* @type boolean
* @default false
*/
isModified?: boolean;
/**
* An arbitrary `String` name to help the user identify and manage composites.
*
* @property label
* @type string
* @default "Composite"
*/
label?: string;
/**
* The `Composite` that is the parent of this composite. It is automatically managed by the `Matter.Composite` methods.
*
* @property parent
* @type composite
* @default null
*/
parent?: CompositeType;
/**
* A `String` denoting the type of object.
*
* @property type
* @type string
* @default "composite"
*/
type?: string;
}
interface IConstraintDefinition {
/**
* The first possible `Body` that this constraint is attached to.
*
* @property bodyA
* @type body
* @default null
*/
bodyA?: IBodyDefinition;
/**
* The second possible `Body` that this constraint is attached to.
*
* @property bodyB
* @type body
* @default null
*/
bodyB?: IBodyDefinition;
/**
* An integer `Number` uniquely identifying number generated in `Composite.create` by `Common.nextId`.
*
* @property id
* @type number
*/
id?: number;
/**
* An arbitrary `String` name to help the user identify and manage bodies.
*
* @property label
* @type string
* @default "Constraint"
*/
label?: string;
/**
* A `Number` that specifies the target resting length of the constraint.
* It is calculated automatically in `Constraint.create` from initial positions of the `constraint.bodyA` and `constraint.bodyB`.
*
* @property length
* @type number
*/
length?: number;
/**
* A `Vector` that specifies the offset of the constraint from center of the `constraint.bodyA` if defined, otherwise a world-space position.
*
* @property pointA
* @type vector
* @default { x: 0, y: 0 }
*/
pointA?: Vector;
/**
* A `Vector` that specifies the offset of the constraint from center of the `constraint.bodyA` if defined, otherwise a world-space position.
*
* @property pointB
* @type vector
* @default { x: 0, y: 0 }
*/
pointB?: Vector;
/**
* An `Object` that defines the rendering properties to be consumed by the module `Matter.Render`.
*
* @property render
* @type object
*/
render?: IConstraintRenderDefinition;
/**
* A `Number` that specifies the stiffness of the constraint, i.e. the rate at which it returns to its resting `constraint.length`.
* A value of `1` means the constraint should be very stiff.
* A value of `0.2` means the constraint acts like a soft spring.
*
* @property stiffness
* @type number
* @default 1
*/
stiffness?: number;
/**
* A `Number` that specifies the damping of the constraint,
* i.e. the amount of resistance applied to each body based on their velocities to limit the amount of oscillation.
* Damping will only be apparent when the constraint also has a very low `stiffness`.
* A value of `0.1` means the constraint will apply heavy damping, resulting in little to no oscillation.
* A value of `0` means the constraint will apply no damping.
*
* @property damping
* @type number
* @default 0
*/
damping?: number;
/**
* A `String` denoting the type of object.
*
* @property type
* @type string
* @default "constraint"
*/
type?: string;
}
interface IConstraintRenderDefinition {
/**
* A flag that indicates if the constraint should be rendered.
*
* @property visible
* @type boolean
* @default true
*/
visible?: boolean;
/**
* The type of constraint.
*
* @property type
* @type string
* @default 'line'
*/
type?: string;
/**
* A flag that indicates if the constraint anchors should be rendered.
*
* @property anchors
* @type boolean
* @default true
*/
anchors?: boolean;
/**
* A hex color value that defines the line color to use when rendering the body.
*
* @property lineColor
* @type number
*/
lineColor?: number;
/**
* A value that defines the line opacity to use when rendering the body.
*
* @property lineOpacity
* @type number
*/
lineOpacity?: number;
/**
* A `Number` that defines the line width to use when rendering the body outline.
*
* @property lineThickness
* @type number
*/
lineThickness?: number;
/**
* The size of the pins during rendering.
*
* @property pinSize
* @type number
*/
pinSize?: number;
/**
* A hex color value that defines the color to use when rendering the anchors.
*
* @property anchorColor
* @type number
*/
anchorColor?: number;
/**
* The size of the anchors during rendering.
*
* @property anchorSize
* @type number
*/
anchorSize?: number;
}
interface IEngineDefinition {
/**
* An integer `Number` that specifies the number of position iterations to perform each update.
* The higher the value, the higher quality the simulation will be at the expense of performance.
*
* @property positionIterations
* @type number
* @default 6
*/
positionIterations?: number;
/**
* An integer `Number` that specifies the number of velocity iterations to perform each update.
* The higher the value, the higher quality the simulation will be at the expense of performance.
*
* @property velocityIterations
* @type number
* @default 4
*/
velocityIterations?: number;
/**
* An integer `Number` that specifies the number of constraint iterations to perform each update.
* The higher the value, the higher quality the simulation will be at the expense of performance.
* The default value of `2` is usually very adequate.
*
* @property constraintIterations
* @type number
* @default 2
*/
constraintIterations?: number;
/**
* A flag that specifies whether the engine should allow sleeping via the `Matter.Sleeping` module.
* Sleeping can improve stability and performance, but often at the expense of accuracy.
*
* @property enableSleeping
* @type boolean
* @default false
*/
enableSleeping?: boolean;
/**
* An `Object` containing properties regarding the timing systems of the engine.
*
* @property timing
* @type object
*/
timing?: IEngineTimingOptions;
/**
* An instance of a broadphase controller. The default value is a `Matter.Grid` instance created by `Engine.create`.
*
* @property broadphase
* @type grid
* @default a Matter.Grid instance
*/
grid?: Grid;
/**
* A `World` composite object that will contain all simulated bodies and constraints.
*
* @property world
* @type world
* @default a Matter.World instance
*/
world?: World;
}
interface IEngineTimingOptions {
/**
* A `Number` that specifies the global scaling factor of time for all bodies.
* A value of `0` freezes the simulation.
* A value of `0.1` gives a slow-motion effect.
* A value of `1.2` gives a speed-up effect.
*
* @property timing.timeScale
* @type number
* @default 1
*/
timeScale: number;
/**
* A `Number` that specifies the current simulation-time in milliseconds starting from `0`.
* It is incremented on every `Engine.update` by the given `delta` argument.
*
* @property timing.timestamp
* @type number
* @default 0
*/
timestamp: number;
}
interface IMouseConstraintDefinition {
/**
* The `Constraint` object that is used to move the body during interaction.
*
* @property constraint
* @type constraint
*/
constraint?: ConstraintType;
/**
* An `Object` that specifies the collision filter properties.
* The collision filter allows the user to define which types of body this mouse constraint can interact with.
* See `body.collisionFilter` for more information.
*
* @property collisionFilter
* @type object
*/
collisionFilter?: ICollisionFilter;
/**
* The `Body` that is currently being moved by the user, or `null` if no body.
*
* @property body
* @type body
* @default null
*/
body?: BodyType;
/**
* A `String` denoting the type of object.
*
* @property type
* @type string
* @default "constraint"
*/
type?: string;
}
interface IGridDefinition {}
interface IPair {
id: number;
bodyA: Body;
bodyB: Body;
contacts: any;
contactCount: number;
separation: number;
isActive: boolean;
timeCreated: number;
timeUpdated: number,
inverseMass: number;
friction: number;
frictionStatic: number;
restitution: number;
slop: number;
}
interface ICollisionData {
collided: boolean;
bodyA: Body;
bodyB: Body;
axisBody: Body;
axisNumber: number;
depth: number;
parentA: Body;
parentB: Body;
normal: Vector;
tangent: Vector;
penetration: Vector;
supports: Vector[];
inverseMass: number;
friction: number;
frictionStatic: number;
restitution: number;
slop: number;
}
interface ICollisionPair {
id: string;
bodyA: Body;
bodyB: Body;
contacts: Vector[];
separation: number;
isActive: boolean;
confirmedActive: boolean;
isSensor: boolean;
timeCreated: number;
timeUpdated: number;
collision: ICollisionData;
inverseMass: number;
friction: number;
frictionStatic: number;
restitution: number;
slop: number;
}
interface ICollisionFilter {
category: number;
mask: number;
group: number;
}
interface IRunnerOptions {
/**
* A `Boolean` that specifies if the runner should use a fixed timestep (otherwise it is variable).
* If timing is fixed, then the apparent simulation speed will change depending on the frame rate (but behaviour will be deterministic).
* If the timing is variable, then the apparent simulation speed will be constant (approximately, but at the cost of determininism).
*
* @property isFixed
* @type boolean
* @default false
*/
isFixed?: boolean;
/**
* A `Number` that specifies the time step between updates in milliseconds.
* If `engine.timing.isFixed` is set to `true`, then `delta` is fixed.
* If it is `false`, then `delta` can dynamically change to maintain the correct apparent simulation speed.
*
* @property delta
* @type number
* @default 1000 / 60
*/
delta?: number;
}
interface IWorldDefinition extends ICompositeDefinition {
gravity?: Gravity;
bounds?: IBound;
}
interface Gravity extends Vector {
scale: number;
}
interface IEvent<T> {
/**
* The name of the event
*/
name: string;
/**
* The source object of the event
*/
source: T;
}
interface IEventComposite<T> extends IEvent<T> {
/**
* EventObjects (may be a single body, constraint, composite or a mixed array of these)
*/
object: any;
}
interface IEventTimestamped<T> extends IEvent<T> {
/**
* The engine.timing.timestamp of the event
*/
timestamp: number;
}
interface IEventCollision<T> extends IEventTimestamped<T> {
/**
* The collision pair
*/
pairs: Array<IPair>;
}
// --------------------------------------------------------------
// Types
// --------------------------------------------------------------
type CompositeType = {
/**
* An integer `Number` uniquely identifying number generated in `Composite.create` by `Common.nextId`.
*
* @property id
* @type number
*/
id: number;
/**
* A `String` denoting the type of object.
*
* @property type
* @type string
* @default "composite"
*/
type: string;
/**
* The `Composite` that is the parent of this composite. It is automatically managed by the `Matter.Composite` methods.
*
* @property parent
* @type composite
* @default null
*/
parent?: CompositeType;
/**
* A flag that specifies whether the composite has been modified during the current step.
* Most `Matter.Composite` methods will automatically set this flag to `true` to inform the engine of changes to be handled.
* If you need to change it manually, you should use the `Composite.setModified` method.
*
* @property isModified
* @type boolean
* @default false
*/
isModified: boolean;
/**
* An array of `Body` that are _direct_ children of this composite.
* To add or remove bodies you should use `Composite.add` and `Composite.remove` methods rather than directly modifying this property.
* If you wish to recursively find all descendants, you should use the `Composite.allBodies` method.
*
* @property bodies
* @type body[]
* @default []
*/
bodies: Array<BodyType>;
/**
* An array of `Constraint` that are _direct_ children of this composite.
* To add or remove constraints you should use `Composite.add` and `Composite.remove` methods rather than directly modifying this property.
* If you wish to recursively find all descendants, you should use the `Composite.allConstraints` method.
*
* @property constraints
* @type constraint[]
* @default []
*/
constraints: Array<ConstraintType>;
/**
* An array of `Composite` that are _direct_ children of this composite.
* To add or remove composites you should use `Composite.add` and `Composite.remove` methods rather than directly modifying this property.
* If you wish to recursively find all descendants, you should use the `Composite.allComposites` method.
*
* @property composites
* @type composite[]
* @default []
*/
composites: Array<CompositeType>;
/**
* An arbitrary `String` name to help the user identify and manage composites.
*
* @property label
* @type string
* @default "Composite"
*/
label: string;
/**
* An object reserved for storing plugin-specific properties.
*
* @property plugin
* @type {}
*/
plugin: any;
}
type ConstraintType = {
/**
* The first possible `Body` that this constraint is attached to.
*
* @property bodyA
* @type body
* @default null
*/
bodyA?: BodyType;
/**
* The second possible `Body` that this constraint is attached to.
*
* @property bodyB
* @type body
* @default null
*/
bodyB?: BodyType;
/**
* A `Vector` that specifies the offset of the constraint from center of the `constraint.bodyA` if defined, otherwise a world-space position.
*
* @property pointA
* @type vector
* @default { x: 0, y: 0 }
*/
pointA: Vector;
/**
* A `Vector` that specifies the offset of the constraint from center of the `constraint.bodyB` if defined, otherwise a world-space position.
*
* @property pointB
* @type vector
* @default { x: 0, y: 0 }
*/
pointB: Vector;
/**
* A `Number` that specifies the target resting length of the constraint.
* It is calculated automatically in `Constraint.create` from initial positions of the `constraint.bodyA` and `constraint.bodyB`.
*
* @property length
* @type number
*/
length: number;
/**
* An integer `Number` uniquely identifying number generated in `Composite.create` by `Common.nextId`.
*
* @property id
* @type number
*/
id: number;
/**
* An arbitrary `String` name to help the user identify and manage bodies.
*
* @property label
* @type string
* @default "Constraint"
*/
label: string;
/**
* A `String` denoting the type of object.
*
* @property type
* @type string
* @default "constraint"
* @readOnly
*/
type: string;
/**
* A `Number` that specifies the stiffness of the constraint, i.e. the rate at which it returns to its resting `constraint.length`.
* A value of `1` means the constraint should be very stiff.
* A value of `0.2` means the constraint acts like a soft spring.
*
* @property stiffness
* @type number
* @default 1
*/
stiffness: number;
/**
* A `Number` that specifies the damping of the constraint,
* i.e. the amount of resistance applied to each body based on their velocities to limit the amount of oscillation.
* Damping will only be apparent when the constraint also has a very low `stiffness`.
* A value of `0.1` means the constraint will apply heavy damping, resulting in little to no oscillation.
* A value of `0` means the constraint will apply no damping.
*
* @property damping
* @type number
* @default 0
*/
damping: number;
/**
* A `Number` that specifies the angular stiffness of the constraint.
*
* @property angularStiffness
* @type number
* @default 0
*/
angularStiffness: number;
/**
* Either the angle of BodyA, or a config value.
*
* @property angleA
* @type number
* @default 0
*/
angleA: number;
/**
* Either the angle of BodyB, or a config value.
*
* @property angleB
* @type number
* @default 0
*/
angleB: number;
/**
* An object reserved for storing plugin-specific properties.
*
* @property plugin
* @type {}
*/
plugin: any;
/**
* An `Object` that defines the rendering properties to be consumed by the module `Matter.Render`.
*
* @property render
* @type object
*/
render: IConstraintRenderDefinition;
};
type BodyType = {
/**
* An integer `Number` uniquely identifying number generated in `Body.create` by `Common.nextId`.
*
* @property id
* @type number
*/
id: number;
/**
* A `String` denoting the type of object.
*
* @property type
* @type string
* @default "body"
* @readOnly
*/
type: string;
/**
* An arbitrary `String` name to help the user identify and manage bodies.
*
* @property label
* @type string
* @default "Body"
*/
label: string;
/**
* An array of bodies that make up this body.
* The first body in the array must always be a self reference to the current body instance.
* All bodies in the `parts` array together form a single rigid compound body.
* Parts are allowed to overlap, have gaps or holes or even form concave bodies.
* Parts themselves should never be added to a `World`, only the parent body should be.
* Use `Body.setParts` when setting parts to ensure correct updates of all properties.
*
* @property parts
* @type body[]
*/
parts: BodyType[];
/**
* An object reserved for storing plugin-specific properties.
*
* @property plugin
* @type {}
*/
plugin: any;
/**
* A `Number` specifying the angle of the body, in radians.
*
* @property angle
* @type number
* @default 0
*/
angle: number;
/**
* A `Number` specifying the delta time, in ms, since the last game step
*/
deltaTime: number;
/**
* An array of `Vector` objects that specify the convex hull of the rigid body.
* These should be provided about the origin `(0, 0)`. E.g.
*
* [{ x: 0, y: 0 }, { x: 25, y: 50 }, { x: 50, y: 0 }]
*
* When passed via `Body.create`, the vertices are translated relative to `body.position` (i.e. world-space, and constantly updated by `Body.update` during simulation).
* The `Vector` objects are also augmented with additional properties required for efficient collision detection.
*
* Other properties such as `inertia` and `bounds` are automatically calculated from the passed vertices (unless provided via `options`).
* Concave hulls are not currently supported. The module `Matter.Vertices` contains useful methods for working with vertices.
*
* @property vertices
* @type vector[]
*/
vertices?: Vector[];
/**
* A `Vector` that specifies the current world-space position of the body.
*
* @property position
* @type vector
* @default { x: 0, y: 0 }
*/
position: Vector;
/**
* A `Vector` that specifies the force to apply in the current step. It is zeroed after every `Body.update`. See also `Body.applyForce`.
*
* @property force
* @type vector
* @default { x: 0, y: 0 }
*/
force: Vector;
/**
* A `Number` that specifies the torque (turning force) to apply in the current step. It is zeroed after every `Body.update`.
*
* @property torque
* @type number
* @default 0
*/
torque: number;
/**
* A `Vector` that specifies the position impulse.
*
* @property positionImpulse
* @type vector
* @default { x: 0, y: 0 }
*/
positionImpulse: Vector;
/**
* A `Vector` that specifies the previous position impulse.
*
* @property previousPositionImpulse
* @type vector
* @default { x: 0, y: 0 }
*/
previousPositionImpulse: Vector;
/**
* A `Vector` that specifies the constraint impulse.
*
* @property constraintImpulse
* @type vector
* @default { x: 0, y: 0 }
*/
constraintImpulse: Vector;
/**
* The total number of contacts.
*
* @property totalContacts
* @type number
* @default 0
*/
totalContacts: number;
/**
* A `Number` that _measures_ the current speed of the body after the last `Body.update`. It is read-only and always positive (it's the magnitude of `body.velocity`).
*
* @readOnly
* @property speed
* @type number
* @default 0
*/
speed: number;
/**
* A `Number` that _measures_ the current angular speed of the body after the last `Body.update`. It is read-only and always positive (it's the magnitude of `body.angularVelocity`).
*
* @readOnly
* @property angularSpeed
* @type number
* @default 0
*/
angularSpeed: number;
/**
* A `Vector` that _measures_ the current velocity of the body after the last `Body.update`. It is read-only.
* If you need to modify a body's velocity directly, you should either apply a force or simply change the body's `position` (as the engine uses position-Verlet integration).
*
* @readOnly
* @property velocity
* @type vector
* @default { x: 0, y: 0 }
*/
velocity: Vector;
/**
* A `Number` that _measures_ the current angular velocity of the body after the last `Body.update`. It is read-only.
* If you need to modify a body's angular velocity directly, you should apply a torque or simply change the body's `angle` (as the engine uses position-Verlet integration).
*
* @readOnly
* @property angularVelocity
* @type number
* @default 0
*/
angularVelocity: number;
/**
* A flag that indicates whether a body is a sensor. Sensor triggers collision events, but doesn't react with colliding body physically.
*
* @property isSensor
* @type boolean
* @default false
*/
isSensor: boolean;
/**
* A flag that indicates whether a body is considered static. A static body can never change p