new Body(sprite)
The Physics Body is linked to a single Sprite and defines properties that determine how the physics body is simulated. These properties affect how the body reacts to forces, what forces it generates on itself (to simulate friction), and how it reacts to collisions in the scene. In most cases, the properties are used to simulate physical effects. Each body also has its own property values that determine exactly how it reacts to forces and collisions in the scene.
Parameters:
Name | Type | Description |
---|---|---|
sprite |
Phaser.Sprite | The Sprite object this physics body belongs to. |
- Source:
Members
-
acceleration
-
- Source:
Properties:
Name Type Description acceleration
Phaser.Point The acceleration in pixels per second sq. of the Body.
-
allowGravity
-
- Default Value:
- true
- Source:
Properties:
Name Type Description allowGravity
boolean Allow this Body to be influenced by the global Gravity value? Note: It will always be influenced by the local gravity if set.
-
allowRotation
-
- Default Value:
- true
- Source:
Properties:
Name Type Description allowRotation
boolean Allow angular rotation? This will cause the Sprite to be rotated via angularVelocity, etc.
-
angle
-
- Source:
Properties:
Name Type Description angle
number The angle of the Body based on its velocity in radians.
-
angularAcceleration
-
- Default Value:
- 0
- Source:
Properties:
Name Type Description angularAcceleration
number The angular acceleration of the Body.
-
angularDrag
-
- Default Value:
- 0
- Source:
Properties:
Name Type Description angularDrag
number angularDrag is used to calculate friction on the body as it rotates.
-
angularVelocity
-
- Default Value:
- 0
- Source:
Properties:
Name Type Description angularVelocity
number The angular velocity of the Body.
-
blocked
-
This object is populated with boolean values when the Body collides with the World bounds or a Tile. For example if blocked.up is true then the Body cannot move up.
- Source:
Properties:
Name Type Description blocked
object An object containing on which faces this Body is blocked from moving, if any.
-
<readonly> bottom
-
- Source:
Properties:
Name Type Description bottom
number The bottom-most point of this Body.
-
bounce
-
- Source:
Properties:
Name Type Description bounce
Phaser.Point The elasticitiy of the Body when colliding. This property determines how much energy a body maintains during a collision, i.e. its bounciness.
-
checkCollision
-
Set the checkCollision properties to control which directions collision is processed for this Body. For example checkCollision.up = false means it won't collide when the collision happened while moving up.
- Source:
Properties:
Name Type Description checkCollision
object An object containing allowed collision.
-
collideCallback
-
- Default Value:
- null
- Source:
Properties:
Name Type Description collideCallback
function If set this callback will be fired whenever this Body is hit (on any face). It will send three parameters, the face it hit on, this Body and the Body that hit it.
-
collideCallbackContext
-
- Default Value:
- null
- Source:
Properties:
Name Type Description collideCallbackContext
object The context in which the collideCallback is called.
-
collideWorldBounds
-
A Body can be set to collide against the World bounds automatically and rebound back into the World if this is set to true. Otherwise it will leave the World.
- Source:
Properties:
Name Type Description collideWorldBounds
boolean Should the Body collide with the World bounds?
-
<protected> contacts
-
- Source:
Properties:
Name Type Description contacts
array.<Phaser.Physics.Arcade.Body> Used to store references to bodies this Body is in contact with.
-
customSeparateCallback
-
- Default Value:
- null
- Source:
Properties:
Name Type Description customSeparateCallback
function If set this callback will be used for Body separation instead of the built-in one. Callback should return true if separated, otherwise false.
-
customSeparateContext
-
- Default Value:
- null
- Source:
Properties:
Name Type Description customSeparateContext
object The context in which the customSeparateCallback is called.
-
facing
-
- Source:
Properties:
Name Type Description facing
number A const reference to the direction the Body is traveling or facing.
-
game
-
- Source:
Properties:
Name Type Description game
Phaser.Game Local reference to game.
-
gravity
-
- Source:
Properties:
Name Type Description gravity
Phaser.Point The gravity applied to the motion of the Body. This works in addition to any gravity set on the world.
-
<readonly> height
-
- Source:
Properties:
Name Type Description height
number The current height of the Body, taking into account the point rotation.
-
immovable
-
- Default Value:
- false
- Source:
Properties:
Name Type Description immovable
boolean An immovable Body will not receive any impacts or exchanges of velocity from other bodies.
-
<readonly> left
-
- Source:
Properties:
Name Type Description left
number The left-most point of this Body.
-
linearDamping
-
- Default Value:
- 0.0
- Source:
Properties:
Name Type Description linearDamping
number linearDamping is used to calculate friction on the body as it moves through the world. For example, this might be used to simulate air or water friction.
-
mass
-
- Default Value:
- 1
- Source:
Properties:
Name Type Description mass
number The mass property determines how forces affect the body, as well as how much momentum the body has when it is involved in a collision.
-
maxAngular
-
- Default Value:
- 1000
- Source:
Properties:
Name Type Description maxAngular
number The maximum angular velocity that the Body can reach.
-
maxVelocity
-
- Source:
Properties:
Name Type Description maxVelocity
Phaser.Point The maximum velocity that the Body can reach.
-
minVelocity
-
- Source:
Properties:
Name Type Description minVelocity
Phaser.Point When a body rebounds off another body or a wall the minVelocity is checked. If the new velocity is lower than minVelocity the body is stopped.
-
moves
-
- Default Value:
- true
- Source:
Properties:
Name Type Description moves
boolean Set to true to allow the Physics system (such as velocity) to move this Body, or false to move it manually.
-
offset
-
- Source:
Properties:
Name Type Description offset
Phaser.Point The offset of the Physics Body from the Sprite x/y position.
-
<protected> overlapX
-
- Source:
Properties:
Name Type Description overlapX
number Mostly used internally to store the overlap values from Tile seperation.
-
<protected> overlapY
-
- Source:
Properties:
Name Type Description overlapY
number Mostly used internally to store the overlap values from Tile seperation.
-
polygon
-
- Source:
Properties:
Name Type Description polygon
SAT.Polygon The SAT Polygons, as derived from the Shape.
-
<readonly> preRotation
-
- Source:
Properties:
Name Type Description preRotation
number The previous rotation of the physics body.
-
<readonly> preX
-
- Source:
Properties:
Name Type Description preX
number The previous x position of the physics body.
-
<readonly> preY
-
- Source:
Properties:
Name Type Description preY
number The previous y position of the physics body.
-
rebound
-
- Default Value:
- true
- Source:
Properties:
Name Type Description rebound
boolean A Body set to rebound will exchange velocity with another Body during collision. Set to false to allow this body to be 'pushed' rather than exchange velocity.
-
<readonly> right
-
- Source:
Properties:
Name Type Description right
number The right-most point of this Body.
-
rotation
-
- Default Value:
- 0
- Source:
Properties:
Name Type Description rotation
number The amount the parent Sprite is rotated.
-
shape
-
- Source:
Properties:
Name Type Description shape
SAT.Box | SAT.Circle | SAT.Polygon The SAT Collision shape.
-
speed
-
- Source:
Properties:
Name Type Description speed
number The speed in pixels per second sq. of the Body.
-
sprite
-
- Source:
Properties:
Name Type Description sprite
Phaser.Sprite Reference to the parent Sprite.
-
<readonly> top
-
- Source:
Properties:
Name Type Description top
number The top-most point of this Body.
-
touching
-
This object is populated with boolean values when the Body collides with another. touching.up = true means the collision happened to the top of this Body for example.
- Source:
Properties:
Name Type Description touching
object An object containing touching results.
-
type
-
- Source:
Properties:
Name Type Description type
Phaser.Physics.Arcade.RECT | Phaser.Physics.Arcade.CIRCLE The type of SAT Shape.
-
velocity
-
- Source:
Properties:
Name Type Description velocity
Phaser.Point The velocity of the Body.
-
<readonly> width
-
- Source:
Properties:
Name Type Description width
number The current width of the Body, taking into account the point rotation.
-
x
-
- Source:
Properties:
Name Type Description x
number The x coordinate of this Body.
-
y
-
- Source:
Properties:
Name Type Description y
number The y coordinate of this Body.
Methods
-
<protected> add(v)
-
Adds the given Vector to this Body.
Parameters:
Name Type Description v
SAT.Vector The vector to add to this Body.
- Source:
-
addContact(body) → {boolean}
-
Adds the given Body to the contact list of this Body. Also adds this Body to the contact list of the given Body.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body to be added.
- Source:
Returns:
True if the given Body was added to this contact list, false if already on it.
- Type
- boolean
-
<protected> applyDamping()
-
Internal method that checks the acceleration and applies damping if not set.
- Source:
-
<protected> checkBlocked()
-
Internal method that checks and potentially resets the blocked status flags.
- Source:
-
deltaX() → {number}
-
Returns the delta x value. The amount the Body has moved horizontally in the current step.
- Source:
Returns:
The delta value. Positive if the motion was to the right, negative if to the left.
- Type
- number
-
deltaY() → {number}
-
Returns the delta y value. The amount the Body has moved vertically in the current step.
- Source:
Returns:
The delta value. Positive if the motion was downwards, negative if upwards.
- Type
- number
-
deltaZ() → {number}
-
Returns the delta z value. The amount the Body has rotated in the current step.
- Source:
Returns:
The delta value.
- Type
- number
-
destroy()
-
Destroys this Body and all references it holds to other objects.
- Source:
-
<protected> exchange(body)
-
Exchange velocity with the given Body.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body that collided.
- Source:
-
getDownwardForce() → {number}
-
Gets the total force being applied on the X axis, including gravity and velocity.
- Source:
Returns:
The total force being applied on the Y axis.
- Type
- number
-
getUpwardForce() → {number}
-
Gets the total force being applied on the X axis, including gravity and velocity.
- Source:
Returns:
The total force being applied on the X axis.
- Type
- number
-
<protected> give(body, response)
-
Separation response handler.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body that collided.
response
SAT.Response The SAT Response object containing the collision data.
- Source:
-
<protected> hitBottom(body, response)
-
Process a collision with the bottom face of this Body. Collision and separation can be further checked by setting a collideCallback. This callback will be sent 4 parameters: The face of collision, this Body, the colliding Body and the SAT Response. If the callback returns true then separation, rebounds and the touching flags will all be set. If it returns false this will be skipped and must be handled manually.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body that collided.
response
SAT.Response The SAT Response object containing the collision data.
- Source:
-
<protected> hitLeft(body, response)
-
Process a collision with the left face of this Body. Collision and separation can be further checked by setting a collideCallback. This callback will be sent 4 parameters: The face of collision, this Body, the colliding Body and the SAT Response. If the callback returns true then separation, rebounds and the touching flags will all be set. If it returns false this will be skipped and must be handled manually.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body that collided.
response
SAT.Response The SAT Response object containing the collision data.
- Source:
-
<protected> hitRight(body, response)
-
Process a collision with the right face of this Body. Collision and separation can be further checked by setting a collideCallback. This callback will be sent 4 parameters: The face of collision, this Body, the colliding Body and the SAT Response. If the callback returns true then separation, rebounds and the touching flags will all be set. If it returns false this will be skipped and must be handled manually.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body that collided.
response
SAT.Response The SAT Response object containing the collision data.
- Source:
-
<protected> hitTop(body, response)
-
Process a collision with the top face of this Body. Collision and separation can be further checked by setting a collideCallback. This callback will be sent 4 parameters: The face of collision, this Body, the colliding Body and the SAT Response. If the callback returns true then separation, rebounds and the touching flags will all be set. If it returns false this will be skipped and must be handled manually.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body that collided.
response
SAT.Response The SAT Response object containing the collision data.
- Source:
-
inContact(body) → {boolean}
-
Checks if this Body is already in contact with the given Body.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body to be checked.
- Source:
Returns:
True if the given Body is already in contact with this Body.
- Type
- boolean
-
<protected> integrateVelocity()
-
Internal method. Integrates velocity, global gravity and the delta timer.
- Source:
-
onFloor() → {boolean}
-
Determines if this Body is 'on the floor', which means in contact with a Tile or World bounds, or other object that has set 'down' as blocked.
- Source:
Returns:
True if this Body is 'on the floor', which means in contact with a Tile or World bounds, or object that has set 'down' as blocked.
- Type
- boolean
-
onWall() → {boolean}
-
Determins if this Body is 'on a wall', which means horizontally in contact with a Tile or World bounds, or other object but not the ground.
- Source:
Returns:
True if this Body is 'on a wall', which means horizontally in contact with a Tile or World bounds, or other object but not the ground.
- Type
- boolean
-
overlap(body, response) → {boolean}
-
Checks for an overlap between this Body and the given Body.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body that is being checked against this Body.
response
SAT.Response SAT Response handler.
- Source:
Returns:
True if the two bodies overlap, otherwise false.
- Type
- boolean
-
<protected> postUpdate()
-
Internal method. This is called directly before the sprites are sent to the renderer and after the update function has finished.
- Source:
-
<protected> preUpdate()
-
Internal method that updates the Body position in relation to the parent Sprite.
- Source:
-
<protected> processRebound(body)
-
Rebound the velocity of this Body.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body that collided.
- Source:
-
<protected> reboundCheck(x, y, rebound)
-
Check if we're below minVelocity and gravity isn't trying to drag us in the opposite direction.
Parameters:
Name Type Description x
boolean Check the X axis?
y
boolean Check the Y axis?
rebound
boolean If true it will reverse the velocity on the given axis
- Source:
-
removeContact(body) → {boolean}
-
Removes the given Body from the contact list of this Body. Also removes this Body from the contact list of the given Body.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body to be removed.
- Source:
Returns:
True if the given Body was removed from this contact list, false if wasn't on it.
- Type
- boolean
-
reset(full)
-
Resets the Body motion values: velocity, acceleration, angularVelocity and angularAcceleration. Also resets the forces to defaults: gravity, bounce, minVelocity,maxVelocity, angularDrag, maxAngular, mass, friction and checkCollision if 'full' specified.
Parameters:
Name Type Argument Default Description full
boolean <optional>
false A full reset clears down settings you may have set, such as gravity, bounce and drag. A non-full reset just clears motion values.
- Source:
-
<protected> separate(body, response) → {boolean}
-
This separates this Body from the given Body unless a customSeparateCallback is set. It assumes they have already been overlap checked and the resulting overlap is stored in the SAT response.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body to be separated from this one.
response
SAT.Response SAT Response handler.
- Source:
Returns:
True if the bodies were separated, false if not (for example checkCollide allows them to pass through)
- Type
- boolean
-
setCircle(radius, offsetX, offsetY)
-
Sets this Body to use a circle of the given radius for all collision. The Circle will be centered on the center of the Sprite by default, but can be adjusted via the Body.offset property and the setCircle x/y parameters.
Parameters:
Name Type Argument Default Description radius
number The radius of this circle (in pixels)
offsetX
number <optional>
0 The x amount the circle will be offset from the Sprites center.
offsetY
number <optional>
0 The y amount the circle will be offset from the Sprites center.
- Source:
-
setPolygon(points)
-
Sets this Body to use a convex polygon for collision. The points are specified in a counter-clockwise direction and must create a convex polygon. Use Body.translate and/or Body.offset to re-position the polygon from the Sprite origin.
Parameters:
Name Type Description points
Array.<SAT.Vector> | Array.<number> | SAT.Vector | number This can be an array of Vectors that form the polygon, a flat array of numbers that will be interpreted as [x,y, x,y, ...], or the arguments passed can be all the points of the polygon e.g.
setPolygon(new SAT.Vector(), new SAT.Vector(), ...)
, or the arguments passed can be flat x,y values e.g.setPolygon(x,y, x,y, x,y, ...)
wherex
andy
are Numbers.- Source:
-
setRectangle(width, height, translateX, translateY)
-
Sets this Body to use a rectangle for all collision. If you don't specify any parameters it will be sized to match the parent Sprites current width and height (including scale factor) and centered on the sprite.
Parameters:
Name Type Argument Description width
number <optional>
The width of the rectangle. If not specified it will default to the width of the parent Sprite.
height
number <optional>
The height of the rectangle. If not specified it will default to the height of the parent Sprite.
translateX
number <optional>
The x amount the rectangle will be translated from the Sprites center.
translateY
number <optional>
The y amount the rectangle will be translated from the Sprites center.
- Source:
-
<protected> split(body, response)
-
Split the collision response evenly between the two bodies.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body that collided.
response
SAT.Response The SAT Response object containing the collision data.
- Source:
-
<protected> sub(v)
-
Subtracts the given Vector from this Body.
Parameters:
Name Type Description v
SAT.Vector The vector to substract from this Body.
- Source:
-
<protected> take(body, response)
-
Separation response handler.
Parameters:
Name Type Description body
Phaser.Physics.Arcade.Body The Body that collided.
response
SAT.Response The SAT Response object containing the collision data.
- Source:
-
translate(x, y)
-
Used for translating rectangle and polygon bodies from the Sprite parent. Doesn't apply to Circles. See also the Body.offset property.
Parameters:
Name Type Description x
number The x amount the polygon or rectangle will be translated by from the Sprite.
y
number The y amount the polygon or rectangle will be translated by from the Sprite.
- Source:
-
<protected> updateBounds()
-
Internal method that updates the left, right, top, bottom, width and height properties.
- Source:
-
<protected> updateScale()
-
Internal method that updates the Body scale in relation to the parent Sprite.
- Source: