Class: ArcadePhysics

Phaser.Physics.Arcade. ArcadePhysics

The Arcade Physics Plugin belongs to a Scene and sets up and manages the Scene's physics simulation.
It also holds some useful methods for moving and rotating Arcade Physics Bodies.

You can access it from within a Scene using this.physics.


new ArcadePhysics(scene)

Parameters:
Name Type Description
scene Phaser.Scene

The Scene that this Plugin belongs to.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 18)

Members


add :Phaser.Physics.Arcade.Factory

An object holding the Arcade Physics factory methods.

Type:
Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 74)

config :object

A configuration object. Union of the physics.arcade.* properties of the GameConfig and SceneConfig objects.

Type:
  • object
Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 56)

scene :Phaser.Scene

The Scene that this Plugin belongs to.

Type:
Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 38)

systems :Phaser.Scenes.Systems

The Scene's Systems.

Type:
Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 47)

world :Phaser.Physics.Arcade.World

The physics simulation.

Type:
Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 65)

Methods


accelerateTo(gameObject, x, y [, speed] [, xSpeedMax] [, ySpeedMax])

Sets the acceleration.x/y property on the game object so it will move towards the x/y coordinates at the given rate (in pixels per second squared)

You must give a maximum speed value, beyond which the game object won't go any faster.

Note: The game object does not continuously track the target. If the target changes location during transit the game object will not modify its course.
Note: The game object doesn't stop moving once it reaches the destination coordinates.

Parameters:
Name Type Argument Default Description
gameObject Phaser.GameObjects.GameObject

Any Game Object with an Arcade Physics body.

x number

The x coordinate to accelerate towards.

y number

The y coordinate to accelerate towards.

speed number <optional>
60

The acceleration (change in speed) in pixels per second squared.

xSpeedMax number <optional>
500

The maximum x velocity the game object can reach.

ySpeedMax number <optional>
500

The maximum y velocity the game object can reach.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 224)
Returns:

The angle (in radians) that the object should be visually set to in order to match its new velocity.

Type
number

accelerateToObject(gameObject, destination [, speed] [, xSpeedMax] [, ySpeedMax])

Sets the acceleration.x/y property on the game object so it will move towards the x/y coordinates at the given rate (in pixels per second squared)

You must give a maximum speed value, beyond which the game object won't go any faster.

Note: The game object does not continuously track the target. If the target changes location during transit the game object will not modify its course.
Note: The game object doesn't stop moving once it reaches the destination coordinates.

Parameters:
Name Type Argument Default Description
gameObject Phaser.GameObjects.GameObject

Any Game Object with an Arcade Physics body.

destination Phaser.GameObjects.GameObject

The Game Object to move towards. Can be any object but must have visible x/y properties.

speed number <optional>
60

The acceleration (change in speed) in pixels per second squared.

xSpeedMax number <optional>
500

The maximum x velocity the game object can reach.

ySpeedMax number <optional>
500

The maximum y velocity the game object can reach.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 260)
Returns:

The angle (in radians) that the object should be visually set to in order to match its new velocity.

Type
number

closest(source)

Finds the Body closest to a source point or object.

Parameters:
Name Type Description
source object

Any object with public x and y properties, such as a Game Object or Geometry object.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 284)
Returns:

The closest Body to the given source point.

Type
Phaser.Physics.Arcade.Body

collide(object1 [, object2] [, collideCallback] [, processCallback] [, callbackContext])

Tests if Game Objects overlap and separates them (if possible). See Phaser.Physics.Arcade.World#collide.

Parameters:
Name Type Argument Description
object1 ArcadeColliderType

The first object or array of objects to check.

object2 ArcadeColliderType <optional>

The second object or array of objects to check, or undefined.

collideCallback ArcadePhysicsCallback <optional>

An optional callback function that is called if the objects collide.

processCallback ArcadePhysicsCallback <optional>

An optional callback function that lets you perform additional checks against the two objects if they collide. If this is set then collideCallback will only be called if this callback returns true.

callbackContext * <optional>

The context in which to run the callbacks.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 173)
See:
Returns:

True if any overlapping Game Objects were separated, otherwise false.

Type
boolean

destroy()

The Scene that owns this plugin is being destroyed.
We need to shutdown and then kill off all external references.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 482)

furthest(source)

Finds the Body farthest from a source point or object.

Parameters:
Name Type Description
source object

Any object with public x and y properties, such as a Game Object or Geometry object.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 318)
Returns:

The Body furthest from the given source point.

Type
Phaser.Physics.Arcade.Body

getConfig()

Creates the physics configuration for the current Scene.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 127)
Returns:

The physics configuration.

Type
object

moveTo(gameObject, x, y [, speed] [, maxTime])

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)

Parameters:
Name Type Argument Default Description
gameObject Phaser.GameObjects.GameObject

Any Game Object with an Arcade Physics body.

x number

The x coordinate to move towards.

y number

The y coordinate to move towards.

speed number <optional>
60

The speed it will move, in pixels per second (default is 60 pixels/sec)

maxTime number <optional>
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.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 352)
Returns:

The angle (in radians) that the object should be visually set to in order to match its new velocity.

Type
number

moveToObject(gameObject, destination [, speed] [, maxTime])

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)

Parameters:
Name Type Argument Default Description
gameObject Phaser.GameObjects.GameObject

Any Game Object with an Arcade Physics body.

destination object

Any object with public x and y properties, such as a Game Object or Geometry object.

speed number <optional>
60

The speed it will move, in pixels per second (default is 60 pixels/sec)

maxTime number <optional>
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.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 389)
Returns:

The angle (in radians) that the object should be visually set to in order to match its new velocity.

Type
number

overlap(object1 [, object2] [, collideCallback] [, processCallback] [, callbackContext])

Tests if Game Objects overlap. See Phaser.Physics.Arcade.World#overlap

Parameters:
Name Type Argument Description
object1 ArcadeColliderType

The first object or array of objects to check.

object2 ArcadeColliderType <optional>

The second object or array of objects to check, or undefined.

collideCallback ArcadePhysicsCallback <optional>

An optional callback function that is called if the objects collide.

processCallback ArcadePhysicsCallback <optional>

An optional callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collideCallback will only be called if this callback returns true.

callbackContext * <optional>

The context in which to run the callbacks.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 148)
See:
Returns:

True if at least one Game Object overlaps another.

Type
boolean

pause()

Pauses the simulation.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 198)
Returns:

The simulation.

Type
Phaser.Physics.Arcade.World

resume()

Resumes the simulation (if paused).

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 211)
Returns:

The simulation.

Type
Phaser.Physics.Arcade.World

shutdown()

The Scene that owns this plugin is shutting down.
We need to kill and reset all internal properties as well as stop listening to Scene events.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 454)

velocityFromAngle(angle [, speed] [, vec2])

Given the angle (in degrees) and speed calculate the velocity and return it as a vector, or set it to the given vector object.
One way to use this is: velocityFromAngle(angle, 200, sprite.body.velocity) which will set the values directly to the sprite's velocity and not create a new vector object.

Parameters:
Name Type Argument Default Description
angle number

The angle in degrees calculated in clockwise positive direction (down = 90 degrees positive, right = 0 degrees positive, up = 90 degrees negative)

speed number <optional>
60

The speed it will move, in pixels per second squared.

vec2 Phaser.Math.Vector2 <optional>

The Vector2 in which the x and y properties will be set to the calculated velocity.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 412)
Returns:

The Vector2 that stores the velocity.

Type
Phaser.Math.Vector2

velocityFromRotation(rotation [, speed] [, vec2])

Given the rotation (in radians) and speed calculate the velocity and return it as a vector, or set it to the given vector object.
One way to use this is: velocityFromRotation(rotation, 200, sprite.body.velocity) which will set the values directly to the sprite's velocity and not create a new vector object.

Parameters:
Name Type Argument Default Description
rotation number

The angle in radians.

speed number <optional>
60

The speed it will move, in pixels per second squared

vec2 Phaser.Math.Vector2 <optional>

The Vector2 in which the x and y properties will be set to the calculated velocity.

Since: 3.0.0
Source: src/physics/arcade/ArcadePhysics.js (Line 433)
Returns:

The Vector2 that stores the velocity.

Type
Phaser.Math.Vector2