The Physics Body is linked to a single Sprite. All physics operations should be performed against the body rather than the Sprite itself. For example you can set the velocity, acceleration, bounce values etc all on the Body. @class Phaser.Physics.Arcade.Body @constructor * @param {Phaser.Sprite} sprite - The Sprite object this physics body belongs to.
Constructor
Variables
data:Dynamic = null
A property to hold any data related to this body. Can be useful if building a larger system on top of this one.
index:Int = -1
A property to hold any index value related to this body. Can be useful if building a larger system on top of this one.
read onlygroups:Array<Group> = null
The list of groups that contain this body (can be null if there are no groups).
enable:Bool = true
@property {boolean} enable - A disabled body won't be checked for any form of collision or overlap or have its pre/post updates run. @default
forceX:Bool = false
If true
World.separate will always separate on the X axis before Y when this body is involved. Otherwise it will check gravity totals first.
isCircle:Bool = false
If true
this Body is using circular collision detection. If false
it is using rectangular.
Use Body.setCircle
to control the collision shape this Body uses.
@property {boolean} isCircle
@default
@readOnly
radius:Float = 0
The radius of the circular collision shape this Body is using if Body.setCircle has been enabled, relative to the Sprite's texture. If you wish to change the radius then call {@link #setCircle} again with the new value. If you wish to stop the Body using a circle then call {@link #setCircle} with a radius of zero (or undefined). The actual radius of the Body (at any Sprite scale) is equal to {@link #halfWidth} and the diameter is equal to {@link #width}. @property {number} radius @default @readOnly
allowRotation:Bool = true
@property {boolean} allowRotation - Allow this Body to be rotated? (via angularVelocity, etc) @default
rotation:Float = 0
The Body's rotation in degrees, as calculated by its angularVelocity and angularAcceleration. Please understand that the collision Body itself never rotates, it is always axis-aligned. However these values are passed up to the parent Sprite and updates its rotation. @property {number} rotation
preRotation:Float = 0
@property {number} preRotation - The previous rotation of the physics body, in degrees. @readonly
halfWidth:Float = 0
@property {number} halfWidth - The calculated width / 2 of the physics body. @readonly
halfHeight:Float = 0
@property {number} halfHeight - The calculated height / 2 of the physics body. @readonly
newVelocityX:Float = 0
@property {Phaser.Point} newVelocity - The distanced traveled during the last update, equal to velocity * physicsElapsed
. Calculated during the Body.preUpdate and applied to its position.
@readonly
allowDrag:Bool = true
@property {boolean} allowDrag - Allow this Body to be influenced by {@link #drag}? @default
allowGravity:Bool = true
@property {boolean} allowGravity - Allow this Body to be influenced by gravity? Either world or local. @default
useWorldBounce:Bool = false
The elasticity of the Body when colliding with the World bounds.
By default this property is null
, in which case Body.bounce
is used instead. Set this property
to a Phaser.Point object in order to enable a World bounds specific bounce value.
@property {Phaser.Point} useWorldBounce
onWorldBounds:(Body, Bool, Bool, Bool, Bool) ‑> Void = null
A Signal that is dispatched when this Body collides with the world bounds.
Due to the potentially high volume of signals this could create it is disabled by default.
To use this feature set this property to a Phaser.Signal: sprite.body.onWorldBounds = new Phaser.Signal()
and it will be called when a collision happens, passing five arguments:
onWorldBounds(sprite, up, down, left, right)
where the Sprite is a reference to the Sprite that owns this Body, and the other arguments are booleans
indicating on which side of the world the Body collided.
@property {Phaser.Signal} onWorldBounds
onCollide:(Body, Body) ‑> Void = null
A Signal that is dispatched when this Body collides with another Body.
You still need to call game.physics.arcade.collide
in your update
method in order
for this signal to be dispatched.
Usually you'd pass a callback to the collide
method, but this signal provides for
a different level of notification.
Due to the potentially high volume of signals this could create it is disabled by default.
To use this feature set this property to a Phaser.Signal: sprite.body.onCollide = new Phaser.Signal()
and it will be called when a collision happens, passing two arguments: the sprites which collided.
The first sprite in the argument is always the owner of this Body.
If two Bodies with this Signal set collide, both will dispatch the Signal. @property {Phaser.Signal} onCollide
onOverlap:(Body, Body) ‑> Void = null
A Signal that is dispatched when this Body overlaps with another Body.
You still need to call game.physics.arcade.overlap
in your update
method in order
for this signal to be dispatched.
Usually you'd pass a callback to the overlap
method, but this signal provides for
a different level of notification.
Due to the potentially high volume of signals this could create it is disabled by default.
To use this feature set this property to a Phaser.Signal: sprite.body.onOverlap = new Phaser.Signal()
and it will be called when a collision happens, passing two arguments: the sprites which collided.
The first sprite in the argument is always the owner of this Body.
If two Bodies with this Signal set collide, both will dispatch the Signal. @property {Phaser.Signal} onOverlap
angularVelocity:Float = 0
@property {number} angularVelocity - The angular velocity is the rate of change of the Body's rotation. It is measured in degrees per second. @default
angularAcceleration:Float = 0
@property {number} angularAcceleration - The angular acceleration is the rate of change of the angular velocity. Measured in degrees per second squared. @default
angularDrag:Float = 0
@property {number} angularDrag - The drag applied during the rotation of the Body. Measured in degrees per second squared. @default
maxAngularVelocity:Float = 1000
@property {number} maxAngularVelocity - The maximum angular velocity in degrees per second that the Body can reach. @default
mass:Float = 1
@property {number} mass - The mass of the Body. When two bodies collide their mass is used in the calculation to determine the exchange of velocity. @default
speed:Float = 0
@property {number} speed - The speed of the Body in pixels per second, equal to the magnitude of the velocity. @readonly
facing:Direction = Direction.NONE
@property {number} facing - A const reference to the direction the Body is traveling or facing: Phaser.NONE, Phaser.LEFT, Phaser.RIGHT, Phaser.UP, or Phaser.DOWN. If the Body is moving on both axes, UP and DOWN take precedence. @default
immovable:Bool = false
@property {boolean} immovable - An immovable Body will not receive any impacts from other bodies. Two immovable Bodies can't separate or exchange momentum and will pass through each other. @default
moves:Bool = true
Whether the physics system should update the Body's position and rotation based on its velocity, acceleration, drag, and gravity.
If you have a Body that is being moved around the world via a tween or a Group motion, but its local x/y position never actually changes, then you should set Body.moves = false. Otherwise it will most likely fly off the screen. If you want the physics system to move the body around, then set moves to true.
A Body with moves = false can still be moved slightly (but not accelerated) during collision separation unless you set {@link #immovable} as well.
@property {boolean} moves - Set to true to allow the Physics system to move this Body, otherwise false to move it manually. @default
customSeparateX:Bool = false
This flag allows you to disable the custom x separation that takes place by Physics.Arcade.separate. Used in combination with your own collision processHandler you can create whatever type of collision response you need. @property {boolean} customSeparateX - Use a custom separation system or the built-in one? @default
customSeparateY:Bool = false
This flag allows you to disable the custom y separation that takes place by Physics.Arcade.separate. Used in combination with your own collision processHandler you can create whatever type of collision response you need. @property {boolean} customSeparateY - Use a custom separation system or the built-in one? @default
overlapX:Float = 0
When this body collides with another, the amount of overlap is stored here. @property {number} overlapX - The amount of horizontal overlap during the collision.
overlapY:Float = 0
When this body collides with another, the amount of overlap is stored here. @property {number} overlapY - The amount of vertical overlap during the collision.
overlapR:Float = 0
If Body.isCircle
is true, and this body collides with another circular body, the amount of overlap is stored here.
@property {number} overlapR - The amount of overlap during the collision.
embedded:Bool = false
If a body is overlapping with another body, but neither of them are moving (maybe they spawned on-top of each other?) this is set to true. @property {boolean} embedded - Body embed value.
collideWorldBounds:Bool = false
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. @property {boolean} collideWorldBounds - Should the Body collide with the World bounds?
blockedNone:Bool = true
@property {boolean} blockedNone - If this Body being blocked by world bounds or another immovable object?
blockedUp:Bool = false
@property {boolean} blockedNone - If this Body being blocked by upper world bounds or another immovable object above it?
blockedDown:Bool = false
@property {boolean} blockedNone - If this Body being blocked by lower world bounds or another immovable object below it?
blockedLeft:Bool = false
@property {boolean} blockedNone - If this Body being blocked by left world bounds or another immovable object on the left?
blockedRight:Bool = false
@property {boolean} blockedNone - If this Body being blocked by right world bounds or another immovable object on the right?
dirty:Bool = false
@property {boolean} dirty - If this Body in a preUpdate (true) or postUpdate (false) state?
skipQuadTree:Bool = false
@property {boolean} skipQuadTree - If true and you collide this Sprite against a Group, it will disable the collision check from using a QuadTree.
stopVelocityOnCollide:Bool = true
@property {boolean} stopVelocityOnCollide - Set by the moveTo
and moveFrom
methods.
onMoveComplete:(Body, Bool) ‑> Void = null
@property {Phaser.Signal} onMoveComplete - Listen for the completion of moveTo
or moveFrom
events.
movementCallback:(body:Body, velocityX:Float, velocityY:Float, percent:Float) ‑> Bool = null
@property {function} movementCallback - Optional callback. If set, invoked during the running of moveTo
or moveFrom
events.
Note: this is not an event (emit{X}) because we are expecting a boolean return value.
Methods
inlineupdateCenter():Void
Update the Body's center from its position.
@method Phaser.Physics.Arcade.Body#updateCenter @protected
inlinestopMovement(stopVelocity:Bool):Void
If this Body is moving as a result of a call to moveTo
or moveFrom
(i.e. it
has Body.isMoving true), then calling this method will stop the movement before
either the duration or distance counters expire.
The onMoveComplete
signal is dispatched.
@method Phaser.Physics.Arcade.Body#stopMovement
Parameters:
{boolean} | [stopVelocity] - Should the Body.velocity be set to zero? |
---|
moveFrom(duration:Float, speed:Float = -999999999.0, direction:Float = -999999999.0):Bool
Note: This method is experimental, and may be changed or removed in a future release.
This method moves the Body in the given direction, for the duration specified.
It works by setting the velocity on the Body, and an internal timer, and then
monitoring the duration each frame. When the duration is up the movement is
stopped and the Body.onMoveComplete
signal is dispatched.
Movement also stops if the Body collides or overlaps with any other Body.
You can control if the velocity should be reset to zero on collision, by using
the property Body.stopVelocityOnCollide
.
Stop the movement at any time by calling Body.stopMovement
.
You can optionally set a speed in pixels per second. If not specified it
will use the current Body.speed
value. If this is zero, the function will return false.
Please note that due to browser timings you should allow for a variance in when the duration will actually expire. Depending on system it may be as much as +- 50ms. Also this method doesn't take into consideration any other forces acting on the Body, such as Gravity, drag or maxVelocity, all of which may impact the movement.
@method Phaser.Physics.Arcade.Body#moveFrom
Parameters:
{number} | duration - The duration of the movement, in seconds. |
---|---|
{number} | [speed] - The speed of the movement, in pixels per second. If not provided |
{number} | [direction] - The angle of movement in degrees. If not provided |
Returns:
{boolean} True if the movement successfully started, otherwise false.
moveTo(duration:Float, distance:Float, direction:Float = -999999999.0):Bool
Note: This method is experimental, and may be changed or removed in a future release.
This method moves the Body in the given direction, for the duration specified.
It works by setting the velocity on the Body, and an internal distance counter.
The distance is monitored each frame. When the distance equals the distance
specified in this call, the movement is stopped, and the Body.onMoveComplete
signal is dispatched.
Movement also stops if the Body collides or overlaps with any other Body.
You can control if the velocity should be reset to zero on collision, by using
the property Body.stopVelocityOnCollide
.
Stop the movement at any time by calling Body.stopMovement
.
Please note that due to browser timings you should allow for a variance in when the distance will actually expire.
Note: This method doesn't take into consideration any other forces acting on the Body, such as Gravity, drag or maxVelocity, all of which may impact the movement.
@method Phaser.Physics.Arcade.Body#moveTo
Parameters:
{float} | duration - The duration of the movement, in seconds. |
---|---|
{float} | distance - The distance, in pixels, the Body will move. |
{float} | [direction] - The angle of movement. If not provided |
Returns:
{boolean} True if the movement successfully started, otherwise false.
setCircle(radius:Float):Void
Sets this Body as using a circle, of the given radius, for all collision detection instead of a rectangle. The radius is given in pixels (relative to the Sprite's texture) and is the distance from the center of the circle to the edge.
You can also control the x and y offset, which is the position of the Body relative to the top-left of the Sprite's texture.
To change a Body back to being rectangular again call Body.setSize
.
Note: Circular collision only happens with other Arcade Physics bodies, it does not work against tile maps, where rectangular collision is the only method supported.
@method Phaser.Physics.Arcade.Body#setCircle
Parameters:
{number} | [radius] - The radius of the Body in pixels. Pass a value of zero / undefined, to stop the Body using a circle for collision. |
---|
inlinereset(x:Float, y:Float, width:Float, height:Float, rotation:Float = 0):Void
Resets all Body values (velocity, acceleration, rotation, etc)
@method Phaser.Physics.Arcade.Body#reset
Parameters:
{number} | x - The new x position of the Body. |
---|---|
{number} | y - The new y position of the Body. |
inlinestop():Void
Sets acceleration, velocity, and {@link #speed} to 0.
@method Phaser.Physics.Arcade.Body#stop
inlinehitTest(x:Float, y:Float):Bool
Tests if a world point lies within this Body.
@method Phaser.Physics.Arcade.Body#hitTest
Parameters:
{number} | x - The world x coordinate to test. |
---|---|
{number} | y - The world y coordinate to test. |
Returns:
{boolean} True if the given coordinates are inside this Body, otherwise false.
inlineisOnFloor():Bool
Returns true if the bottom of this Body is in contact with either the world bounds or a tile.
@method Phaser.Physics.Arcade.Body#onFloor
Returns:
{boolean} True if in contact with either the world bounds or a tile.
inlineisOnCeiling():Bool
Returns true if the top of this Body is in contact with either the world bounds or a tile.
@method Phaser.Physics.Arcade.Body#onCeiling
Returns:
{boolean} True if in contact with either the world bounds or a tile.
inlineisOnWall():Bool
Returns true if either side of this Body is in contact with either the world bounds or a tile.
@method Phaser.Physics.Arcade.Body#onWall
Returns:
{boolean} True if in contact with either the world bounds or a tile.
inlinedeltaAbsX():Float
Returns the absolute delta x value.
@method Phaser.Physics.Arcade.Body#deltaAbsX
Returns:
{number} The absolute delta value.
inlinedeltaAbsY():Float
Returns the absolute delta y value.
@method Phaser.Physics.Arcade.Body#deltaAbsY
Returns:
{number} The absolute delta value.
inlinedeltaX():Float
Returns the delta x value. The difference between Body.x now and in the previous step.
@method Phaser.Physics.Arcade.Body#deltaX
Returns:
{number} The delta value. Positive if the motion was to the right, negative if to the left.
inlinedeltaY():Float
Returns the delta y value. The difference between Body.y now and in the previous step.
@method Phaser.Physics.Arcade.Body#deltaY
Returns:
{number} The delta value. Positive if the motion was downwards, negative if upwards.
inlinedeltaZ():Float
Returns the delta z value. The difference between Body.rotation now and in the previous step.
@method Phaser.Physics.Arcade.Body#deltaZ
Returns:
{number} The delta value. Positive if the motion was clockwise, negative if anti-clockwise.