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 | 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 19)

Members


x :number

The x component of this Vector.

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

y :number

The y component of this Vector.

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

<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 616)

<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 592)

<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 628)

<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 580)

<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 604)

<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 568)

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 219)
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 197)
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 73)
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 86)
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 465)
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 337)
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 355)
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 301)
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 450)
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 180)
Returns:

Whether the given Vector is equal to this Vector.

Type
boolean

length()

Calculate the length (or magnitude) of this Vector.

Since: 3.0.0
Source: src/math/Vector2.js (Line 373)
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 389)
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 480)
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 255)
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 321)
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 405)
Returns:

This Vector2.

Type
Phaser.Math.Vector2

normalizeRightHand()

Right-hand normalize (make unit length) this Vector.

Since: 3.0.0
Source: src/math/Vector2.js (Line 432)
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 550)
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 275)
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 122)
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 Vector2Like

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

Since: 3.0.0
Source: src/math/Vector2.js (Line 104)
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 143)
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 159)
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 237)
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 506)
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 528)
Returns:

This Vector2.

Type
Phaser.Math.Vector2