Body
The Physics Body is linked to a single game object. All physics operations should be performed against the body rather than the object itself. For example you can set the velocity, acceleration, bounce values etc all on the Body.
Instance Members
A property to hold any data related to this body. Can be useful if building a larger system on top of this one.
A property to hold any index value related to this body. Can be useful if building a larger system on top of this one.
The list of groups that contain this body (can be null if there are no groups).
A "main" group associated with this body.
A disabled body won't be checked for any form of collision or overlap or have its pre/post updates run.
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.
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.
The radius of the circular collision shape this Body is using if Body.setCircle has been enabled.
If you wish to change the radius then call setCircle
again with the new value.
If you wish to stop the Body using a circle then call setCircle
with a radius of zero (or undefined).
The actual radius of the Body is equal to halfWidth
and the diameter is equal to width
.
The x position of the physics body.
The y position of the physics body.
The previous x position of the physics body.
The previous y position of the physics body.
Allow this Body to be rotated? (via angularVelocity, etc)
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 object and updates its rotation.
The previous rotation of the physics body, in degrees.
The calculated width of the physics body.
The calculated height of the physics body.
The calculated width / 2 of the physics body.
The calculated height / 2 of the physics body.
The center x coordinate of the Physics Body.
The center y coordinate of the Physics Body.
The x velocity, or rate of change of the Body's x position. Measured in pixels per second.
The y velocity, or rate of change of the Body's y position. Measured in pixels per second.
The x distance traveled during the last update, equal to velocityX * physicsElapsed
. Calculated during the Body.preUpdate and applied to its position.
The y distance traveled during the last update, equal to velocityY * physicsElapsed
. Calculated during the Body.preUpdate and applied to its position.
The maximum x delta value. The Body position is updated based on the delta x/y values. You can set a cap on those (both +-) using maxDeltaX.
The maximum y delta value. The Body position is updated based on the delta x/y values. You can set a cap on those (both +-) using maxDeltaY.
The x acceleration is the rate of change of the x velocity. Measured in pixels per second squared.
The y acceleration is the rate of change of the y velocity. Measured in pixels per second squared.
Allow this Body to be influenced by drag?
The x drag applied to the motion of the Body (when allowDrag
is enabled). Measured in pixels per second squared.
The y drag applied to the motion of the Body (when allowDrag
is enabled). Measured in pixels per second squared.
Allow this Body to be influenced by gravity? Either world or local.
This Body's local x gravity, added to any world x gravity, unless Body.allowGravity is set to false.
This Body's local y gravity, added to any world y gravity, unless Body.allowGravity is set to false.
The x elasticity of the Body when colliding. bounceX = 1 means full rebound, bounceX = 0.5 means 50% rebound velocity.
The y elasticity of the Body when colliding. bounceY = 1 means full rebound, bounceY = 0.5 means 50% rebound velocity.
The elasticity of the Body when colliding with the World bounds.
By default this property is false
, in which case Body.bounce
is used instead. Set this property
to true in order to enable a World bounds specific bounce value.
The x elasticity of the Body when colliding with the World bounds.
The y elasticity of the Body when colliding with the World bounds.
The maximum x velocity (in pixels per second) that the Body can reach.
The maximum y velocity (in pixels per second) that the Body can reach.
If this Body is immovable
and moving, and another Body is 'riding' this one, this is the amount of x motion the riding Body receives.
If this Body is immovable
and moving, and another Body is 'riding' this one, this is the amount of y motion the riding Body receives.
The angular velocity is the rate of change of the Body's rotation. It is measured in degrees per second.
The angular acceleration is the rate of change of the angular velocity. Measured in degrees per second squared.
The drag applied during the rotation of the Body. Measured in degrees per second squared.
The maximum angular velocity in degrees per second that the Body can reach.
The mass of the Body. When two bodies collide their mass is used in the calculation to determine the exchange of velocity.
The angle of the Body's velocity in radians.
The speed of the Body in pixels per second, equal to the magnitude of the velocity.
A const reference to the direction the Body is traveling or facing: NONE, LEFT, RIGHT, UP, or DOWN. If the Body is moving on both axes, UP and DOWN take precedence.
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.
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 immovable
as well.
This flag allows you to disable the custom x separation that takes place by World.separate. Used in combination with your own collision processHandler you can create whatever type of collision response you need.
This flag allows you to disable the custom y separation that takes place by World.separate. Used in combination with your own collision processHandler you can create whatever type of collision response you need.
When this body collides with another, the amount of horizontal overlap is stored here.
When this body collides with another, the amount of vertical overlap is stored here.
If Body.isCircle
is true, and this body collides with another circular body, the amount of overlap is stored here.
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.
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.
If true, collision and overlap checks are disabled for this Body, but motion is retained.
Whether this body processes collisions on its top edge.
Whether this body processes collisions on its bottom edge.
Whether this body processes collisions on its left edge.
Whether this body processes collisions on its right edge.
True if the Body is not touching any other Body.
True if the Body is touching another Body on its top edge.
True if the Body is touching another Body on its bottom edge.
True if the Body is touching another Body on its left edge.
True if the Body is touching another Body on its right edge.
True if the Body was not touching any other Body in the previous collision check.
True if the Body was touching another Body on its top edge in the previous collision check.
True if the Body was touching another Body on its bottom edge in the previous collision check.
True if the Body was touching another Body on its left edge in the previous collision check.
True if the Body was touching another Body on its right edge in the previous collision check.
If this Body being blocked by world bounds or another immovable object?
If this Body being blocked by upper world bounds or another immovable object above it?
If this Body being blocked by lower world bounds or another immovable object below it?
If this Body being blocked by left world bounds or another immovable object on the left?
If this Body being blocked by right world bounds or another immovable object on the right?
If this Body in a preUpdate (true) or postUpdate (false) state?
If true and you collide this Body against a Group, it will disable the collision check from using a QuadTree.
Set by the moveTo
and moveFrom
methods.
Set by the moveTo
and moveFrom
methods.
A callback 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 function
and it will be called when a collision happens, passing five arguments:
onWorldBounds(body, up, down, left, right)
where the Body is a reference to this Body, and the other arguments are booleans
indicating on which side of the world the Body collided.
Name | Type |
---|---|
`` | Body |
`` | Bool |
`` | Bool |
`` | Bool |
`` | Bool |
A callback that is dispatched when this Body collides with another Body.
You still need to call world.collide
in your update method in order
for this callback to be dispatched.
Usually you'd pass a callback to the collide
method, but this callback provides for
a different level of notification.
Due to the potentially high volume of callbacks this could create it is disabled by default.
To use this feature set this property to a function and it will be called when a collision happens, passing two arguments: the bodies which collided. The first body in the argument is always this Body.
If two Bodies with this callback set collide, both will dispatch the callback.
Name | Type |
---|---|
`` | Body |
`` | Body |
A callback that is dispatched when this Body overlaps with another Body.
You still need to call world.overlap
in your update method in order
for this callback to be dispatched.
Usually you'd pass a callback to the overlap
method, but this callback provides for
a different level of notification.
Due to the potentially high volume of callbacks this could create it is disabled by default.
To use this feature set this property to a function and it will be called when an overlap happens, passing two arguments: the bodies which overlapped. The first body in the argument is always this Body.
If two Bodies with this callback set overlap, both will dispatch the callback.
Name | Type |
---|---|
`` | Body |
`` | Body |
Listen for the completion of moveTo
or moveFrom
events.
Name | Type |
---|---|
`` | Body |
`` | Bool |
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.
Name | Type |
---|---|
body |
Body |
velocityX |
Float |
velocityY |
Float |
percent |
Float |
Returns |
---|
Bool |
Update the Body's center from its position.
Name | Type |
---|---|
width |
Float |
height |
Float |
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
callback is dispatched.
Name | Type | Description |
---|---|---|
stopVelocity |
Bool | 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
callback 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.
Name | Type | Default | Description |
---|---|---|---|
duration |
Float | The duration of the movement, in seconds. | |
speed |
Float | -999999999.0 |
The speed of the movement, in pixels per second. If not provided Body.speed is used. |
direction |
Float | -999999999.0 |
The angle of movement in degrees. If not provided Body.angle is used. |
Returns | Description |
---|---|
Bool | True if the movement successfully started, otherwise false. |
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
callback 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.
Name | Type | Default | Description |
---|---|---|---|
duration |
Float | The duration of the movement, in seconds. | |
distance |
Float | The distance, in pixels, the Body will move. | |
direction |
Float | -999999999.0 |
The angle of movement. If not provided Body.angle is used. |
Returns | Description |
---|---|
Bool | True if the movement successfully started, otherwise false. |
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 and is the distance from the center of the circle to the edge.
To change a Body back to being rectangular again call 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.
Name | Type | Description |
---|---|---|
radius |
Float | The radius of the Body in pixels. Pass a value of zero / undefined, to stop the Body using a circle for collision. |
Resets all Body values (velocity, acceleration, rotation, etc)
Name | Type | Default | Description |
---|---|---|---|
x |
Float | The new x position of the Body. | |
y |
Float | The new y position of the Body. | |
width |
Float | The new width of the Body. | |
height |
Float | The new height of the Body. | |
rotation |
Float | 0 |
The new rotation of the Body. |
Sets acceleration, velocity, and speed to 0.
Tests if a world point lies within this Body.
Name | Type | Description |
---|---|---|
x |
Float | The world x coordinate to test. |
y |
Float | The world y coordinate to test. |
Returns | Description |
---|---|
Bool | True if the given coordinates are inside this Body, otherwise false. |
Returns true if the bottom of this Body is in contact with either the world bounds or a tile.
Returns | Description |
---|---|
Bool | True if in contact with either the world bounds or a tile. |
Returns true if the top of this Body is in contact with either the world bounds or a tile.
Returns | Description |
---|---|
Bool | True if in contact with either the world bounds or a tile. |
Returns true if either side of this Body is in contact with either the world bounds or a tile.
Returns | Description |
---|---|
Bool | True if in contact with either the world bounds or a tile. |
Returns the absolute delta x value.
Returns | Description |
---|---|
Float | The absolute delta value. |
Returns the absolute delta y value.
Returns | Description |
---|---|
Float | The absolute delta value. |
Returns the delta x value. The difference between Body.x now and in the previous step.
Returns | Description |
---|---|
Float | The delta value. Positive if the motion was to the right, negative if to the left. |
Returns the delta y value. The difference between Body.y now and in the previous step.
Returns | Description |
---|---|
Float | The delta value. Positive if the motion was downwards, negative if upwards. |
Returns the delta z value. The difference between Body.rotation now and in the previous step.
Returns | Description |
---|---|
Float | The delta value. Positive if the motion was clockwise, negative if anti-clockwise. |
Destroys this Body.
First it removes this body from any groups it belongs to. Then it nulls the data reference.
Name | Type | Default |
---|---|---|
azimuth |
Float | |
radius |
Float | 1 |
asDegrees |
Bool | false |
Name | Type | Default |
---|---|---|
azimuth |
Float | |
radius |
Float | 1 |
asDegrees |
Bool | false |
Name | Type | Default |
---|---|---|
x |
Float | |
y |
Float | |
width |
Float | |
height |
Float | |
rotation |
Float | 0 |
Private Members
Internal time used by the moveTo
and moveFrom
methods.
Internal distance value, used by the moveTo
and moveFrom
methods.
Internal duration value, used by the moveTo
and moveFrom
methods.
Set by the moveTo
method, and updated each frame.
Set by the moveTo
method, and updated each frame.
Return true if the given x/y coordinates are within the circular body.
Name | Type | Description |
---|---|---|
body |
Body | The Body to be checked. |
x |
Float | The X value of the coordinate to test. |
y |
Float | The Y value of the coordinate to test. |
Returns | Description |
---|---|
Bool | True if the coordinates are within this circle, otherwise false. |
Determines whether the specified coordinates are contained within the region defined by this rectangular body.
Name | Type | Description |
---|---|---|
body |
Body | The Body object. |
x |
Float | The x coordinate of the point to test. |
y |
Float | The y coordinate of the point to test. |
Returns | Description |
---|---|
Bool | A value of true if the Body contains the specified point; otherwise false. |
Name | Type |
---|---|
deg |
Float |
Returns |
---|
Float |
Name | Type |
---|---|
rad |
Float |
Returns |
---|
Float |
Internal method.
Name | Type |
---|---|
world |
World |
Returns |
---|
Bool |
Internal method.
Name | Type |
---|---|
world |
World |
Returns | Description |
---|---|
Bool | True if the Body collided with the world bounds, otherwise false. |
Returns |
---|
String |
Metadata
Name | Parameters |
---|---|
:allow |
arcade.World |