Class: Vector2

Phaser.Math. Vector2

A representation of a vector in 2D space.

A two-component vector.


new Vector2( [x] [, y])

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


x :number

The x component of this Vector.

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

y :number

The y component of this Vector.

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

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

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)

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

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)

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

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)

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

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

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

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)

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

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)

Methods


add(src)

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.

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

This Vector2.

Type
Phaser.Math.Vector2

angle()

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

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

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

Type
number

clone()

Make a clone of this Vector2.

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

A clone of this Vector2.

Type
Phaser.Math.Vector2

copy(src)

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.

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

This Vector2.

Type
Phaser.Math.Vector2

cross(src)

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.

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

The cross product of this Vector and the given Vector.

Type
number

distance(src)

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.

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

The distance from this Vector to the given Vector.

Type
number

distanceSq(src)

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.

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

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

Type
number

divide(src)

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.

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

This Vector2.

Type
Phaser.Math.Vector2

dot(src)

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.

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

The dot product of this Vector and the given Vector.

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

The vector to compare with this Vector.

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

Whether the given Vector is equal to this Vector.

Type
boolean

fuzzyEquals(v [, epsilon])

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

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

The vector to compare with this Vector.

epsilon number <optional>
0.0001

The tolerance value.

Since: 3.23.0
Source: src/math/Vector2.js (Line 191)
Returns:

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

Type
boolean

length()

Calculate the length (or magnitude) of this Vector.

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

The length of this Vector.

Type
number

lengthSq()

Calculate the length of this Vector squared.

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

The length of this Vector, squared.

Type
number

lerp(src [, t])

Linearly interpolate between this Vector and the given Vector.

Interpolates this Vector towards the given Vector.

Parameters:
Name Type Argument Default Description
src Phaser.Math.Vector2

The Vector2 to interpolate towards.

t number <optional>
0

The interpolation percentage, between 0 and 1.

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

This Vector2.

Type
Phaser.Math.Vector2

limit(max)

Limit the length (or magnitude) of this Vector.

Parameters:
Name Type Description
max number

The maximum length.

Since: 3.23.0
Source: src/math/Vector2.js (Line 624)
Returns:

This Vector2.

Type
Phaser.Math.Vector2

mirror(axis)

Reflect this Vector across another.

Parameters:
Name Type Description
axis Phaser.Math.Vector2

A vector to reflect across.

Since: 3.23.0
Source: src/math/Vector2.js (Line 663)
Returns:

This Vector2.

Type
Phaser.Math.Vector2

multiply(src)

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.

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

This Vector2.

Type
Phaser.Math.Vector2

negate()

Negate the x and y components of this Vector.

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

This Vector2.

Type
Phaser.Math.Vector2

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/Vector2.js (Line 445)
Returns:

This Vector2.

Type
Phaser.Math.Vector2

normalizeLeftHand()

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

Since: 3.23.0
Source: src/math/Vector2.js (Line 490)
Returns:

This Vector2.

Type
Phaser.Math.Vector2

normalizeRightHand()

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

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

This Vector2.

Type
Phaser.Math.Vector2

reflect(normal)

Reflect this Vector off a line defined by a normal.

Parameters:
Name Type Description
normal Phaser.Math.Vector2

A vector perpendicular to the line.

Since: 3.23.0
Source: src/math/Vector2.js (Line 646)
Returns:

This Vector2.

Type
Phaser.Math.Vector2

reset()

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

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

This Vector2.

Type
Phaser.Math.Vector2

rotate(delta)

Rotate this Vector by an angle amount.

Parameters:
Name Type Description
delta number

The angle to rotate by, in radians.

Since: 3.23.0
Source: src/math/Vector2.js (Line 678)
Returns:

This Vector2.

Type
Phaser.Math.Vector2

scale(value)

Scale this Vector by the given value.

Parameters:
Name Type Description
value number

The value to scale this Vector by.

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

This Vector2.

Type
Phaser.Math.Vector2

set(x [, y])

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

Parameters:
Name Type Argument Default Description
x number

The x value to set for this Vector.

y number <optional>
x

The y value to set for this Vector.

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

This Vector2.

Type
Phaser.Math.Vector2

setAngle(angle)

Set the angle of this Vector.

Parameters:
Name Type Description
angle number

The angle, in radians.

Since: 3.23.0
Source: src/math/Vector2.js (Line 229)
Returns:

This Vector2.

Type
Phaser.Math.Vector2

setFromObject(obj)

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.

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

This Vector2.

Type
Phaser.Math.Vector2

setLength(length)

Set the length (or magnitude) of this Vector.

Parameters:
Name Type Description
length number
Since: 3.23.0
Source: src/math/Vector2.js (Line 414)
Returns:

This Vector2.

Type
Phaser.Math.Vector2

setTo(x [, y])

This method is an alias for Vector2.set.

Parameters:
Name Type Argument Default Description
x number

The x value to set for this Vector.

y number <optional>
x

The y value to set for this Vector.

Since: 3.4.0
Source: src/math/Vector2.js (Line 137)
Returns:

This Vector2.

Type
Phaser.Math.Vector2

setToPolar(azimuth [, radius])

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

Parameters:
Name Type Argument Default Description
azimuth number

The angular coordinate, in radians.

radius number <optional>
1

The radial coordinate (length).

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

This Vector2.

Type
Phaser.Math.Vector2

subtract(src)

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.

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

This Vector2.

Type
Phaser.Math.Vector2

transformMat3(mat)

Transform this Vector with the given Matrix.

Parameters:
Name Type Description
mat Phaser.Math.Matrix3

The Matrix3 to transform this Vector2 with.

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

This Vector2.

Type
Phaser.Math.Vector2

transformMat4(mat)

Transform this Vector with the given Matrix.

Parameters:
Name Type Description
mat Phaser.Math.Matrix4

The Matrix4 to transform this Vector2 with.

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

This Vector2.

Type
Phaser.Math.Vector2