The Arcade Physics world. Contains Arcade Physics related collision, overlap and motion methods. @class Phaser.Physics.Arcade * @constructor
Constructor
Variables
overlapBias:Float = 4
A value added to the delta values during collision checks. Increase it to prevent sprite tunneling.
forceX:Bool = false
If true World.separate will always separate on the X axis before Y. Otherwise it will check gravity totals first.
sortDirection:SortDirection = SortDirection.LEFT_RIGHT
Used when colliding a Sprite vs. a Group, or a Group vs. a Group, this defines the direction the sort is based on. Default is LEFT_RIGHT
.
skipQuadTree:Bool = false
If true
the QuadTree will not be used for any collision. QuadTrees are great if objects are well spread out in your game, otherwise they are a performance hit. If you enable this you can disable on a per body basis via Body.skipQuadTree
.
isPaused:Bool = false
If true
the Body.preUpdate
method will be skipped, halting all motion for all bodies. Note that other methods such as collide
will still work, so be careful not to call them on paused bodies.
maxObjectsWithoutQuadTree:Int = 10
When colliding/overlapping with groups. Use a quad tree if we reach this threshold value
Methods
getQuadTree():QuadTree
Get a QuadTree object configured for this world.
IMPORTANT: you must release it with releaseQuadTree()
when you are done using it and the items it returned.
Returns:
QuadTree
inlinesetBounds(x:Float, y:Float, width:Float, height:Float):Void
Updates the size of this physics world.
@method Phaser.Physics.Arcade#setBounds
Parameters:
{number} | x - Top left most corner of the world. |
---|---|
{number} | y - Top left most corner of the world. |
{number} | width - New width of the world. Can never be smaller than the Game.width. |
{number} | height - New height of the world. Can never be smaller than the Game.height. |
enableBody(body:Body):Void
Creates an Arcade Physics body on the given game object.
A game object can only have 1 physics body active at any one time, and it can't be changed until the body is nulled.
When you add an Arcade Physics body to an object it will automatically add the object into its parent Groups hash array.
@method Phaser.Physics.Arcade#enableBody
Parameters:
{object} | object - The game object to create the physics body on. A body will only be created if this object has a null |
---|
inlineupdateMotion(body:Body):Void
Called automatically by a Physics body, it updates all motion related values on the Body unless World.isPaused
is true
.
@method Phaser.Physics.Arcade#updateMotion
Parameters:
{Phaser.Physics.Arcade.Body} | The Body object to be updated. |
---|
inlinecomputeVelocity(axis:Axis, body:Body, velocity:Float, acceleration:Float, drag:Float, max:Float = 10000):Float
A tween-like function that takes a starting velocity and some other factors and returns an altered velocity. Based on a function in Flixel by @ADAMATOMIC
@method Phaser.Physics.Arcade#computeVelocity
Parameters:
{number} | axis - 0 for nothing, 1 for horizontal, 2 for vertical. |
---|---|
{Phaser.Physics.Arcade.Body} | body - The Body object to be updated. |
{number} | velocity - Any component of velocity (e.g. 20). |
{number} | acceleration - Rate at which the velocity is changing. |
{number} | drag - Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set. |
{number} | [max=10000] - An absolute value cap for the velocity. |
Returns:
{number} The altered Velocity value.
overlap(element1:Collidable, ?element2:Collidable, ?collideCallback:(Body, Body) ‑> Void, ?processCallback:(Body, Body) ‑> Bool):Bool
overlapBodyVsBody(body1:Body, body2:Body, ?overlapCallback:(Body, Body) ‑> Void, ?processCallback:(Body, Body) ‑> Bool):Bool
Checks for overlaps between two bodies. The objects can be Sprites, Groups or Emitters. Unlike {@link #collide} the objects are NOT automatically separated or have any physics applied, they merely test for overlap results.
Returns:
{boolean} True if an overlap occurred otherwise false.
overlapGroupVsGroup(group1:Group, group2:Group, ?overlapCallback:(Body, Body) ‑> Void, ?processCallback:(Body, Body) ‑> Bool):Bool
overlapGroupVsItself(group:Group, ?overlapCallback:(Body, Body) ‑> Void, ?processCallback:(Body, Body) ‑> Bool):Bool
overlapBodyVsGroup(body:Body, group:Group, ?overlapCallback:(Body, Body) ‑> Void, ?processCallback:(Body, Body) ‑> Bool):Bool
collide(element1:Collidable, ?element2:Collidable, ?collideCallback:(Body, Body) ‑> Void, ?processCallback:(Body, Body) ‑> Bool):Bool
collideBodyVsBody(body1:Body, body2:Body, ?collideCallback:(Body, Body) ‑> Void, ?processCallback:(Body, Body) ‑> Bool):Bool
Checks for collision between two bodies and separates them if colliding ({@link https://gist.github.com/samme/cbb81dd19f564dcfe2232761e575063d details}). If you don't require separation then use {@link #overlap} instead.
Returns:
{boolean} True if a collision occurred otherwise false.
collideGroupVsGroup(group1:Group, group2:Group, ?collideCallback:(Body, Body) ‑> Void, ?processCallback:(Body, Body) ‑> Bool):Bool
collideGroupVsItself(group:Group, ?collideCallback:(Body, Body) ‑> Void, ?processCallback:(Body, Body) ‑> Bool):Bool
collideBodyVsGroup(body:Body, group:Group, ?collideCallback:(Body, Body) ‑> Void, ?processCallback:(Body, Body) ‑> Bool):Bool
sort(group:Group, sortDirection:SortDirection = SortDirection.INHERIT):Void
This method will sort a Groups hash array.
If the Group has physicsSortDirection
set it will use the sort direction defined.
Otherwise if the sortDirection parameter is undefined, or Group.physicsSortDirection is null, it will use Phaser.Physics.Arcade.sortDirection.
By changing Group.physicsSortDirection you can customise each Group to sort in a different order.
@method Phaser.Physics.Arcade#sort
Parameters:
{Phaser.Group} | group - The Group to sort. |
---|---|
{integer} | [sortDirection] - The sort direction used to sort this Group. |
intersects(body1:Body, body2:Body):Bool
Check for intersection against two bodies.
@method Phaser.Physics.Arcade#intersects
Parameters:
{Phaser.Physics.Arcade.Body} | body1 - The first Body object to check. |
---|---|
{Phaser.Physics.Arcade.Body} | body2 - The second Body object to check. |
Returns:
{boolean} True if they intersect, otherwise false.
getObjectsAtLocation<T>(x:Float, y:Float, group:Group, ?callback:(T, Body) ‑> Void, ?callbackArg:T, ?output:Array<Body>):Array<Body>
Given a Group and a location this will check to see which Group children overlap with the coordinates. Each child will be sent to the given callback for further processing. Note that the children are not checked for depth order, but simply if they overlap the coordinate or not.
@method Phaser.Physics.Arcade#getObjectsAtLocation
Parameters:
{number} | x - The x coordinate to check. |
---|---|
{number} | y - The y coordinate to check. |
{Phaser.Group} | group - The Group to check. |
{function} | [callback] - A callback function that is called if the object overlaps the coordinates. The callback will be sent two parameters: the callbackArg and the Object that overlapped the location. |
{object} | [callbackContext] - The context in which to run the callback. |
{object} | [callbackArg] - An argument to pass to the callback. |
Returns:
An array of the Sprites from the Group that overlapped the coordinates.
moveToDestination(body:Body, destination:Body, speed:Float = 60, maxTime:Float = 0):Float
Move the given display object towards the destination object at a steady velocity. If you specify a maxTime then it will adjust the speed (overwriting what you set) so it arrives at the destination in that number of seconds. Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms. Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course. Note: The display object doesn't stop moving once it reaches the destination coordinates. Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
@method Phaser.Physics.Arcade#moveToObject
Parameters:
{any} | displayObject - The display object to move. |
---|---|
{any} | destination - The display object to move towards. Can be any object but must have visible x/y properties. |
{number} | [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec) |
{number} | [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms. |
Returns:
{number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
moveToXY(body:Body, x:Float, y:Float, speed:Float = 60, maxTime:Float = 0):Float
Move the given display object towards the x/y coordinates at a steady velocity. If you specify a maxTime then it will adjust the speed (over-writing what you set) so it arrives at the destination in that number of seconds. Timings are approximate due to the way browser timers work. Allow for a variance of +- 50ms. Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course. Note: The display object doesn't stop moving once it reaches the destination coordinates. Note: Doesn't take into account acceleration, maxVelocity or drag (if you've set drag or acceleration too high this object may not move at all)
@method Phaser.Physics.Arcade#moveToXY
Parameters:
{any} | displayObject - The display object to move. |
---|---|
{number} | x - The x coordinate to move towards. |
{number} | y - The y coordinate to move towards. |
{number} | [speed=60] - The speed it will move, in pixels per second (default is 60 pixels/sec) |
{number} | [maxTime=0] - Time given in milliseconds (1000 = 1 sec). If set the speed is adjusted so the object will arrive at destination in the given number of ms. |
Returns:
{number} The angle (in radians) that the object should be visually set to in order to match its new velocity.
velocityFromAngle(angle:Float, speed:Float = 60, ?point:Point):Point
Given the angle (in degrees) and speed calculate the velocity and return it as a Point object, or set it to the given point object. One way to use this is: velocityFromAngle(angle, 200, sprite.velocity) which will set the values directly to the sprites velocity and not create a new Point object.
@method Phaser.Physics.Arcade#velocityFromAngle
Parameters:
{number} | angle - The angle in degrees calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative) |
---|---|
{number} | [speed=60] - The speed it will move, in pixels per second sq. |
{Phaser.Point|object} | [point] - The Point object in which the x and y properties will be set to the calculated velocity. |
Returns:
{Phaser.Point} - A Point where point.x contains the velocity x value and point.y contains the velocity y value.
velocityFromRotation(rotation:Float, speed:Float = 60, ?point:Point):Point
Given the rotation (in radians) and speed calculate the velocity and return it as a Point object, or set it to the given point object. One way to use this is: velocityFromRotation(rotation, 200, sprite.velocity) which will set the values directly to the sprites velocity and not create a new Point object.
@method Phaser.Physics.Arcade#velocityFromRotation
Parameters:
{number} | rotation - The angle in radians. |
---|---|
{number} | [speed=60] - The speed it will move, in pixels per second sq. |
{Phaser.Point|object} | [point] - The Point object in which the x and y properties will be set to the calculated velocity. |
Returns:
{Phaser.Point} - A Point where point.x contains the velocity x value and point.y contains the velocity y value.
accelerationFromRotation(rotation:Float, speed:Float = 60, ?point:Point):Point
Given the rotation (in radians) and speed calculate the acceleration and return it as a Point object, or set it to the given point object. One way to use this is: accelerationFromRotation(rotation, 200, sprite.acceleration) which will set the values directly to the sprites acceleration and not create a new Point object.
@method Phaser.Physics.Arcade#accelerationFromRotation
Parameters:
{number} | rotation - The angle in radians. |
---|---|
{number} | [speed=60] - The speed it will move, in pixels per second sq. |
{Phaser.Point|object} | [point] - The Point object in which the x and y properties will be set to the calculated acceleration. |
Returns:
{Phaser.Point} - A Point where point.x contains the acceleration x value and point.y contains the acceleration y value.
accelerateToDestination(body:Body, destination:Body, speed:Float = 60, xSpeedMax:Float = 1000, ySpeedMax:Float = 1000):Float
Sets the acceleration.x/y property on the display object so it will move towards the target at the given speed (in pixels per second sq.) You must give a maximum speed value, beyond which the display object won't go any faster. Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course. Note: The display object doesn't stop moving once it reaches the destination coordinates.
@method Phaser.Physics.Arcade#accelerateToObject
Parameters:
{any} | displayObject - The display object to move. |
---|---|
{any} | destination - The display object to move towards. Can be any object but must have visible x/y properties. |
{number} | [speed=60] - The speed it will accelerate in pixels per second. |
{number} | [xSpeedMax=1000] - The maximum x velocity the display object can reach. |
{number} | [ySpeedMax=1000] - The maximum y velocity the display object can reach. |
Returns:
{number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
accelerateToXY(body:Body, x:Float, y:Float, speed:Float = 60, xSpeedMax:Float = 1000, ySpeedMax:Float = 1000):Float
Sets the acceleration.x/y property on the display object so it will move towards the x/y coordinates at the given speed (in pixels per second sq.) You must give a maximum speed value, beyond which the display object won't go any faster. Note: The display object does not continuously track the target. If the target changes location during transit the display object will not modify its course. Note: The display object doesn't stop moving once it reaches the destination coordinates.
@method Phaser.Physics.Arcade#accelerateToXY
Parameters:
{any} | displayObject - The display object to move. |
---|---|
{number} | x - The x coordinate to accelerate towards. |
{number} | y - The y coordinate to accelerate towards. |
{number} | [speed=60] - The speed it will accelerate in pixels per second. |
{number} | [xSpeedMax=1000] - The maximum x velocity the display object can reach. |
{number} | [ySpeedMax=1000] - The maximum y velocity the display object can reach. |
Returns:
{number} The angle (in radians) that the object should be visually set to in order to match its new trajectory.
distanceBetween(source:Body, target:Body, useCenter:Bool = false):Float
Find the distance between two display objects (like Sprites).
The optional world
argument allows you to return the result based on the Game Objects world
property,
instead of its x
and y
values. This is useful of the object has been nested inside an offset Group,
or parent Game Object.
If you have nested objects and need to calculate the distance between their centers in World coordinates,
set their anchors to (0.5, 0.5) and use the world
argument.
If objects aren't nested or they share a parent's offset, you can calculate the distance between their
centers with the useCenter
argument, regardless of their anchor values.
@method Phaser.Physics.Arcade#distanceBetween
@link Phaser.Sprite#centerX} and {@link Phaser.Sprite#centerY} coordinates. If true, this value overrides the world
argument.
Parameters:
{any} | source - The Display Object to test from. |
---|---|
{any} | target - The Display Object to test to. |
{boolean} | [world=false] - Calculate the distance using World coordinates (true), or Object coordinates (false, the default). If |
{boolean} | [useCenter=false] - Calculate the distance using the { |
Returns:
{number} The distance between the source and target objects.
inlinedistanceToXY(body:Body, x:Float, y:Float):Float
Find the distance between a display object (like a Sprite) and the given x/y coordinates.
The calculation is made from the display objects x/y coordinate. This may be the top-left if its anchor hasn't been changed.
If you need to calculate from the center of a display object instead use {@link #distanceBetween} with the useCenter
argument.
The optional world
argument allows you to return the result based on the Game Objects world
property,
instead of its x
and y
values. This is useful of the object has been nested inside an offset Group,
or parent Game Object.
@method Phaser.Physics.Arcade#distanceToXY
Parameters:
{any} | displayObject - The Display Object to test from. |
---|---|
{number} | x - The x coordinate to move towards. |
{number} | y - The y coordinate to move towards. |
{boolean} | [world=false] - Calculate the distance using World coordinates (true), or Object coordinates (false, the default) |
Returns:
{number} The distance between the object and the x/y coordinates.
closest(source:Body, targets:Array<Body>, world:Bool = false, useCenter:Bool = false):Body
From a set of points or display objects, find the one closest to a source point or object.
@method Phaser.Physics.Arcade#closest
@link Phaser.Point Point} or Display Object distances will be measured from.
@link Phaser.Point Points} or Display Objects whose distances to the source will be compared.
@link Phaser.Sprite#centerX} and {@link Phaser.Sprite#centerY} coordinates. If true, this value overrides the world
argument.
Parameters:
{any} | source - The { |
---|---|
{any[]} | targets - The { |
{boolean} | [world=false] - Calculate the distance using World coordinates (true), or Object coordinates (false, the default). If |
{boolean} | [useCenter=false] - Calculate the distance using the { |
Returns:
{any} - The first target closest to the origin.
farthest(source:Body, targets:Array<Body>, useCenter:Bool = false):Body
From a set of points or display objects, find the one farthest from a source point or object.
@method Phaser.Physics.Arcade#farthest
@link Phaser.Point Point} or Display Object distances will be measured from.
@link Phaser.Point Points} or Display Objects whose distances to the source will be compared.
@link Phaser.Sprite#centerX} and {@link Phaser.Sprite#centerY} coordinates. If true, this value overrides the world
argument.
Parameters:
{any} | source - The { |
---|---|
{any[]} | targets - The { |
{boolean} | [world=false] - Calculate the distance using World coordinates (true), or Object coordinates (false, the default). If |
{boolean} | [useCenter=false] - Calculate the distance using the { |
Returns:
{any} - The target closest to the origin.
inlineangleBetween(source:Body, target:Body):Float
Find the angle in radians between two display objects (like Sprites).
The optional world
argument allows you to return the result based on the Game Objects world
property,
instead of its x
and y
values. This is useful of the object has been nested inside an offset Group,
or parent Game Object.
@method Phaser.Physics.Arcade#angleBetween
Parameters:
{any} | source - The Display Object to test from. |
---|---|
{any} | target - The Display Object to test to. |
{boolean} | [world=false] - Calculate the angle using World coordinates (true), or Object coordinates (false, the default) |
Returns:
{number} The angle in radians between the source and target display objects.
angleBetweenCenters(source:Body, target:Body):Float
Find the angle in radians between centers of two display objects (like Sprites).
@method Phaser.Physics.Arcade#angleBetweenCenters
Parameters:
{any} | source - The Display Object to test from. |
---|---|
{any} | target - The Display Object to test to. |
Returns:
{number} The angle in radians between the source and target display objects.
inlineangleToXY(body:Body, x:Float, y:Float):Float
Find the angle in radians between a display object (like a Sprite) and the given x/y coordinate.
The optional world
argument allows you to return the result based on the Game Objects world
property,
instead of its x
and y
values. This is useful of the object has been nested inside an offset Group,
or parent Game Object.
@method Phaser.Physics.Arcade#angleToXY
Parameters:
{any} | displayObject - The Display Object to test from. |
---|---|
{number} | x - The x coordinate to get the angle to. |
{number} | y - The y coordinate to get the angle to. |
{boolean} | [world=false] - Calculate the angle using World coordinates (true), or Object coordinates (false, the default) |
Returns:
{number} The angle in radians between displayObject.x/y to Pointer.x/y