Options
All
• Public
• Public/Protected
• All

# Class Vector2

Class representing a vector containing 2 coordinates Example Playground - Overview - https://playground.babylonjs.com/#QYBWV4#9

## Constructors

• new Vector2(x?: number, y?: number): Vector2
• Creates a new Vector2 from the given x and y coordinates

#### Parameters

• ##### Optional x: number

defines the first coordinate

• ##### Optional y: number

defines the second coordinate

## Properties

x: number

defines the first coordinate

y: number

defines the second coordinate

## Accessors

• Gets a zero Vector2 that must not be updated

## Methods

• Add another vector with the current one Example Playground https://playground.babylonjs.com/#QYBWV4#11

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

#### Returns Vector2

a new Vector2 set with the addition of the current Vector2 and the given one coordinates

• Set the Vector2 coordinates by adding the given Vector2 coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#13

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

#### Returns Vector2

the current updated Vector2

• Sets the "result" coordinates with the addition of the current Vector2 and the given one coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#12

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

• ##### result: T

defines the target vector

#### Returns T

result input

• Gets a new Vector2 by adding the current Vector2 coordinates to the given Vector3 x, y coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#14

#### Parameters

• ##### otherVector: Vector3

defines the other vector

#### Returns Vector2

a new Vector2

• asArray(): number[]
• Copy the current vector to an array Example Playground https://playground.babylonjs.com/#QYBWV4#40

#### Returns number[]

a new array with 2 elements: the Vector2 coordinates.

• Sets the Vector2 coordinates with the given Vector2 coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#24

#### Parameters

• ##### source: DeepImmutableObject<Vector2>

defines the source Vector2

#### Returns Vector2

the current updated Vector2

• copyFromFloats(x: number, y: number): Vector2
• Sets the Vector2 coordinates with the given floats Example Playground https://playground.babylonjs.com/#QYBWV4#25

#### Parameters

• ##### x: number

defines the first coordinate

• ##### y: number

defines the second coordinate

#### Returns Vector2

the current updated Vector2

• Returns a new Vector2 set with the Vector2 coordinates divided by the given one coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#27

#### Parameters

• ##### otherVector: Vector2

defines the other vector

#### Returns Vector2

a new Vector2

• Divides the current Vector2 coordinates by the given ones Example Playground https://playground.babylonjs.com/#QYBWV4#28

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

#### Returns Vector2

the current updated Vector2

• Sets the "result" coordinates with the Vector2 divided by the given one coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#30

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

• ##### result: T

defines the target vector

#### Returns T

result input

• Gets a boolean if two vectors are equals Example Playground https://playground.babylonjs.com/#QYBWV4#31

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

#### Returns boolean

true if the given vector coordinates strictly equal the current Vector2 ones

• Gets a boolean if two vectors are equals (using an epsilon value) Example Playground https://playground.babylonjs.com/#QYBWV4#32

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

• ##### Optional epsilon: number

defines the minimal distance to consider equality

#### Returns boolean

true if the given vector coordinates are close to the current ones by a distance of epsilon.

• Gets a new Vector2 from current Vector2 floored values Example Playground https://playground.babylonjs.com/#QYBWV4#35 eg (1.2, 2.31) returns (1, 2)

#### Returns Vector2

a new Vector2

• Gets a new Vector2 from current Vector2 fractional values Example Playground https://playground.babylonjs.com/#QYBWV4#34 eg (1.2, 2.31) returns (0.2, 0.31)

#### Returns Vector2

a new Vector2

• Update the current vector from an array Example Playground https://playground.babylonjs.com/#QYBWV4#39

#### Parameters

• ##### array: FloatArray

defines the destination array

• ##### Optional index: number

defines the offset in the destination array

#### Returns Vector2

the current Vector2

• getClassName(): string
• Gets class name

#### Returns string

the string "Vector2"

• getHashCode(): number
• Gets current vector hash code

#### Returns number

the Vector2 hash code as a number

• length(): number
• Gets the length of the vector

#### Returns number

the vector length (float)

• lengthSquared(): number
• Gets the vector squared length

#### Returns number

the vector squared length (float)

• Returns a new Vector2 set with the multiplication of the current Vector2 and the given one coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#42

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

#### Returns Vector2

a new Vector2

• multiplyByFloats(x: number, y: number): Vector2
• Gets a new Vector2 set with the Vector2 coordinates multiplied by the given floats Example Playground https://playground.babylonjs.com/#QYBWV4#89

#### Parameters

• ##### x: number

defines the first coordinate

• ##### y: number

defines the second coordinate

#### Returns Vector2

a new Vector2

• Multiplies in place the current Vector2 coordinates by the given ones Example Playground https://playground.babylonjs.com/#QYBWV4#43

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

#### Returns Vector2

the current updated Vector2

• Sets "result" coordinates with the multiplication of the current Vector2 and the given one coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#44

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

• ##### result: T

defines the target vector

#### Returns T

result input

• Gets a new Vector2 with current Vector2 negated coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#22

#### Returns Vector2

a new Vector2

• negateToRef<T>(result: T): T
• Negate the current Vector2 and stores the result in the given vector "result" coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#41

#### Parameters

• ##### result: T

defines the Vector3 object where to store the result

#### Returns T

the result

• rotateToRef<T>(angle: number, result: T): T
• Rotate the current vector into a given result vector Example Playground https://playground.babylonjs.com/#QYBWV4#49

#### Parameters

• ##### angle: number

defines the rotation angle

• ##### result: T

defines the result vector where to store the rotated vector

#### Returns T

result input

• Returns a new Vector2 scaled by "scale" from the current Vector2 Example Playground https://playground.babylonjs.com/#QYBWV4#52

#### Parameters

• ##### scale: number

defines the scaling factor

#### Returns Vector2

a new Vector2

• scaleAndAddToRef<T>(scale: number, result: T): T
• Scale the current Vector2 values by a factor and add the result to a given Vector2 Example Playground https://playground.babylonjs.com/#QYBWV4#58

#### Parameters

• ##### scale: number

defines the scale factor

• ##### result: T

defines the Vector2 object where to store the result

#### Returns T

result input

• scaleInPlace(scale: number): Vector2
• Multiply the Vector2 coordinates by Example Playground https://playground.babylonjs.com/#QYBWV4#59

#### Parameters

• ##### scale: number

defines the scaling factor

#### Returns Vector2

the current updated Vector2

• scaleToRef<T>(scale: number, result: T): T
• Scale the current Vector2 values by a factor to a given Vector2 Example Playground https://playground.babylonjs.com/#QYBWV4#57

#### Parameters

• ##### scale: number

defines the scale factor

• ##### result: T

defines the Vector2 object where to store the result

#### Returns T

result input

• Sets the Vector2 coordinates with the given floats Example Playground https://playground.babylonjs.com/#QYBWV4#62

#### Parameters

• ##### x: number

defines the first coordinate

• ##### y: number

defines the second coordinate

#### Returns Vector2

the current updated Vector2

• Gets a new Vector2 set with the subtracted coordinates of the given one from the current Vector2 Example Playground https://playground.babylonjs.com/#QYBWV4#61

#### Parameters

• ##### otherVector: Vector2

defines the other vector

#### Returns Vector2

a new Vector2

• Sets the current Vector2 coordinates by subtracting from it the given one coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#88

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

#### Returns Vector2

the current updated Vector2

• Sets the "result" coordinates with the subtraction of the given one from the current Vector2 coordinates. Example Playground https://playground.babylonjs.com/#QYBWV4#63

#### Parameters

• ##### otherVector: DeepImmutableObject<Vector2>

defines the other vector

• ##### result: T

defines the target vector

#### Returns T

result input

• Sets the Vector2 coordinates in the given array or Float32Array from the given index. Example Playground https://playground.babylonjs.com/#QYBWV4#15

#### Parameters

• ##### array: FloatArray

defines the source array

• ##### Optional index: number

defines the offset in source array

#### Returns Vector2

the current Vector2

• toString(): string
• Gets a string with the Vector2 coordinates

#### Returns string

a string with the Vector2 coordinates

• Gets a new Vector2 located for "amount" (float) on the CatmullRom spline defined by the given four Vector2 Example Playground https://playground.babylonjs.com/#QYBWV4#65

#### Parameters

• ##### value1: DeepImmutable<T>

defines 1st point of control

• ##### value2: DeepImmutableObject<Vector2>

defines 2nd point of control

• ##### value3: DeepImmutableObject<Vector2>

defines 3rd point of control

• ##### value4: DeepImmutableObject<Vector2>

defines 4th point of control

• ##### amount: number

defines the interpolation factor

#### Returns T

a new Vector2

• Gets a new Vector2 located at the center of the vectors "value1" and "value2" Example Playground https://playground.babylonjs.com/#QYBWV4#86 Example Playground https://playground.babylonjs.com/#QYBWV4#66

#### Parameters

• ##### value1: DeepImmutable<T>

defines first vector

• ##### value2: DeepImmutableObject<Vector2>

defines second vector

#### Returns T

a new Vector2

• Gets the center of the vectors "value1" and "value2" and stores the result in the vector "ref" Example Playground https://playground.babylonjs.com/#QYBWV4#66

#### Parameters

• ##### value1: DeepImmutableObject<Vector2>

defines first vector

• ##### value2: DeepImmutableObject<Vector2>

defines second vector

• ##### ref: T

defines third vector

#### Returns T

ref

• Returns a new Vector2 set with same the coordinates than "value" ones if the vector "value" is in the square defined by "min" and "max". If a coordinate of "value" is lower than "min" coordinates, the returned Vector2 is given this "min" coordinate. If a coordinate of "value" is greater than "max" coordinates, the returned Vector2 is given this "max" coordinate Example Playground https://playground.babylonjs.com/#QYBWV4#76

#### Parameters

• ##### value: DeepImmutable<T>

defines the value to clamp

• ##### min: DeepImmutableObject<Vector2>

defines the lower limit

• ##### max: DeepImmutableObject<Vector2>

defines the upper limit

#### Returns T

a new Vector2

• Gets the distance between the vectors "value1" and "value2" Example Playground https://playground.babylonjs.com/#QYBWV4#71

#### Parameters

• ##### value1: DeepImmutableObject<Vector2>

defines first vector

• ##### value2: DeepImmutableObject<Vector2>

defines second vector

#### Returns number

the distance between vectors

• Gets the shortest distance (float) between the point "p" and the segment defined by the two points "segA" and "segB". Example Playground https://playground.babylonjs.com/#QYBWV4#77

#### Parameters

• ##### p: DeepImmutableObject<Vector2>

defines the middle point

• ##### segA: DeepImmutableObject<Vector2>

defines one point of the segment

• ##### segB: DeepImmutableObject<Vector2>

defines the other point of the segment

#### Returns number

the shortest distance

• Returns the squared distance between the vectors "value1" and "value2" Example Playground https://playground.babylonjs.com/#QYBWV4#72

#### Parameters

• ##### value1: DeepImmutableObject<Vector2>

defines first vector

• ##### value2: DeepImmutableObject<Vector2>

defines second vector

#### Returns number

the squared distance between vectors

• Gets the dot product of the vector "left" and the vector "right" Example Playground https://playground.babylonjs.com/#QYBWV4#90

#### Parameters

• ##### left: DeepImmutableObject<Vector2>

defines first vector

• ##### right: DeepImmutableObject<Vector2>

defines second vector

#### Returns number

the dot product (float)

• Gets a new Vector2 set from the given index element of the given array Example Playground https://playground.babylonjs.com/#QYBWV4#79

#### Parameters

• ##### array: DeepImmutableObject<ArrayLike<number>>

defines the data source

• ##### Optional offset: number

defines the offset in the data source

#### Returns Vector2

a new Vector2

• Sets "result" from the given index element of the given array Example Playground https://playground.babylonjs.com/#QYBWV4#80

#### Parameters

• ##### array: DeepImmutableObject<ArrayLike<number>>

defines the data source

• ##### offset: number

defines the offset in the data source

• ##### result: T

defines the target vector

#### Returns T

result input

• Returns a new Vector2 located for "amount" (float) on the Hermite spline defined by the vectors "value1", "value2", "tangent1", "tangent2" Example Playground https://playground.babylonjs.com/#QYBWV4#81

#### Parameters

• ##### value1: DeepImmutable<T>

defines the 1st control point

• ##### tangent1: DeepImmutableObject<Vector2>

defines the outgoing tangent

• ##### value2: DeepImmutableObject<Vector2>

defines the 2nd control point

• ##### tangent2: DeepImmutableObject<Vector2>

defines the incoming tangent

• ##### amount: number

defines the interpolation factor

#### Returns T

a new Vector2

• Returns a new Vector2 which is the 1st derivative of the Hermite spline defined by the vectors "value1", "value2", "tangent1", "tangent2". Example Playground https://playground.babylonjs.com/#QYBWV4#82

#### Parameters

• ##### value1: DeepImmutable<T>

defines the first control point

• ##### tangent1: DeepImmutableObject<Vector2>

defines the first tangent

• ##### value2: DeepImmutableObject<Vector2>

defines the second control point

• ##### tangent2: DeepImmutableObject<Vector2>

defines the second tangent

• ##### time: number

define where the derivative must be done

#### Returns T

1st derivative

• Returns a new Vector2 which is the 1st derivative of the Hermite spline defined by the vectors "value1", "value2", "tangent1", "tangent2". Example Playground https://playground.babylonjs.com/#QYBWV4#83

#### Parameters

• ##### value1: DeepImmutableObject<Vector2>

defines the first control point

• ##### tangent1: DeepImmutableObject<Vector2>

defines the first tangent

• ##### value2: DeepImmutableObject<Vector2>

defines the second control point

• ##### tangent2: DeepImmutableObject<Vector2>

defines the second tangent

• ##### time: number

define where the derivative must be done

• ##### result: T

define where the derivative will be stored

#### Returns T

result input

• Returns a new Vector2 located for "amount" (float) on the linear interpolation between the vector "start" adn the vector "end". Example Playground https://playground.babylonjs.com/#QYBWV4#84

#### Parameters

• ##### start: DeepImmutable<T>

defines the start vector

• ##### end: DeepImmutableObject<Vector2>

defines the end vector

• ##### amount: number

defines the interpolation factor

#### Returns Vector2

a new Vector2

• Gets a new Vector2 set with the maximal coordinate values from the "left" and "right" vectors Example Playground https://playground.babylonjs.com/#QYBWV4#86

#### Parameters

• ##### left: DeepImmutable<T>

defines 1st vector

• ##### right: DeepImmutableObject<Vector2>

defines 2nd vector

#### Returns T

a new Vector2

• Gets a new Vector2 set with the minimal coordinate values from the "left" and "right" vectors Example Playground https://playground.babylonjs.com/#QYBWV4#86

#### Parameters

• ##### left: DeepImmutable<T>

defines 1st vector

• ##### right: DeepImmutableObject<Vector2>

defines 2nd vector

#### Returns T

a new Vector2

• Returns a new Vector2 equal to the normalized given vector Example Playground https://playground.babylonjs.com/#QYBWV4#46

#### Parameters

• ##### vector: DeepImmutable<T>

defines the vector to normalize

#### Returns T

a new Vector2

• Normalize a given vector into a second one Example Playground https://playground.babylonjs.com/#QYBWV4#50

#### Parameters

• ##### vector: DeepImmutableObject<Vector2>

defines the vector to normalize

• ##### result: T

defines the vector where to store the result

#### Returns T

result input

• Gets a new Vector2(1, 1)

#### Returns Vector2

a new Vector2

• Determines if a given vector is included in a triangle Example Playground https://playground.babylonjs.com/#QYBWV4#87

#### Parameters

• ##### p: DeepImmutableObject<Vector2>

defines the vector to test

• ##### p0: DeepImmutableObject<Vector2>

defines 1st triangle point

• ##### p1: DeepImmutableObject<Vector2>

defines 2nd triangle point

• ##### p2: DeepImmutableObject<Vector2>

defines 3rd triangle point

#### Returns boolean

true if the point "p" is in the triangle defined by the vectors "p0", "p1", "p2"

• Gets a new Vector2 set with the transformed coordinates of the given vector by the given transformation matrix Example Playground https://playground.babylonjs.com/#QYBWV4#17

#### Parameters

• ##### vector: DeepImmutable<T>

defines the vector to transform

• ##### transformation: DeepImmutableObject<Matrix>

defines the matrix to apply

#### Returns T

a new Vector2

• Transforms the given vector coordinates by the given transformation matrix and stores the result in the vector "result" coordinates Example Playground https://playground.babylonjs.com/#QYBWV4#19

#### Parameters

• ##### vector: DeepImmutableObject<Vector2>

defines the vector to transform

• ##### transformation: DeepImmutableObject<Matrix>

defines the matrix to apply

• ##### result: T

defines the target vector

#### Returns T

result input

• Gets a new Vector2(0, 0)

#### Returns Vector2

a new Vector2

## Legend

• Constructor
• Property
• Method
• Inherited constructor
• Inherited property
• Inherited method
• Property
• Method
• Static method

Theme