Phaser API Documentation

  Version: 
Filter
This is a beta release of our new docs system. Found an issue?
Please tell us about it in the #📖-newdocs-feedback channel on the Phaser Discord

Phaser . Math . Vector3

This is a beta release of our new docs system. Found an issue?
Please tell us about it in the #📖-newdocs-feedback channel on the Phaser Discord

Phaser . Math . Vector3

A representation of a vector in 3D space.

A three-component vector.

Constructor:

new Vector3([x], [y], [z])

Parameters:

name type arguments description
x number <optional>

The x component.

y number <optional>

The y component.

z number <optional>

The z component.

Since: 3.0.0
Source: src/math/Vector3.js (Line 12)

Members

<static, constant> BACK: Phaser.Math.Vector3
Focus
Focus

Description:

A static back Vector3 for use by reference.

This constant is meant for comparison operations and should not be modified directly.

Type:
Since: 3.16.0
Source: src/math/Vector3.js (Line 1016)
Focus
Focus
<static, constant> DOWN: Phaser.Math.Vector3
Focus
Focus

Description:

A static down Vector3 for use by reference.

This constant is meant for comparison operations and should not be modified directly.

Type:
Since: 3.16.0
Source: src/math/Vector3.js (Line 992)
Focus
Focus
<static, constant> FORWARD: Phaser.Math.Vector3
Focus
Focus

Description:

A static forward Vector3 for use by reference.

This constant is meant for comparison operations and should not be modified directly.

Type:
Since: 3.16.0
Source: src/math/Vector3.js (Line 1004)
Focus
Focus
<static, constant> LEFT: Phaser.Math.Vector3
Focus
Focus

Description:

A static left Vector3 for use by reference.

This constant is meant for comparison operations and should not be modified directly.

Type:
Since: 3.16.0
Source: src/math/Vector3.js (Line 968)
Focus
Focus
<static, constant> ONE: Phaser.Math.Vector3
Focus
Focus

Description:

A static one Vector3 for use by reference.

This constant is meant for comparison operations and should not be modified directly.

Type:
Since: 3.16.0
Source: src/math/Vector3.js (Line 1028)
Focus
Focus
<static, constant> UP: Phaser.Math.Vector3
Focus
Focus

Description:

A static up Vector3 for use by reference.

This constant is meant for comparison operations and should not be modified directly.

Type:
Since: 3.16.0
Source: src/math/Vector3.js (Line 980)
Focus
Focus
<static, constant> ZERO: Phaser.Math.Vector3
Focus
Focus

Description:

A static zero Vector3 for use by reference.

This constant is meant for comparison operations and should not be modified directly.

Type:
Since: 3.16.0
Source: src/math/Vector3.js (Line 944)
Focus
Focus
x: number
Focus
Focus

Description:

The x component of this Vector.

Type:
number
Default: 0
Since: 3.0.0
Source: src/math/Vector3.js (Line 33)
Focus
Focus
y: number
Focus
Focus

Description:

The y component of this Vector.

Type:
number
Default: 0
Since: 3.0.0
Source: src/math/Vector3.js (Line 43)
Focus
Focus
z: number
Focus
Focus

Description:

The z component of this Vector.

Type:
number
Default: 0
Since: 3.0.0
Source: src/math/Vector3.js (Line 53)
Focus
Focus

Methods

add(v)
Focus
Focus

Description:

Add a given Vector to this Vector. Addition is component-wise.

Parameters:

name type description
v Phaser.Math.Vector2 | Phaser.Math.Vector3

The Vector to add to this Vector.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 317)
Focus
Focus
addScalar(s)
Focus
Focus

Description:

Add the given value to each component of this Vector.

Parameters:

name type description
s number

The amount to add to this Vector.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 336)
Focus
Focus
addScale(v, scale)
Focus
Focus

Description:

Add and scale a given Vector to this Vector. Addition is component-wise.

Parameters:

name type description
v Phaser.Math.Vector2 | Phaser.Math.Vector3

The Vector to add to this Vector.

scale number

The amount to scale v by.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 355)
Focus
Focus
addVectors(a, b)
Focus
Focus

Description:

Adds the two given Vector3s and sets the results into this Vector3.

Parameters:

name type description
a Phaser.Math.Vector3

The first Vector to add.

b Phaser.Math.Vector3

The second Vector to add.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 147)
Focus
Focus
applyMatrix3(mat3)
Focus
Focus

Description:

Takes a Matrix3 and applies it to this Vector3.

Parameters:

name type description
mat3 Phaser.Math.Matrix3

The Matrix3 to apply to this Vector3.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 651)
Focus
Focus
applyMatrix4(mat4)
Focus
Focus

Description:

Takes a Matrix4 and applies it to this Vector3.

Parameters:

name type description
mat4 Phaser.Math.Matrix4

The Matrix4 to apply to this Vector3.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 675)
Focus
Focus
clone()
Focus
Focus

Description:

Make a clone of this Vector3.

Returns:
Description:

A new Vector3 object containing this Vectors values.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 134)
Focus
Focus
copy(src)
Focus
Focus

Description:

Copy the components of a given Vector into this Vector.

Parameters:

name type description
src Phaser.Math.Vector2 | Phaser.Math.Vector3

The Vector to copy the components from.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 211)
Focus
Focus
cross(v)
Focus
Focus

Description:

Calculate the cross (vector) product of this Vector (which will be modified) and the given Vector.

Parameters:

name type description
v Phaser.Math.Vector3

The Vector to cross product with.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 597)
Focus
Focus
crossVectors(a, b)
Focus
Focus

Description:

Calculate the cross (vector) product of two given Vectors.

Parameters:

name type description
a Phaser.Math.Vector3

The first Vector to multiply.

b Phaser.Math.Vector3

The second Vector to multiply.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 167)
Focus
Focus
distance(v)
Focus
Focus

Description:

Calculate the distance between this Vector and the given Vector.

Parameters:

name type description
v Phaser.Math.Vector2 | Phaser.Math.Vector3

The Vector to calculate the distance to.

Returns:
Description:

The distance from this Vector to the given Vector.

Type:
  • number
Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 481)
Focus
Focus
distanceSq(v)
Focus
Focus

Description:

Calculate the distance between this Vector and the given Vector, squared.

Parameters:

name type description
v Phaser.Math.Vector2 | Phaser.Math.Vector3

The Vector to calculate the distance to.

Returns:
Description:

The distance from this Vector to the given Vector, squared.

Type:
  • number
Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 500)
Focus
Focus
divide(v)
Focus
Focus

Description:

Perform a component-wise division between this Vector and the given Vector.

Divides this Vector by the given Vector.

Parameters:

name type description
v Phaser.Math.Vector2 | Phaser.Math.Vector3

The Vector to divide this Vector by.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 443)
Focus
Focus
dot(v)
Focus
Focus

Description:

Calculate the dot product of this Vector and the given Vector.

Parameters:

name type description
v Phaser.Math.Vector3

The Vector3 to dot product with this Vector3.

Returns:
Description:

The dot product of this Vector and v.

Type:
  • number
Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 582)
Focus
Focus
equals(v)
Focus
Focus

Description:

Check whether this Vector is equal to a given Vector.

Performs a strict equality check against each Vector's components.

Parameters:

name type description
v Phaser.Math.Vector3

The Vector3 to compare against.

Returns:
Description:

True if the two vectors strictly match, otherwise false.

Type:
  • boolean
Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 194)
Focus
Focus
fromArray(array, [offset])
Focus
Focus

Description:

Sets the components of this Vector3 from the given array, based on the offset.

Vector3.x = array[offset] Vector3.y = array[offset + 1] Vector3.z = array[offset + 2]

Parameters:

name type arguments description
array Array.<number>

The array of values to get this Vector from.

offset number <optional>

The offset index into the array.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 291)
Focus
Focus
length()
Focus
Focus

Description:

Calculate the length (or magnitude) of this Vector.

Returns:
Description:

The length of this Vector.

Type:
  • number
Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 519)
Focus
Focus
lengthSq()
Focus
Focus

Description:

Calculate the length of this Vector squared.

Returns:
Description:

The length of this Vector, squared.

Type:
  • number
Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 536)
Focus
Focus
lerp(v, [t])
Focus
Focus

Description:

Linearly interpolate between this Vector and the given Vector.

Interpolates this Vector towards the given Vector.

Parameters:

name type arguments description
v Phaser.Math.Vector3

The Vector3 to interpolate towards.

t number <optional>

The interpolation percentage, between 0 and 1.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 623)
Focus
Focus
max(v)
Focus
Focus

Description:

Sets the components of this Vector to be the Math.max result from the given vector.

Parameters:

name type description
v Phaser.Math.Vector3

The Vector3 to check the maximum values against.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 115)
Focus
Focus
min(v)
Focus
Focus

Description:

Sets the components of this Vector to be the Math.min result from the given vector.

Parameters:

name type description
v Phaser.Math.Vector3

The Vector3 to check the minimum values against.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 96)
Focus
Focus
multiply(v)
Focus
Focus

Description:

Perform a component-wise multiplication between this Vector and the given Vector.

Multiplies this Vector by the given Vector.

Parameters:

name type description
v Phaser.Math.Vector2 | Phaser.Math.Vector3

The Vector to multiply this Vector by.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 394)
Focus
Focus
negate()
Focus
Focus

Description:

Negate the x, y and z components of this Vector.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 464)
Focus
Focus
normalize()
Focus
Focus

Description:

Normalize this Vector.

Makes the vector a unit length vector (magnitude of 1) in the same direction.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 553)
Focus
Focus
project(mat)
Focus
Focus

Description:

Multiplies this Vector3 by the specified matrix, applying a W divide. This is useful for projection, e.g. unprojecting a 2D point into 3D space.

Parameters:

name type description
mat Phaser.Math.Matrix4

The Matrix4 to multiply this Vector3 with.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 813)
Focus
Focus
projectViewMatrix(viewMatrix, projectionMatrix)
Focus
Focus

Description:

Multiplies this Vector3 by the given view and projection matrices.

Parameters:

name type description
viewMatrix Phaser.Math.Matrix4

A View Matrix.

projectionMatrix Phaser.Math.Matrix4

A Projection Matrix.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 857)
Focus
Focus
reset()
Focus
Focus

Description:

Make this Vector the zero vector (0, 0, 0).

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 925)
Focus
Focus
scale(scale)
Focus
Focus

Description:

Scale this Vector by the given value.

Parameters:

name type description
scale number

The value to scale this Vector by.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 415)
Focus
Focus
set(x, [y], [z])
Focus
Focus

Description:

Set the x, y, and z components of this Vector to the given x, y, and z values.

Parameters:

name type arguments description
x number | object

The x value to set for this Vector, or an object containing x, y and z components.

y number <optional>

The y value to set for this Vector.

z number <optional>

The z value to set for this Vector.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 230)
Focus
Focus
setFromMatrixColumn(mat4, index)
Focus
Focus

Description:

Sets the components of this Vector3 from the Matrix4 column specified.

Parameters:

name type description
mat4 Phaser.Math.Matrix4

The Matrix4 to get the column from.

index number

The column index.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 275)
Focus
Focus
setFromMatrixPosition(mat4)
Focus
Focus

Description:

Sets the components of this Vector3 from the position of the given Matrix4.

Parameters:

name type description
mat4 Phaser.Math.Matrix4

The Matrix4 to get the position from.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 260)
Focus
Focus
subtract(v)
Focus
Focus

Description:

Subtract the given Vector from this Vector. Subtraction is component-wise.

Parameters:

name type description
v Phaser.Math.Vector2 | Phaser.Math.Vector3

The Vector to subtract from this Vector.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 375)
Focus
Focus
transformCoordinates(mat)
Focus
Focus

Description:

Transforms the coordinates of this Vector3 with the given Matrix4.

Parameters:

name type description
mat Phaser.Math.Matrix4

The Matrix4 to transform this Vector3 with.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 749)
Focus
Focus
transformMat3(mat)
Focus
Focus

Description:

Transform this Vector with the given Matrix.

Parameters:

name type description
mat Phaser.Math.Matrix3

The Matrix3 to transform this Vector3 with.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 701)
Focus
Focus
transformMat4(mat)
Focus
Focus

Description:

Transform this Vector with the given Matrix4.

Parameters:

name type description
mat Phaser.Math.Matrix4

The Matrix4 to transform this Vector3 with.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 725)
Focus
Focus
transformQuat(q)
Focus
Focus

Description:

Transform this Vector with the given Quaternion.

Parameters:

name type description
q Phaser.Math.Quaternion

The Quaternion to transform this Vector with.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 778)
Focus
Focus
unproject(viewport, invProjectionView)
Focus
Focus

Description:

Unproject this point from 2D space to 3D space. The point should have its x and y properties set to 2D screen space, and the z either at 0 (near plane) or 1 (far plane). The provided matrix is assumed to already be combined, i.e. projection * view * model.

After this operation, this vector's (x, y, z) components will represent the unprojected 3D coordinate.

Parameters:

name type description
viewport Phaser.Math.Vector4

Screen x, y, width and height in pixels.

invProjectionView Phaser.Math.Matrix4

Combined projection and view matrix.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 889)
Focus
Focus
unprojectViewMatrix(projectionMatrix, worldMatrix)
Focus
Focus

Description:

Multiplies this Vector3 by the given inversed projection matrix and world matrix.

Parameters:

name type description
projectionMatrix Phaser.Math.Matrix4

An inversed Projection Matrix.

worldMatrix Phaser.Math.Matrix4

A World View Matrix.

Returns:
Description:

This Vector3.

Examples:
Since: 3.50.0
Source: src/math/Vector3.js (Line 873)
Focus
Focus
up()
Focus
Focus

Description:

Set this Vector to point up.

Sets the y component of the vector to 1, and the others to 0.

Returns:
Description:

This Vector3.

Examples:
Since: 3.0.0
Source: src/math/Vector3.js (Line 77)
Focus
Focus