Class: Vector3

Phaser.Math. Vector3

A representation of a vector in 3D space.

A three-component vector.


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

Parameters:
Name Type Argument 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


x :number

The x component of this Vector.

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

y :number

The y component of this Vector.

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

z :number

The z component of this Vector.

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

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

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)

<static, constant> DOWN :Phaser.Math.Vector3

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)

<static, constant> FORWARD :Phaser.Math.Vector3

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)

<static, constant> LEFT :Phaser.Math.Vector3

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)

<static, constant> ONE :Phaser.Math.Vector3

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)

<static, constant> RIGHT :Phaser.Math.Vector3

A static right 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 956)

<static, constant> UP :Phaser.Math.Vector3

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)

<static, constant> ZERO :Phaser.Math.Vector3

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)

Methods


add(v)

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.

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

This Vector3.

Type
Phaser.Math.Vector3

addScalar(s)

Add the given value to each component of this Vector.

Parameters:
Name Type Description
s number

The amount to add to this Vector.

Since: 3.50.0
Source: src/math/Vector3.js (Line 336)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

addScale(v, scale)

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.

Since: 3.50.0
Source: src/math/Vector3.js (Line 355)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

addVectors(a, b)

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.

Since: 3.50.0
Source: src/math/Vector3.js (Line 147)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

applyMatrix3(mat3)

Takes a Matrix3 and applies it to this Vector3.

Parameters:
Name Type Description
mat3 Phaser.Math.Matrix3

The Matrix3 to apply to this Vector3.

Since: 3.50.0
Source: src/math/Vector3.js (Line 651)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

applyMatrix4(mat4)

Takes a Matrix4 and applies it to this Vector3.

Parameters:
Name Type Description
mat4 Phaser.Math.Matrix4

The Matrix4 to apply to this Vector3.

Since: 3.50.0
Source: src/math/Vector3.js (Line 675)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

clone()

Make a clone of this Vector3.

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

A new Vector3 object containing this Vectors values.

Type
Phaser.Math.Vector3

copy(src)

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.

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

This Vector3.

Type
Phaser.Math.Vector3

cross(v)

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.

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

This Vector3.

Type
Phaser.Math.Vector3

crossVectors(a, b)

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.

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

This Vector3.

Type
Phaser.Math.Vector3

distance(v)

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.

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

The distance from this Vector to the given Vector.

Type
number

distanceSq(v)

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.

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

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

Type
number

divide(v)

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.

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

This Vector3.

Type
Phaser.Math.Vector3

dot(v)

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.

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

The dot product of this Vector and v.

Type
number

equals(v)

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.

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

True if the two vectors strictly match, otherwise false.

Type
boolean

fromArray(array [, offset])

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 Argument Default Description
array Array.<number>

The array of values to get this Vector from.

offset number <optional>
0

The offset index into the array.

Since: 3.50.0
Source: src/math/Vector3.js (Line 291)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

length()

Calculate the length (or magnitude) of this Vector.

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

The length of this Vector.

Type
number

lengthSq()

Calculate the length of this Vector squared.

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

The length of this Vector, squared.

Type
number

lerp(v [, t])

Linearly interpolate between this Vector and the given Vector.

Interpolates this Vector towards the given Vector.

Parameters:
Name Type Argument Default Description
v Phaser.Math.Vector3

The Vector3 to interpolate towards.

t number <optional>
0

The interpolation percentage, between 0 and 1.

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

This Vector3.

Type
Phaser.Math.Vector3

max(v)

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.

Since: 3.50.0
Source: src/math/Vector3.js (Line 115)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

min(v)

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.

Since: 3.50.0
Source: src/math/Vector3.js (Line 96)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

multiply(v)

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.

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

This Vector3.

Type
Phaser.Math.Vector3

negate()

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

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

This Vector3.

Type
Phaser.Math.Vector3

normalize()

Normalize this Vector.

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

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

This Vector3.

Type
Phaser.Math.Vector3

project(mat)

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.

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

This Vector3.

Type
Phaser.Math.Vector3

projectViewMatrix(viewMatrix, projectionMatrix)

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.

Since: 3.50.0
Source: src/math/Vector3.js (Line 857)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

reset()

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

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

This Vector3.

Type
Phaser.Math.Vector3

scale(scale)

Scale this Vector by the given value.

Parameters:
Name Type Description
scale number

The value to scale this Vector by.

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

This Vector3.

Type
Phaser.Math.Vector3

set(x [, y] [, z])

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

Parameters:
Name Type Argument 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.

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

This Vector3.

Type
Phaser.Math.Vector3

setFromMatrixColumn(mat4, index)

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.

Since: 3.50.0
Source: src/math/Vector3.js (Line 275)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

setFromMatrixPosition(mat4)

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.

Since: 3.50.0
Source: src/math/Vector3.js (Line 260)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

subtract(v)

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.

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

This Vector3.

Type
Phaser.Math.Vector3

transformCoordinates(mat)

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.

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

This Vector3.

Type
Phaser.Math.Vector3

transformMat3(mat)

Transform this Vector with the given Matrix.

Parameters:
Name Type Description
mat Phaser.Math.Matrix3

The Matrix3 to transform this Vector3 with.

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

This Vector3.

Type
Phaser.Math.Vector3

transformMat4(mat)

Transform this Vector with the given Matrix4.

Parameters:
Name Type Description
mat Phaser.Math.Matrix4

The Matrix4 to transform this Vector3 with.

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

This Vector3.

Type
Phaser.Math.Vector3

transformQuat(q)

Transform this Vector with the given Quaternion.

Parameters:
Name Type Description
q Phaser.Math.Quaternion

The Quaternion to transform this Vector with.

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

This Vector3.

Type
Phaser.Math.Vector3

unproject(viewport, invProjectionView)

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.

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

This Vector3.

Type
Phaser.Math.Vector3

unprojectViewMatrix(projectionMatrix, worldMatrix)

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.

Since: 3.50.0
Source: src/math/Vector3.js (Line 873)
Returns:

This Vector3.

Type
Phaser.Math.Vector3

up()

Set this Vector to point up.

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

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

This Vector3.

Type
Phaser.Math.Vector3