# Class: Vector2

## Vector2(x, y)

#### new Vector2(x, y)

Class representing a 2D vector. A 2D vector is an ordered pair of numbers (labeled x and y), which can be used to represent points in space, directions, etc.

##### Parameters:

Name | Type | Description
--- | --- | ---
`x` | number | X value.
`y` | number | Y value.

Source:
  - math/Vector2.js, line 8

### Methods

#### add(v)

Add the content of another vector to this one.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 | The other vector.

Source:
  - math/Vector2.js, line 63

#### addScalar(s)

Add a scalar value to booth vector components.

##### Parameters:

Name | Type | Description
--- | --- | ---
`s` | number | Scalar value.

Source:
  - math/Vector2.js, line 74

#### addScaledVector(v, s)

Scale a vector components and add the result to this vector.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 | The other vector.
`s` | number | Scalar value.

Source:
  - math/Vector2.js, line 98

#### addVectors(a, b)

Add two vectors and store the result in this vector.

##### Parameters:

Name | Type | Description
--- | --- | ---
`a` | Vector2 | The first vector.
`b` | Vector2 | The second vector.

Source:
  - math/Vector2.js, line 86

#### angle(forcePositive) → {number}

Computes the angle in radians with respect to the positive x-axis.

##### Parameters:

Name | Type | Description
--- | --- | ---
`forcePositive` | boolean | If true, the angle will be forced to be positive.

Source:
  - math/Vector2.js, line 350

##### Returns:

Angle in radians.

Type
  number

#### ceil()

Round the vector coordinates to integer by ceiling to the bigger integer.

Source:
  - math/Vector2.js, line 254

#### clamp(min, max)

Clamp the vector coordinates to the range defined by two vectors. Applied to x and y independently.

##### Parameters:

Name | Type | Description
--- | --- | ---
`min` | Vector2 | Minimum value.
`max` | Vector2 | Maximum value.

Source:
  - math/Vector2.js, line 216

#### clampScalar(minVal, maxVal)

Clamp the vector coordinates to the range defined by two scalars.

##### Parameters:

Name | Type | Description
--- | --- | ---
`minVal` | number | Minimum value.
`maxVal` | number | Maximum value.

Source:
  - math/Vector2.js, line 229

#### clone() → {Vector2}

Create a clone of this vector object.

Source:
  - math/Vector2.js, line 42

##### Returns:

A new vector with the same values as this one.

Type
  Vector2

#### copy(v)

Copy the content of another vector into this one.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 |

Source:
  - math/Vector2.js, line 52

#### cross(vector) → {number}

Cross multiplication between this vector and another vector.

##### Parameters:

Name | Type | Description
--- | --- | ---
`vector` | Vector2 |

Source:
  - math/Vector2.js, line 297

##### Returns:

Result of the cross multiplication.

Type
  number

#### distanceTo(v) → {number}

Distance between two vector positions.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 | Vector to compute the distance to.

Source:
  - math/Vector2.js, line 368

##### Returns:

Distance between the two vectors.

Type
  number

#### distanceToSquared(v) → {number}

Distance between two vector positions squared. Faster for comparisons.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 | Vector to compute the distance to.

Source:
  - math/Vector2.js, line 381

##### Returns:

Distance between the two vectors squared.

Type
  number

#### divide(v)

Divide the content of another vector from this one.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 |

Source:
  - math/Vector2.js, line 166

#### divideScalar(s)

Divide a scalar value by booth vector components.

##### Parameters:

Name | Type | Description
--- | --- | ---
`s` | number |

Source:
  - math/Vector2.js, line 177

#### dot(vector) → {number}

Dot multiplication between this vector and another vector.

##### Parameters:

Name | Type | Description
--- | --- | ---
`vector` | Vector2 |

Source:
  - math/Vector2.js, line 286

##### Returns:

Result of the dot multiplication.

Type
  number

#### equals(v1, v2, alpha, v) → {Vector2}

Lerp between this vector and another vector.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v1` | Vector2 | Vector to lerp from.
`v2` | Vector2 | Vector to lerp to.
`alpha` | number | Lerp factor.
`v` | Vector2 | Vector to compare with.

Source:
  - math/Vector2.js, line 441

##### Returns:

This vector. Vector2.prototype.lerpVectors = function(v1, v2, alpha) { return this.subVectors(v2, v1).multiplyScalar(alpha).add(v1); }; /** Check if two vectors are equal.

Type
  Vector2

#### floor()

Round the vector coordinates to integer by flooring to the smaller integer.

Source:
  - math/Vector2.js, line 245

#### fromArray(array)

Set vector value from array [x, y]. The vector can be converted to array using the toArray() method.

##### Parameters:

Name | Type | Description
--- | --- | ---
`array` | Array.<number> | Array to set the vector value from.

Source:
  - math/Vector2.js, line 453

#### length() → {number}

Length of the vector.

Source:
  - math/Vector2.js, line 319

##### Returns:

Length of the vector.

Type
  number

#### lengthSq() → {number}

Squared length of the vector. Faster for comparions.

Source:
  - math/Vector2.js, line 309

##### Returns:

Squared length of the vector.

Type
  number

#### lerp(v, alpha)

Lerp this vector to another vector.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 | Vector to lerp to.
`alpha` | number | Lerp factor.

Source:
  - math/Vector2.js, line 417

#### manhattanDistanceTo(v) → {number}

Manhattan distance between two vector positions.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 | Vector to compute the distance to.

Source:
  - math/Vector2.js, line 395

##### Returns:

Manhattan distance between the two vectors.

Type
  number

#### manhattanLength() → {number}

Manhattan length of the vector.

Source:
  - math/Vector2.js, line 329

##### Returns:

Manhattan length of the vector.

Type
  number

#### max(v)

Set the maximum of x and y coordinates between two vectors. X is set as the max between this vector and the other vector.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 |

Source:
  - math/Vector2.js, line 202

#### min(v)

Set the minimum of x and y coordinates between two vectors. X is set as the min between this vector and the other vector.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 |

Source:
  - math/Vector2.js, line 189

#### multiply(v)

Multiply the content of another vector to this one.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 | The other vector.

Source:
  - math/Vector2.js, line 143

#### multiplyScalar(scalar)

Multiply a scalar value by booth vector components.

##### Parameters:

Name | Type | Description
--- | --- | ---
`scalar` | number | Scalar value.

Source:
  - math/Vector2.js, line 154

#### negate()

Negate the coordinates of this vector.

Source:
  - math/Vector2.js, line 272

#### normalize() → {Vector2}

Normalize the vector (make it length one).

Source:
  - math/Vector2.js, line 339

##### Returns:

This vector.

Type
  Vector2

#### rotateAround(center, angle)

Rotate the vector around a central point.

##### Parameters:

Name | Type | Description
--- | --- | ---
`center` | Vector2 | Point to rotate around.
`angle` | number | Angle in radians.

Source:
  - math/Vector2.js, line 476

#### round()

Round the vector coordinates to their closest integer.

Source:
  - math/Vector2.js, line 263

#### set(x, y)

Set vector x and y values.

##### Parameters:

Name | Type | Description
--- | --- | ---
`x` | number | X value.
`y` | number | Y value.

Source:
  - math/Vector2.js, line 20

#### setLength(length) → {Vector2}

Scale the vector to have a defined length value.

##### Parameters:

Name | Type | Description
--- | --- | ---
`length` | number | Length to scale the vector to.

Source:
  - math/Vector2.js, line 406

##### Returns:

This vector.

Type
  Vector2

#### setScalar(scalar)

Set a scalar value into the x and y values.

##### Parameters:

Name | Type | Description
--- | --- | ---
`scalar` | number | Scalar value.

Source:
  - math/Vector2.js, line 31

#### sub(v)

Subtract the content of another vector to this one.

##### Parameters:

Name | Type | Description
--- | --- | ---
`v` | Vector2 | The other vector.

Source:
  - math/Vector2.js, line 109

#### subScalar(s)

Subtract a scalar value to booth vector components.

##### Parameters:

Name | Type | Description
--- | --- | ---
`s` | number | Scalar value.

Source:
  - math/Vector2.js, line 120

#### subVectors(a, b)

Subtract two vectors and store the result in this vector.

##### Parameters:

Name | Type | Description
--- | --- | ---
`a` | Vector2 | The first vector.
`b` | Vector2 | The second vector.

Source:
  - math/Vector2.js, line 132

#### toArray() → {Array.<number>}

Convert this vector to an array. Useful for serialization and storage. Values stored as [x, y].

Source:
  - math/Vector2.js, line 465

##### Returns:

Array containing the values of the vector.

Type
  Array.<number>