Skip to main content
Version: Phaser v4.0.0

Vector4

A four-component vector (x, y, z, w) for use in 3D mathematics and transformations.

Vector4 is commonly used to represent homogeneous coordinates in 3D space, where the w component acts as a scaling factor, making it compatible with 4x4 matrix multiplication via Matrix4. It is also used to represent RGBA color values, quaternion components, and any other quantity that requires four independent scalar values. The class supports the standard vector operations you would expect: addition, subtraction, scaling, normalization, dot product, linear interpolation, and transformation by both Matrix4 and Quaternion objects.

Constructor

new Vector4([x], [y], [z], [w])

Parameters

nametypeoptionaldescription
xnumberYesThe x component.
ynumberYesThe y component.
znumberYesThe z component.
wnumberYesThe w component.

Scope: static

Source: src/math/Vector4.js#L12
Since: 3.0.0

Public Members

w

w: number

Description:

The w component of this Vector.

Source: src/math/Vector4.js#L69
Since: 3.0.0


x

x: number

Description:

The x component of this Vector.

Source: src/math/Vector4.js#L39
Since: 3.0.0


y

y: number

Description:

The y component of this Vector.

Source: src/math/Vector4.js#L49
Since: 3.0.0


z

z: number

Description:

The z component of this Vector.

Source: src/math/Vector4.js#L59
Since: 3.0.0


Public Methods

add

<instance> add(v)

Description:

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

Parameters:

nametypeoptionaldescription
vPhaser.Math.Vector2 | Phaser.Math.Vector3Phaser.Math.Vector4No

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L178
Since: 3.0.0


clone

<instance> clone()

Description:

Make a clone of this Vector4.

Returns: Phaser.Math.Vector4 - A clone of this Vector4.

Source: src/math/Vector4.js#L95
Since: 3.0.0


copy

<instance> copy(src)

Description:

Copy the components of a given Vector into this Vector.

Parameters:

nametypeoptionaldescription
srcPhaser.Math.Vector4NoThe Vector to copy the components from.

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L108
Since: 3.0.0


distance

<instance> distance(v)

Description:

Calculate the distance between this Vector and the given Vector.

Parameters:

nametypeoptionaldescription
vPhaser.Math.Vector2 | Phaser.Math.Vector3Phaser.Math.Vector4No

Returns: number - The distance from this Vector to the given Vector.

Source: src/math/Vector4.js#L394
Since: 3.0.0


distanceSq

<instance> distanceSq(v)

Description:

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

Parameters:

nametypeoptionaldescription
vPhaser.Math.Vector2 | Phaser.Math.Vector3Phaser.Math.Vector4No

Returns: number - The distance from this Vector to the given Vector, squared.

Source: src/math/Vector4.js#L414
Since: 3.0.0


divide

<instance> divide(v)

Description:

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

Divides this Vector by the given Vector.

Parameters:

nametypeoptionaldescription
vPhaser.Math.Vector2 | Phaser.Math.Vector3Phaser.Math.Vector4No

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L372
Since: 3.0.0


dot

<instance> dot(v)

Description:

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

Parameters:

nametypeoptionaldescription
vPhaser.Math.Vector4NoThe Vector4 to dot product with this Vector4.

Returns: number - The dot product of this Vector and the given Vector.

Source: src/math/Vector4.js#L305
Since: 3.0.0


equals

<instance> equals(v)

Description:

Check whether this Vector is equal to a given Vector.

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

Parameters:

nametypeoptionaldescription
vPhaser.Math.Vector4NoThe vector to check equality with.

Returns: boolean - A boolean indicating whether the two Vectors are equal or not.

Source: src/math/Vector4.js#L128
Since: 3.0.0


length

<instance> length()

Description:

Calculate the length (or magnitude) of this Vector.

Returns: number - The length of this Vector.

Source: src/math/Vector4.js#L238
Since: 3.0.0


lengthSq

<instance> lengthSq()

Description:

Calculate the length of this Vector squared.

Returns: number - The length of this Vector, squared.

Source: src/math/Vector4.js#L256
Since: 3.0.0


lerp

<instance> lerp(v, [t])

Description:

Linearly interpolate between this Vector and the given Vector.

Interpolates this Vector towards the given Vector.

Parameters:

nametypeoptionaldefaultdescription
vPhaser.Math.Vector4NoThe Vector4 to interpolate towards.
tnumberYes0The interpolation percentage, between 0 and 1.

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L320
Since: 3.0.0


multiply

<instance> multiply(v)

Description:

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

Multiplies this Vector by the given Vector.

Parameters:

nametypeoptionaldescription
vPhaser.Math.Vector2 | Phaser.Math.Vector3Phaser.Math.Vector4No

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L350
Since: 3.0.0


negate

<instance> negate()

Description:

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

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L434
Since: 3.0.0


normalize

<instance> normalize()

Description:

Normalize this Vector.

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

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L274
Since: 3.0.0


reset

<instance> reset()

Description:

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

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L512
Since: 3.0.0


scale

<instance> scale(scale)

Description:

Scale this Vector by the given value.

Parameters:

nametypeoptionaldescription
scalenumberNoThe value to scale this Vector by.

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L218
Since: 3.0.0


set

<instance> set(x, y, z, w)

Description:

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

Parameters:

nametypeoptionaldescription
xnumber | objectNoThe x value to set for this Vector, or an object containing x, y, z and w components.
ynumberNoThe y value to set for this Vector.
znumberNoThe z value to set for this Vector.
wnumberNoThe w value to set for this Vector.

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L145
Since: 3.0.0


subtract

<instance> subtract(v)

Description:

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

Parameters:

nametypeoptionaldescription
vPhaser.Math.Vector2 | Phaser.Math.Vector3Phaser.Math.Vector4No

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L198
Since: 3.0.0


transformMat4

<instance> transformMat4(mat)

Description:

Transform this Vector with the given Matrix.

Parameters:

nametypeoptionaldescription
matPhaser.Math.Matrix4NoThe Matrix4 to transform this Vector4 with.

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L452
Since: 3.0.0


transformQuat

<instance> transformQuat(q)

Description:

Transform this Vector with the given Quaternion.

Parameters:

nametypeoptionaldescription
qPhaser.Math.QuaternionNoThe Quaternion to transform this Vector with.

Returns: Phaser.Math.Vector4 - This Vector4.

Source: src/math/Vector4.js#L478
Since: 3.0.0