Constructor
new Vector2(xopt, yopt)
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x | number | | <optional> | 0 | the x value |
y | number | <optional> | x | y - the y value |
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x | number | <optional> | 0 | The x value |
y | number | <optional> | x | The y value |
- Source
new Vector2(56, 78); // (56, 78)
Vector2.zero(); // (0,0)
Classes
Methods
absolute() → {Vector2}
Change the values to absolute values
- Source
this Vector2 updated
- Type:
- Vector2
new Vector2(-1, 5).absolute() // (1, 5)
add(vector) → {Vector2}
Add the values of this vector 2 with the values of the given vector2
Name | Type | Description |
---|---|---|
vector | Vector2 | the vector to add |
- Source
This Vector2
- Type:
- Vector2
new Vector2(1,2).add(new Vector2(10)); // (11, 12)
angle() → {number}
Calculate the angle between this Vector and the positive x-axis.
- Source
the result
- Type:
- number
clone() → {Vector2}
Return a new Vector2 with the same values of this
- Source
- Type:
- Vector2
new Vector2(1, 2).clone() // (1, 2)
diferenceAngle(target) → {number}
Calculate the angle between this Vector and the given Vector.
Name | Type | Description |
---|---|---|
target | Vector2 | the vector to get the angle |
- Source
- Type:
- number
new Vector2(5, 10).diferenceAngle(new Vector2(5, 10)); // 0
new Vector2(1000, 123).diferenceAngle(new Vector2(5, 10)); // -173.52080244507272
Vector2.zero().diferenceAngle(new Vector2(90, 90)); // 45
distance(vector) → {number}
Returns the distance between this vector and a given vector.
Name | Type | Description |
---|---|---|
vector | Vector2 | the vector to compare |
- Source
- Type:
- number
new Vector2(5, 10).distance(); // 125
new Vector2(5, 10).distance(new Vector2(100, 20))); // 9125
distanceSqrt(vector) → {number}
Returns the squared distance between this vector and a given vector.
Name | Type | Description |
---|---|---|
vector | Vector2 | the vector to compare |
- Source
- Type:
- number
new Vector2(5, 10).distanceSqrt(); // 11.180339887498949
new Vector2(5, 10).distanceSqrt(new Vector2(100, 20)); // 95.524865872714
Vector2.zero().distanceSqrt(new Vector2(100, 20)); // 101.9803902718557
divide(vector2) → {Vector2}
Divides the values of vector 2 with the values of the given vector2
Name | Type | Description |
---|---|---|
vector2 | Vector2 | the vector to divide |
- Source
This Vector2
- Type:
- Vector2
new Vector2(10,5).divide(new Vector2(5)); // (5, 2.5)
dot(vector) → {number}
Dot product of two vectors.
Name | Type | Description |
---|---|---|
vector | Vector2 |
- Source
- Type:
- number
new Vector2(10,5).dot(new Vector2(5)); // 75
equals(vector) → {boolean}
Returns true if the given vector is exactly equal to this vector.
Name | Type | Description |
---|---|---|
vector | Vector2 | the vector to compare |
- Source
- Type:
- boolean
new Vector2(5, 10).equals(new Vector2(5, 10)); // true
new Vector2(10, 5).equals(new Vector2(5, 10)) // false
invert() → {Vector2}
Invert the X and Y values of this Vector2
- Source
This Vector2.
- Type:
- Vector2
new Vector2(-1, 5).invert() // (5, -1)
magnitude() → {number}
Returns the length of this vector.
- Source
- Type:
- number
new Vector2(0).magnitude(); // 0
new Vector2(1).magnitude(); // 2
new Vector2(2).magnitude(); // 8
new Vector2(5, 10).magnitude(); // 125
magnitudeSqr() → {number}
Returns the squared length of this vector.
- Source
- Type:
- number
new Vector2(0).magnitude(); // 0
new Vector2(1).magnitude(); // 1.4142135623730951
new Vector2(2).magnitude(); // 2.8284271247461903
new Vector2(5, 10).magnitude(); // 11.180339887498949
moveTowards(target, step) → {Vector2}
Linearly interpolates between current vector and the given vector by time.
Name | Type | Default | Description |
---|---|---|---|
target | Vector2 | the vector to interpolate | |
step | number | 1 | step, distance of the target (between 0, and 1) |
- Source
this vector changed
- Type:
- Vector2
new Vector2(5,10).moveTowards(); // (0,0)
new Vector2(5,10).moveTowards(new Vector2(5, 10)); // (5,10)
new Vector2(5,10).moveTowards(new Vector2(5, 10), 0.5); // (7.5, 7.5)
multiply(vector2) → {Vector2}
Multiplies the values of vector 2 with the values of the given vector2
Name | Type | Description |
---|---|---|
vector2 | Vector2 | the vector to multiply |
- Source
This Vector2
- Type:
- Vector2
new Vector2(1,2).multiply(new Vector2(10)); // (10, 20)
negate() → {Vector2}
Negate the x
and y
components of this Vector.
- Source
this Vector2
- Type:
- Vector2
new Vector2(-1, 5).negate() // (1, 5)
negative() → {Vector2}
Change the values to negative values
- Source
This Vector2.
- Type:
- Vector2
new Vector2(-1, 5).negative() // (-1, -5)
normalize() → {Vector2}
Returns this vector with a magnitude of 1.
- Source
this vector normalized
- Type:
- Vector2
new Vector2(5, 10).normalize()); // (0.4472135954999579, 0.8944271909999159)
new Vector2(1000, 123).normalize()); // (0.9925202644900105, 0.1220799925322713)
Vector2.zero().normalize()); // (0, 0)
rotate(radiansopt) → {Vector2}
Return a new vector rotated
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
radians | number | <optional> | 0 | the radians to rotate |
- Source
- Type:
- Vector2
scale(valueopt) → {Vector2}
Multiplies the values of vector 2 with the given value.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
value | number | <optional> | 1 | value - the value to multiply/scale |
- Source
This Vector2
- Type:
- Vector2
new Vector2(1,2).scale(2); // (2, 3)
set(xopt, yopt) → {Vector2}
Set x and y components of an existing Vector2.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x | number | | <optional> | 0 | the new X value |
y | number | <optional> | x | the new Y value |
- Source
This Vector2
- Type:
- Vector2
new Vector2(1, 2).set(3, 4); // (3, 4)
new Vector2(1, 2).set({5.-10}); // (5, -10)
new Vector2(1, 2).set(new Vector2(-100, 55)); // (-100, 5)
subtract(vector) → {Vector2}
Subtracts the values of vector 2 with the values of the given vector2
Normally used to get the distance between two vectors.
Name | Type | Description |
---|---|---|
vector | Vector2 | the vector to subtract |
- Source
This Vector2
- Type:
- Vector2
new Vector2(1,2).subtract(new Vector2(10)); // (-9, -8)
new Vector2(0).distanceVector(new Vector2(10, 5)); // (10, 5)
new Vector2(5, 10).distanceVector(new Vector2(10, 5)); // (5, -5)
toPrecision(precision) → {Vector2}
Fix the precision to the given decimal places
Name | Type | Default | Description |
---|---|---|---|
precision | number | 1 | the number of decimal places |
- Source
- Type:
- Vector2
new Vector2(1.234, 5.123456).toPrecision(2) // (1.23, 5.12)
toString() → {string}
Returns a formatted string for this vector.
- Source
- Type:
- string
(static) down() → {Vector2}
Shorthand for writing Vector2(0, 1).
- Source
- Type:
- Vector2
Vector2.zero()
(static) left() → {Vector2}
Shorthand for writing Vector2(-1, 0).
- Source
- Type:
- Vector2
Vector2.zero()
(static) negativeInfinity() → {Vector2}
Shorthand for writing Vector2(-Infinity, -Infinity).
- Source
- Type:
- Vector2
Vector2.zero()
(static) one() → {Vector2}
Shorthand for writing Vector2(1, 1). * * @example Vector2.zero()
- Source
- Type:
- Vector2
(static) positiveInfinity() → {Vector2}
Shorthand for writing Vector2(Infinity, Infinity).
- Source
- Type:
- Vector2
Vector2.zero()
(static) random() → {Vector2}
Creates a random vector with random normalized values
- Source
- Type:
- Vector2
(static) right() → {Vector2}
Shorthand for writing Vector2(1, 0).
- Source
- Type:
- Vector2
Vector2.zero()
(static) up() → {Vector2}
Shorthand for writing Vector2(0, -1).
- Source
- Type:
- Vector2
Vector2.zero()
(static) zero() → {Vector2}
Shorthand for writing Vector2(0, 0).
- Source
- Type:
- Vector2
Vector2.zero()