Constructor
new Vector2(xopt, yopt)
Create a 2D vector with the x and y passed in, can also be created with vec2()
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x | number | <optional> | 0 | X axis location |
y | number | <optional> | 0 | Y axis location |
- Source
let a = new Vector2(2, 3); // vector with coordinates (2, 3)
let b = new Vector2; // vector with coordinates (0, 0)
let c = vec2(4, 2); // use the vec2 function to make a Vector2
let d = a.add(b).scale(5); // operators can be chained
Members
x
PropertiesType | Description |
---|---|
number | X axis location |
- Source
y
PropertiesType | Description |
---|---|
number | Y axis location |
- Source
Methods
add(v) → {Vector2}
Returns a copy of this vector plus the vector passed in
Name | Type | Description |
---|---|---|
v | Vector2 | other vector |
- Source
- Type:
- Vector2
angle() → {number}
Returns the clockwise angle of this vector, up is angle 0
- Source
- Type:
- number
area() → {number}
Returns the area this vector covers as a rectangle
- Source
- Type:
- number
arrayCheck(arraySize) → {boolean}
Returns true if this vector is within the bounds of an array size passed in
Name | Type | Description |
---|---|---|
arraySize | Vector2 |
- Source
- Type:
- boolean
clampLength(lengthopt) → {Vector2}
Returns a new vector clamped to length passed in
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
length | number | <optional> | 1 |
- Source
- Type:
- Vector2
copy() → {Vector2}
Returns a new vector that is a copy of this
- Source
- Type:
- Vector2
cross(v) → {number}
Returns the cross product of this and the vector passed in
Name | Type | Description |
---|---|---|
v | Vector2 | other vector |
- Source
- Type:
- number
direction() → {number}
Returns the integer direction of this vector, corresponding to multiples of 90 degree rotation (0-3)
- Source
- Type:
- number
distance(v) → {number}
Returns the distance from this vector to vector passed in
Name | Type | Description |
---|---|---|
v | Vector2 | other vector |
- Source
- Type:
- number
distanceSquared(v) → {number}
Returns the distance squared from this vector to vector passed in
Name | Type | Description |
---|---|---|
v | Vector2 | other vector |
- Source
- Type:
- number
divide(v) → {Vector2}
Returns a copy of this vector divided by the vector passed in
Name | Type | Description |
---|---|---|
v | Vector2 | other vector |
- Source
- Type:
- Vector2
dot(v) → {number}
Returns the dot product of this and the vector passed in
Name | Type | Description |
---|---|---|
v | Vector2 | other vector |
- Source
- Type:
- number
floor() → {Vector2}
Returns a copy of this vector with each axis floored
- Source
- Type:
- Vector2
invert() → {Vector2}
Returns a copy of this vector that has been inverted
- Source
- Type:
- Vector2
isValid() → {boolean}
Checks if this is a valid vector
- Source
- Type:
- boolean
length() → {number}
Returns the length of this vector
- Source
- Type:
- number
lengthSquared() → {number}
Returns the length of this vector squared
- Source
- Type:
- number
lerp(v, percent) → {Vector2}
Returns a new vector that is p percent between this and the vector passed in
Name | Type | Description |
---|---|---|
v | Vector2 | other vector |
percent | number |
- Source
- Type:
- Vector2
multiply(v) → {Vector2}
Returns a copy of this vector times the vector passed in
Name | Type | Description |
---|---|---|
v | Vector2 | other vector |
- Source
- Type:
- Vector2
normalize(lengthopt) → {Vector2}
Returns a new vector in same direction as this one with the length passed in
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
length | number | <optional> | 1 |
- Source
- Type:
- Vector2
rotate(angle) → {Vector2}
Returns copy of this vector rotated by the clockwise angle passed in
Name | Type | Description |
---|---|---|
angle | number |
- Source
- Type:
- Vector2
scale(s) → {Vector2}
Returns a copy of this vector scaled by the vector passed in
Name | Type | Description |
---|---|---|
s | number | scale |
- Source
- Type:
- Vector2
set(xopt, yopt) → {Vector2}
Sets values of this vector and returns self
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x | number | <optional> | 0 | X axis location |
y | number | <optional> | 0 | Y axis location |
- Source
- Type:
- Vector2
setAngle(angleopt, lengthopt) → {Vector2}
Sets this vector with clockwise angle and length passed in
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
angle | number | <optional> | 0 | |
length | number | <optional> | 1 |
- Source
- Type:
- Vector2
setDirection(directionopt, lengthopt)
Set the integer direction of this vector, corresponding to multiples of 90 degree rotation (0-3)
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
direction | number | <optional> | ||
length | number | <optional> | 1 |
- Source
subtract(v) → {Vector2}
Returns a copy of this vector minus the vector passed in
Name | Type | Description |
---|---|---|
v | Vector2 | other vector |
- Source
- Type:
- Vector2
toString(digits) → {string}
Returns this vector expressed as a string
Name | Type | Default | Description |
---|---|---|---|
digits | number | 3 | precision to display |
- Source
- Type:
- string