Skip to main content
Version: Phaser v4.0.0

Vector2

A representation of a vector in 2D space, defined by an x and y component.

Vector2 is used throughout Phaser for positions, directions, velocities, and other quantities that have both magnitude and direction. It provides methods for common vector operations such as addition, subtraction, scaling, normalization, dot and cross products, linear interpolation, and rotation. Many Phaser APIs accept a Vector2Like object (any object with x and y number properties), making Vector2 easy to integrate across the framework.

Constructor

new Vector2([x], [y])

Parameters

nametypeoptionaldefaultdescription
xnumber | Phaser.Types.Math.Vector2LikeYes0The x component, or an object with x and y properties.
ynumberYes"x"The y component.

Scope: static

Source: src/math/Vector2.js#L13
Since: 3.0.0

Public Members

x

x: number

Description:

The x component of this Vector.

Source: src/math/Vector2.js#L38
Since: 3.0.0


y

y: number

Description:

The y component of this Vector.

Source: src/math/Vector2.js#L48
Since: 3.0.0


Public Methods

add

<instance> add(src)

Description:

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

Parameters:

nametypeoptionaldescription
srcPhaser.Types.Math.Vector2LikeNoThe Vector to add to this Vector.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L294
Since: 3.0.0


angle

<instance> angle()

Description:

Calculate the angle between this Vector and the positive x-axis, in radians.

Returns: number - The angle between this Vector, and the positive x-axis, given in radians.

Source: src/math/Vector2.js#L257
Since: 3.0.0


ceil

<instance> ceil()

Description:

Runs the x and y components of this Vector2 through Math.ceil and then sets them.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L158
Since: 4.0.0


clone

<instance> clone()

Description:

Make a clone of this Vector2.

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

Source: src/math/Vector2.js#L72
Since: 3.0.0


copy

<instance> copy(src)

Description:

Copy the components of a given Vector into this Vector.

Parameters:

nametypeoptionaldescription
srcPhaser.Types.Math.Vector2LikeNoThe Vector to copy the components from.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L85
Since: 3.0.0


cross

<instance> cross(src)

Description:

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

Parameters:

nametypeoptionaldescription
srcPhaser.Types.Math.Vector2LikeNoThe Vector2 to cross with this Vector2.

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

Source: src/math/Vector2.js#L573
Since: 3.0.0


distance

<instance> distance(src)

Description:

Calculate the distance between this Vector and the given Vector.

Parameters:

nametypeoptionaldescription
srcPhaser.Types.Math.Vector2LikeNoThe Vector to calculate the distance to.

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

Source: src/math/Vector2.js#L412
Since: 3.0.0


distanceSq

<instance> distanceSq(src)

Description:

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

Parameters:

nametypeoptionaldescription
srcPhaser.Types.Math.Vector2LikeNoThe Vector to calculate the distance to.

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

Source: src/math/Vector2.js#L430
Since: 3.0.0


divide

<instance> divide(src)

Description:

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

Divides this Vector by the given Vector.

Parameters:

nametypeoptionaldescription
srcPhaser.Types.Math.Vector2LikeNoThe Vector to divide this Vector by.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L376
Since: 3.0.0


dot

<instance> dot(src)

Description:

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

Parameters:

nametypeoptionaldescription
srcPhaser.Types.Math.Vector2LikeNoThe Vector2 to dot product with this Vector2.

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

Source: src/math/Vector2.js#L558
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.Types.Math.Vector2LikeNoThe vector to compare with this Vector.

Returns: boolean - Whether the given Vector is equal to this Vector.

Source: src/math/Vector2.js#L224
Since: 3.0.0


floor

<instance> floor()

Description:

Runs the x and y components of this Vector2 through Math.floor and then sets them.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L174
Since: 4.0.0


fuzzyEquals

<instance> fuzzyEquals(v, [epsilon])

Description:

Check whether this Vector is approximately equal to a given Vector.

Parameters:

nametypeoptionaldefaultdescription
vPhaser.Types.Math.Vector2LikeNoThe vector to compare with this Vector.
epsilonnumberYes0.0001The tolerance value.

Returns: boolean - Whether both absolute differences of the x and y components are smaller than epsilon.

Source: src/math/Vector2.js#L241
Since: 3.23.0


invert

<instance> invert()

Description:

Swaps the x and y components of this Vector2.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L190
Since: 4.0.0


length

<instance> length()

Description:

Calculate the length (or magnitude) of this Vector.

Returns: number - The length of this Vector.

Source: src/math/Vector2.js#L448
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/Vector2.js#L479
Since: 3.0.0


lerp

<instance> lerp(src, [t])

Description:

Linearly interpolate between this Vector and the given Vector.

Interpolates this Vector towards the given Vector.

Parameters:

nametypeoptionaldefaultdescription
srcPhaser.Types.Math.Vector2LikeNoThe Vector2 to interpolate towards.
tnumberYes0The interpolation percentage, between 0 and 1.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L588
Since: 3.0.0


limit

<instance> limit(max)

Description:

Limit the length (or magnitude) of this Vector.

Parameters:

nametypeoptionaldescription
maxnumberNoThe maximum length.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L674
Since: 3.23.0


mirror

<instance> mirror(axis)

Description:

Reflect this Vector across another.

Parameters:

nametypeoptionaldescription
axisPhaser.Math.Vector2NoA vector to reflect across.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L713
Since: 3.23.0


multiply

<instance> multiply(src)

Description:

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

Multiplies this Vector by the given Vector.

Parameters:

nametypeoptionaldescription
srcPhaser.Types.Math.Vector2LikeNoThe Vector to multiply this Vector by.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L330
Since: 3.0.0


negate

<instance> negate()

Description:

Negate the x and y components of this Vector.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L396
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.Vector2 - This Vector2.

Source: src/math/Vector2.js#L495
Since: 3.0.0


normalizeLeftHand

<instance> normalizeLeftHand()

Description:

Rotate this Vector to its perpendicular, in the negative direction.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L540
Since: 3.23.0


normalizeRightHand

<instance> normalizeRightHand()

Description:

Rotate this Vector to its perpendicular, in the positive direction.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L522
Since: 3.0.0


project

<instance> project(src)

Description:

Project this Vector onto another.

Parameters:

nametypeoptionaldescription
srcPhaser.Math.Vector2NoThe vector to project onto.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L746
Since: 3.60.0


projectUnit

<instance> projectUnit(vecB, [out])

Description:

Calculates the vector projection of this Vector2 onto the non-zero vecB. This is the orthogonal projection of this vector onto a straight line parallel to vecB.

Parameters:

nametypeoptionaldescription
vecBPhaser.Math.Vector2NoThe vector to project onto.
outPhaser.Math.Vector2YesThe Vector2 object to store the position in. If not given, a new Vector2 instance is created.

Returns: Phaser.Math.Vector2 - The out Vector2 containing the projected values.

Source: src/math/Vector2.js#L763
Since: 4.0.0


reflect

<instance> reflect(normal)

Description:

Reflect this Vector off a line defined by a normal.

Parameters:

nametypeoptionaldescription
normalPhaser.Math.Vector2NoA vector perpendicular to the line.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L696
Since: 3.23.0


reset

<instance> reset()

Description:

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

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L658
Since: 3.0.0


rotate

<instance> rotate(delta)

Description:

Rotate this Vector by an angle amount.

Parameters:

nametypeoptionaldescription
deltanumberNoThe angle to rotate by, in radians.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L728
Since: 3.23.0


scale

<instance> scale(value)

Description:

Scale this Vector by the given value.

Parameters:

nametypeoptionaldescription
valuenumberNoThe value to scale this Vector by.

Returns: Phaser.Math.Vector2 - This Vector2.

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


set

<instance> set(x, [y])

Description:

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

Parameters:

nametypeoptionaldefaultdescription
xnumberNoThe x value to set for this Vector.
ynumberYes"x"The y value to set for this Vector.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L121
Since: 3.0.0


setAngle

<instance> setAngle(angle)

Description:

Set the angle of this Vector.

Parameters:

nametypeoptionaldescription
anglenumberNoThe angle, in radians.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L279
Since: 3.23.0


setFromObject

<instance> setFromObject(obj)

Description:

Set the component values of this Vector from a given Vector2Like object.

Parameters:

nametypeoptionaldescription
objPhaser.Types.Math.Vector2LikeNoThe object containing the component values to set for this Vector.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L103
Since: 3.0.0


setLength

<instance> setLength(length)

Description:

Set the length (or magnitude) of this Vector.

Parameters:

nametypeoptionaldescription
lengthnumberNoThe new magnitude of this Vector.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L464
Since: 3.23.0


setTo

<instance> setTo(x, [y])

Description:

This method is an alias for Vector2.set.

Parameters:

nametypeoptionaldefaultdescription
xnumberNoThe x value to set for this Vector.
ynumberYes"x"The y value to set for this Vector.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L142
Since: 3.4.0


setToPolar

<instance> setToPolar(angle, [length])

Description:

Sets the x and y components of this Vector from the given angle and length.

Parameters:

nametypeoptionaldefaultdescription
anglenumberNoThe angle from the positive x-axis, in radians.
lengthnumberYes1The distance from the origin.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L203
Since: 3.0.0


subtract

<instance> subtract(src)

Description:

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

Parameters:

nametypeoptionaldescription
srcPhaser.Types.Math.Vector2LikeNoThe Vector to subtract from this Vector.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L312
Since: 3.0.0


transformMat3

<instance> transformMat3(mat)

Description:

Transform this Vector with the given Matrix3.

Parameters:

nametypeoptionaldescription
matPhaser.Math.Matrix3NoThe Matrix3 to transform this Vector2 with.

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L614
Since: 3.0.0


transformMat4

<instance> transformMat4(mat)

Description:

Transform this Vector with the given Matrix4.

Parameters:

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

Returns: Phaser.Math.Vector2 - This Vector2.

Source: src/math/Vector2.js#L636
Since: 3.0.0


Constants:

Public Members

DOWN

DOWN: Phaser.Math.Vector2

Description:

A static down Vector2 for use by reference.

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

Source: src/math/Vector2.js#L840
Since: 3.16.0


LEFT

LEFT: Phaser.Math.Vector2

Description:

A static left Vector2 for use by reference.

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

Source: src/math/Vector2.js#L816
Since: 3.16.0


ONE

ONE: Phaser.Math.Vector2

Description:

A static one Vector2 for use by reference.

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

Source: src/math/Vector2.js#L852
Since: 3.16.0


RIGHT: Phaser.Math.Vector2

Description:

A static right Vector2 for use by reference.

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

Source: src/math/Vector2.js#L804
Since: 3.16.0


UP

UP: Phaser.Math.Vector2

Description:

A static up Vector2 for use by reference.

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

Source: src/math/Vector2.js#L828
Since: 3.16.0


ZERO

ZERO: Phaser.Math.Vector2

Description:

A static zero Vector2 for use by reference.

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

Source: src/math/Vector2.js#L792
Since: 3.1.0