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 . Vector2

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 . Vector2

A representation of a vector in 2D space.

A two-component vector.

Constructor:

new Vector2([x], [y])

Parameters:

name type arguments description
x number | Phaser.Types.Math.Vector2Like <optional>

The x component, or an object with x and y properties.

y number <optional>

The y component.

Since: 3.0.0
Source: src/math/Vector2.js (Line 13)

Members

<static, constant> DOWN: Phaser.Math.Vector2
Focus
Focus

Description:

A static down Vector2 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/Vector2.js (Line 746)
Focus
Focus
<static, constant> LEFT: Phaser.Math.Vector2
Focus
Focus

Description:

A static left Vector2 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/Vector2.js (Line 722)
Focus
Focus
<static, constant> ONE: Phaser.Math.Vector2
Focus
Focus

Description:

A static one Vector2 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/Vector2.js (Line 758)
Focus
Focus
<static, constant> UP: Phaser.Math.Vector2
Focus
Focus

Description:

A static up Vector2 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/Vector2.js (Line 734)
Focus
Focus
<static, constant> ZERO: Phaser.Math.Vector2
Focus
Focus

Description:

A static zero Vector2 for use by reference.

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

Type:
Since: 3.1.0
Source: src/math/Vector2.js (Line 698)
Focus
Focus
x: number
Focus
Focus

Description:

The x component of this Vector.

Type:
number
Default: 0
Since: 3.0.0
Source: src/math/Vector2.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/Vector2.js (Line 43)
Focus
Focus

Methods

add(src)
Focus
Focus

Description:

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

Parameters:

name type description
src Phaser.Math.Vector2

The Vector to add to this Vector.

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 244)
Focus
Focus
angle()
Focus
Focus

Description:

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

Returns:
Description:

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

Type:
  • number
Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 207)
Focus
Focus
clone()
Focus
Focus

Description:

Make a clone of this Vector2.

Returns:
Description:

A clone of this Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 67)
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

The Vector to copy the components from.

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 80)
Focus
Focus
cross(src)
Focus
Focus

Description:

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

Parameters:

name type description
src Phaser.Math.Vector2

The Vector2 to cross with this Vector2.

Returns:
Description:

The cross product of this Vector and the given Vector.

Type:
  • number
Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 523)
Focus
Focus
distance(src)
Focus
Focus

Description:

Calculate the distance between this Vector and the given Vector.

Parameters:

name type description
src Phaser.Math.Vector2

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/Vector2.js (Line 362)
Focus
Focus
distanceSq(src)
Focus
Focus

Description:

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

Parameters:

name type description
src Phaser.Math.Vector2

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/Vector2.js (Line 380)
Focus
Focus
divide(src)
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
src Phaser.Math.Vector2

The Vector to divide this Vector by.

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 326)
Focus
Focus
dot(src)
Focus
Focus

Description:

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

Parameters:

name type description
src Phaser.Math.Vector2

The Vector2 to dot product with this Vector2.

Returns:
Description:

The dot product of this Vector and the given Vector.

Type:
  • number
Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 508)
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.Vector2

The vector to compare with this Vector.

Returns:
Description:

Whether the given Vector is equal to this Vector.

Type:
  • boolean
Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 174)
Focus
Focus
fuzzyEquals(v, [epsilon])
Focus
Focus

Description:

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

Parameters:

name type arguments Default description
v Phaser.Math.Vector2

The vector to compare with this Vector.

epsilon number <optional> 0.0001

The tolerance value.

Returns:
Description:

Whether both absolute differences of the x and y components are smaller than epsilon.

Type:
  • boolean
Examples:
Since: 3.23.0
Source: src/math/Vector2.js (Line 191)
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/Vector2.js (Line 398)
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/Vector2.js (Line 429)
Focus
Focus
lerp(src, [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
src Phaser.Math.Vector2

The Vector2 to interpolate towards.

t number <optional>

The interpolation percentage, between 0 and 1.

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 538)
Focus
Focus
limit(max)
Focus
Focus

Description:

Limit the length (or magnitude) of this Vector.

Parameters:

name type description
max number

The maximum length.

Returns:
Description:

This Vector2.

Examples:
Since: 3.23.0
Source: src/math/Vector2.js (Line 624)
Focus
Focus
mirror(axis)
Focus
Focus

Description:

Reflect this Vector across another.

Parameters:

name type description
axis Phaser.Math.Vector2

A vector to reflect across.

Returns:
Description:

This Vector2.

Examples:
Since: 3.23.0
Source: src/math/Vector2.js (Line 663)
Focus
Focus
multiply(src)
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
src Phaser.Math.Vector2

The Vector to multiply this Vector by.

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 280)
Focus
Focus
negate()
Focus
Focus

Description:

Negate the x and y components of this Vector.

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 346)
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 Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 445)
Focus
Focus
normalizeLeftHand()
Focus
Focus

Description:

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

Returns:
Description:

This Vector2.

Examples:
Since: 3.23.0
Source: src/math/Vector2.js (Line 490)
Focus
Focus
normalizeRightHand()
Focus
Focus

Description:

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

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 472)
Focus
Focus
reflect(normal)
Focus
Focus

Description:

Reflect this Vector off a line defined by a normal.

Parameters:

name type description
normal Phaser.Math.Vector2

A vector perpendicular to the line.

Returns:
Description:

This Vector2.

Examples:
Since: 3.23.0
Source: src/math/Vector2.js (Line 646)
Focus
Focus
reset()
Focus
Focus

Description:

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

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 608)
Focus
Focus
rotate(delta)
Focus
Focus

Description:

Rotate this Vector by an angle amount.

Parameters:

name type description
delta number

The angle to rotate by, in radians.

Returns:
Description:

This Vector2.

Examples:
Since: 3.23.0
Source: src/math/Vector2.js (Line 678)
Focus
Focus
scale(value)
Focus
Focus

Description:

Scale this Vector by the given value.

Parameters:

name type description
value number

The value to scale this Vector by.

Returns:
Description:

This Vector2.

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

Description:

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

Parameters:

name type arguments Default description
x number

The x value to set for this Vector.

y number <optional> x

The y value to set for this Vector.

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 116)
Focus
Focus
setAngle(angle)
Focus
Focus

Description:

Set the angle of this Vector.

Parameters:

name type description
angle number

The angle, in radians.

Returns:
Description:

This Vector2.

Examples:
Since: 3.23.0
Source: src/math/Vector2.js (Line 229)
Focus
Focus
setFromObject(obj)
Focus
Focus

Description:

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

Parameters:

name type description
obj Phaser.Types.Math.Vector2Like

The object containing the component values to set for this Vector.

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 98)
Focus
Focus
setLength(length)
Focus
Focus

Description:

Set the length (or magnitude) of this Vector.

Parameters:

name type
length number
Returns:
Description:

This Vector2.

Examples:
Since: 3.23.0
Source: src/math/Vector2.js (Line 414)
Focus
Focus
setTo(x, [y])
Focus
Focus

Description:

This method is an alias for Vector2.set.

Parameters:

name type arguments Default description
x number

The x value to set for this Vector.

y number <optional> x

The y value to set for this Vector.

Returns:
Description:

This Vector2.

Examples:
Since: 3.4.0
Source: src/math/Vector2.js (Line 137)
Focus
Focus
setToPolar(azimuth, [radius])
Focus
Focus

Description:

Sets the x and y values of this object from a given polar coordinate.

Parameters:

name type arguments Default description
azimuth number

The angular coordinate, in radians.

radius number <optional> 1

The radial coordinate (length).

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 153)
Focus
Focus
subtract(src)
Focus
Focus

Description:

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

Parameters:

name type description
src Phaser.Math.Vector2

The Vector to subtract from this Vector.

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 262)
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 Vector2 with.

Returns:
Description:

This Vector2.

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

Description:

Transform this Vector with the given Matrix.

Parameters:

name type description
mat Phaser.Math.Matrix4

The Matrix4 to transform this Vector2 with.

Returns:
Description:

This Vector2.

Examples:
Since: 3.0.0
Source: src/math/Vector2.js (Line 586)
Focus
Focus