Vector4

Phaser.Math.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

instanceadd(v)

Description:

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

Parameters:

Returns: Phaser.Math.Vector4 - This Vector4.

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


clone

instanceclone()

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

instancecopy(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

instancedistance(v)

Description:

Calculate the distance between this Vector and the given Vector.

Parameters:

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

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


distanceSq

instancedistanceSq(v)

Description:

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

Parameters:

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

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


divide

instancedivide(v)

Description:

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

Divides this Vector by the given Vector.

Parameters:

Returns: Phaser.Math.Vector4 - This Vector4.

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


dot

instancedot(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

instanceequals(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

instancelength()

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

instancelengthSq()

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

instancelerp(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

instancemultiply(v)

Description:

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

Multiplies this Vector by the given Vector.

Parameters:

Returns: Phaser.Math.Vector4 - This Vector4.

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


negate

instancenegate()

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

instancenormalize()

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

instancereset()

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

instancescale(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

instanceset(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

instancesubtract(v)

Description:

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

Parameters:

Returns: Phaser.Math.Vector4 - This Vector4.

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


transformMat4

instancetransformMat4(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

instancetransformQuat(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