Class: Ray

Raycaster. Ray

Ray class responsible for casting ray's and testing their collisions with mapped objects.

new Ray( [options] [, raycaster])

Parameters:
Name Type Argument Description
options object <optional>
Ray's congfiguration options. May include:
Properties
Name Type Argument Default Description
origin Phaser.Geom.Point | Point <optional>
{x:0, y:0} Ray's position.
angle number <optional>
0 Ray's angle in radians.
angleDeg number <optional>
0 Ray's angle in degrees.
cone number <optional>
0 Ray's cone angle in radians.
coneDeg number <optional>
0 Ray's cone angle in degrees.
range number <optional>
Phaser.Math.MAX_SAFE_INTEGER Ray's range.
collisionRange number <optional>
Phaser.Math.MAX_SAFE_INTEGER Ray's maximum collision range of ray's field of view.
detectionRange number <optional>
Phaser.Math.MAX_SAFE_INTEGER Maximum distance between ray's position and tested objects bounding boxes.
ignoreNotIntersectedRays boolean <optional>
true If set true, ray returns false when it didn't hit anything. Otherwise returns ray's target position.
autoSlice boolean <optional>
false If set true, ray will automatically slice intersections into array of triangles and store it in Ray.slicedIntersections.
round boolean <optional>
false If set true, point where ray hit will be rounded.
enablePhysics boolean | 'arcade' | 'matter' <optional>
false Add to ray physics body. Body will be a circle with radius equal to Ray.collisionRange. If set true, arcade physics body will be added.
raycaster Raycaster <optional>
Parent raycaster object.
Since:
  • 0.6.0
Source:

Members


origin :Phaser.Geom.Point

Ray's source position.
Type:
  • Phaser.Geom.Point
Since:
  • 0.6.0
Source:

angle :number

Ray's angle in radians.
Type:
  • number
Since:
  • 0.6.0
Default Value:
  • 0
Source:

cone :number

Ray's cone width angle in radians.
Type:
  • number
Since:
  • 0.7.0
Default Value:
  • 0
Source:

rayRange :number

Ray's maximum range
Type:
  • number
Since:
  • 0.6.0
Default Value:
  • Phaser.Math.MAX_SAFE_INTEGER
Source:

detectionRange :number

Ray's maximum detection range. Objects outside detection range won't be tested. Ray tests all objects when set to 0.
Type:
  • number
Since:
  • 0.6.0
Source:

collisionRange :number

Ray's maximum collision range of ray's field of view. Radius of Ray.body.
Type:
  • number
Since:
  • 0.8.0
Default Value:
  • Phaser.Math.MAX_SAFE_INTEGER
Source:

ignoreNotIntersectedRays :boolean

If set true, ray returns false when it didn't hit anything. Otherwise returns ray's target position.
Type:
  • boolean
Since:
  • 0.6.0
Default Value:
  • true
Source:

round :boolean

If set true, ray's hit points will be rounded.
Type:
  • boolean
Since:
  • 0.8.1
Default Value:
  • false
Source:

autoSlice :boolean

If set true, ray will automatically slice intersections into array of triangles and store it in Ray.slicedIntersections.
Type:
  • boolean
Since:
  • 0.8.0
Default Value:
  • false
Source:

intersections :Array.<object>

Array of intersections from last raycast representing field of view.
Type:
  • Array.<object>
Since:
  • 0.8.0
Default Value:
  • []
Source:

slicedIntersections :Array.<Phaser.Geom.Triangle>

Array of triangles representing slices of field of view from last raycast.
Type:
  • Array.<Phaser.Geom.Triangle>
Since:
  • 0.8.0
Default Value:
  • []
Source:

body :object

Physics body for testing field of view collisions.
Type:
  • object
Since:
  • 0.8.0
Default Value:
  • undefined
Source:

bodyType :boolean|'arcade'|'matter'

Physics body type.
Type:
  • boolean | 'arcade' | 'matter'
Since:
  • 0.9.0
Default Value:
  • false
Source:

Methods


config( [options])

Configure ray.
Parameters:
Name Type Argument Description
options object <optional>
Ray's congfiguration options. May include:
Properties
Name Type Argument Default Description
origin Phaser.Geom.Point | Point <optional>
{x:0, y:0} Ray's position.
angle number <optional>
0 Ray's angle in radians.
angleDeg number <optional>
0 Ray's angle in degrees.
cone number <optional>
0 Ray's cone angle in radians.
coneDeg number <optional>
0 Ray's cone angle in degrees.
range number <optional>
Phaser.Math.MAX_SAFE_INTEGER Ray's range.
collisionRange number <optional>
Phaser.Math.MAX_SAFE_INTEGER Ray's maximum collision range of ray's field of view.
detectionRange number <optional>
Phaser.Math.MAX_SAFE_INTEGER Maximum distance between ray's position and tested objects bounding boxes.
ignoreNotIntersectedRays boolean <optional>
true If set true, ray returns false when it didn't hit anything. Otherwise returns ray's target position.
autoSlice boolean <optional>
false If set true, ray will automatically slice intersections into array of triangles and store it in Ray.slicedIntersections.
round boolean <optional>
false If set true, point where ray hit will be rounded.
enablePhysics boolean | 'arcade' | 'matter' <optional>
false Add to ray physics body. Body will be a circle with radius equal to Ray.collisionRange. If set true, arcade physics body will be added.
Since:
  • 0.6.0
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

cast( [options])

Cast ray to find closest intersection with tested mapped objects.
Parameters:
Name Type Argument Description
options object <optional>
options that may include:
Properties
Name Type Argument Default Description
objects Array.<object> <optional>
{Raycaster#mappedObjects} Array of game objects to test. If not provided test all mapped game objects.
target Phaser.Geom.Point | Point <optional>
Ray's target point. Used in other casting methods to determine if ray was targeting mapped objects point.
internal boolean <optional>
false Flag determining if method is used by other casting method.
Since:
  • 0.6.0
Source:
Returns:
Ray's closest intersection with tested objects. Returns false if no intersection has been found. Additionally contains reference to hit mapped object and segment if available.
Type
Phaser.Geom.Point | boolean

castCircle( [options])

Cast ray in all directions to find closest intersections with tested mapped objects.
Parameters:
Name Type Argument Description
options object <optional>
options that may include:
Properties
Name Type Argument Default Description
objects Array.<object> <optional>
Raycaster.mappedObjects Array of game objects to test. If not provided test all mapped game objects.
Since:
  • 0.6.0
Source:
Returns:
Array of points of ray's closest intersections with tested objects. Additionally each point contains reference to hit mapped object and it's segment if available.
Type
Array.<Phaser.Geom.Point>

castCone( [options])

Cast ray in a cone to find closest intersections with tested mapped objects.
Parameters:
Name Type Argument Description
options object <optional>
options that may include:
Properties
Name Type Argument Default Description
objects Array.<object> <optional>
Raycaster.mappedObjects Array of game objects to test. If not provided test all mapped game objects.
Since:
  • 0.7.0
Source:
Returns:
Array of points of ray's closest intersections with tested objects. Additionally each point contains reference to hit mapped object and it's segment if available.
Type
Array.<Phaser.Geom.Point>

setRay(x, y [, angle] [, range])

Set ray's position, direction (angle) and range.
Parameters:
Name Type Argument Default Description
x number X coordinate.
y number Y coordinate.
angle number <optional>
Ray's angle in radians.
range number <optional>
Phaser.Math.MAX_SAFE_INTEGER Ray's range.
Since:
  • 0.6.0
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setOrigin(x, y)

Set ray's source position.
Parameters:
Name Type Description
x number X coordinate.
y number Y coordinate.
Since:
  • 0.6.0
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setAngle( [angle])

Set ray's angle (direction) in radians.
Parameters:
Name Type Argument Default Description
angle number <optional>
0 Ray's angle in radians.
Since:
  • 0.6.0
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setAngleDeg( [angle])

Set ray's angle (direction) in degrees.
Parameters:
Name Type Argument Default Description
angle number <optional>
0 Ray's angle in degrees.
Since:
  • 0.6.1
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setRayRange( [rayRange])

Set ray's range.
Parameters:
Name Type Argument Default Description
rayRange number <optional>
Phaser.Math.MAX_SAFE_INTEGER Ray's range.
Since:
  • 0.6.0
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setDetectionRange( [detectionRange])

Set ray's maximum detection range. Objects outside detection range won't be tested. Ray tests all objects when set to 0.
Parameters:
Name Type Argument Default Description
detectionRange number <optional>
0 Maximum distance between ray's position and tested objects bounding boxes.
Since:
  • 0.6.0
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setCollisionRange( [collisionRange])

Set ray's field of view maximum collision range. Objects outside collision range won't be tested by Raycaster.Ray.overlap method. Determines ray's physics body radius.
Parameters:
Name Type Argument Default Description
collisionRange number <optional>
Phaser.Math.MAX_SAFE_INTEGER Ray's collision range and physics body radius.
Since:
  • 0.8.0
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

boundsInRange(object [, bounds])

Test if object's bounding box is in ray's detection range.
Parameters:
Name Type Argument Default Description
object object Tested object
bounds Phaser.Geom.Rectangle | boolean <optional>
false Tested object's bounds. If not passed bounds will be generated automatically.
Since:
  • 0.6.0
Source:
Returns:
Information if object is in ray's detection range.
Type
boolean

setCone( [cone])

Set ray's cone angle (width) in radians.
Parameters:
Name Type Argument Default Description
cone number <optional>
0 Ray's cone angle in radians.
Since:
  • 0.7.0
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setConeDeg( [cone])

Set ray's cone angle (width) in degrees.
Parameters:
Name Type Argument Default Description
cone number <optional>
0 Ray's cone angle in degrees.
Since:
  • 0.7.0
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

overlap( [objects])

Get game objects overlaping field of view.
Parameters:
Name Type Argument Description
objects object | Array.<object> <optional>
Game object / array off game objects to test.
Since:
  • 0.8.0
Source:
Returns:
Array of game objects that overlaps with field of view.
Type
Array.<object>

processOverlap(object1, object2)

Process callback for physics collider / overlap.
Parameters:
Name Type Description
object1 object Game object or matter body passed by collider / overlap or matter CollisionInfo object.
object2 object Game object or matter body passed by collider / overlap. Ignored if matter CollisionInfo object was passed as first argument.
Since:
  • 0.8.0
Source:
Returns:
Return true if game object is overlapping ray's field of view.
Type
boolean

enablePhysics( [type])

Add to ray physics body. Body will be a circle with radius equal to Ray.collisionRange. Physics body can be added only once.
Parameters:
Name Type Argument Default Description
type 'arcade' | 'matter' <optional>
'arcade' Physics type
Since:
  • 0.8.0
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setCollisionCategory(value)

Sets the collision category of this ray's Matter Body. This number must be a power of two between 2^0 (= 1) and 2^31. Two bodies with different collision groups (see #setCollisionGroup) will only collide if their collision categories are included in their collision masks (see #setCollidesWith).
Parameters:
Name Type Description
value number Unique category bitfield.
Since:
  • 0.9.1
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setCollisionCategory(value)

Sets the collision group of this ray's Matter Body. If this is zero or two Matter Bodies have different values, they will collide according to the usual rules (see #setCollisionCategory and #setCollisionGroup). If two Matter Bodies have the same positive value, they will always collide; if they have the same negative value, they will never collide.
Parameters:
Name Type Description
value number Unique group index.
Since:
  • 0.9.1
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setCollidesWith(categories)

Sets the collision mask for this ray's Matter Body. Two Matter Bodies with different collision groups will only collide if each one includes the other's category in its mask based on a bitwise AND, i.e. `(categoryA & maskB) !== 0` and `(categoryB & maskA) !== 0` are both true.*
Parameters:
Name Type Description
categories number | Array.<number> A unique category bitfield, or an array of them.
Since:
  • 0.9.1
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setOnCollide(callback)

The callback is sent a `Phaser.Types.Physics.Matter.MatterCollisionData` object. This does not change the bodies collision category, group or filter. Those must be set in addition to the callback.
Parameters:
Name Type Description
callback function The callback to invoke when this body starts colliding with another.
Since:
  • 0.9.1
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setOnCollideEnd(callback)

The callback is sent a `Phaser.Types.Physics.Matter.MatterCollisionData` object. This does not change the bodies collision category, group or filter. Those must be set in addition to the callback.
Parameters:
Name Type Description
callback function The callback to invoke when this body stops colliding with another.
Since:
  • 0.9.1
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setOnCollideActive(callback)

The callback is sent a `Phaser.Types.Physics.Matter.MatterCollisionData` object. This does not change the bodies collision category, group or filter. Those must be set in addition to the callback.
Parameters:
Name Type Description
callback function The callback to invoke for the duration of this body colliding with another.
Since:
  • 0.9.1
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray

setOnCollideWith(body, callback)

The callback is sent a reference to the other body, along with a `Phaser.Types.Physics.Matter.MatterCollisionData` object. This does not change the bodies collision category, group or filter. Those must be set in addition to the callback.
Parameters:
Name Type Description
body MatterJS.Body | Array.<MatterJS.Body> The body, or an array of bodies, to test for collisions with.
callback function The callback to invoke when this body collides with the given body or bodies.
Since:
  • 0.9.1
Source:
Returns:
Raycaster.Ray instance
Type
Raycaster.Ray