Constructor
# new Vector2(xopt, yopt)
Create a 2D vector with the x and y passed in, can also be created with vec2()
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
x |
Number |
<optional> |
0 | X axis location |
y |
Number |
<optional> |
0 | Y axis location |
- Source:
Example
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
Members
# x
Properties
Type | Description |
---|---|
Number | X axis location |
- Source:
# y
Properties
Type | Description |
---|---|
Number | Y axis location |
- Source:
Methods
# add(vector) → {Vector2}
Returns a copy of this vector plus the vector passed in
Parameters:
Name | Type | Description |
---|---|---|
vector |
Vector2 |
- Source:
Returns:
- Type
- Vector2
# angle() → {Number}
Returns the angle of this vector, up is angle 0
- Source:
Returns:
- Type
- Number
# area() → {Number}
Returns the area this vector covers as a rectangle
- Source:
Returns:
- Type
- Number
# arrayCheck(arraySize) → {Boolean}
Returns true if this vector is within the bounds of an array size passed in
Parameters:
Name | Type | Description |
---|---|---|
arraySize |
Vector2 |
- Source:
Returns:
- Type
- Boolean
# clampLength(lengthopt) → {Vector2}
Returns a new vector clamped to length passed in
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
length |
Number |
<optional> |
1 |
- Source:
Returns:
- Type
- Vector2
# copy() → {Vector2}
Returns a new vector that is a copy of this
- Source:
Returns:
- Type
- Vector2
# cross(vector) → {Number}
Returns the cross product of this and the vector passed in
Parameters:
Name | Type | Description |
---|---|---|
vector |
Vector2 |
- Source:
Returns:
- Type
- Number
# direction() → {Number}
Returns the integer direction of this vector, corrosponding to multiples of 90 degree rotation (0-3)
- Source:
Returns:
- Type
- Number
# distance(vector) → {Number}
Returns the distance from this vector to vector passed in
Parameters:
Name | Type | Description |
---|---|---|
vector |
Vector2 |
- Source:
Returns:
- Type
- Number
# distanceSquared(vector) → {Number}
Returns the distance squared from this vector to vector passed in
Parameters:
Name | Type | Description |
---|---|---|
vector |
Vector2 |
- Source:
Returns:
- Type
- Number
# divide(vector) → {Vector2}
Returns a copy of this vector divided by the vector passed in
Parameters:
Name | Type | Description |
---|---|---|
vector |
Vector2 |
- Source:
Returns:
- Type
- Vector2
# dot(vector) → {Number}
Returns the dot product of this and the vector passed in
Parameters:
Name | Type | Description |
---|---|---|
vector |
Vector2 |
- Source:
Returns:
- Type
- Number
# flip() → {Vector2}
Returns a copy of this vector with the axies flipped
- Source:
Returns:
- Type
- Vector2
# floor() → {Vector2}
Returns a copy of this vector with each axis floored
- Source:
Returns:
- Type
- Vector2
# invert() → {Vector2}
Returns a copy of this vector that has been inverted
- Source:
Returns:
- Type
- Vector2
# length() → {Number}
Returns the length of this vector
- Source:
Returns:
- Type
- Number
# lengthSquared() → {Number}
Returns the length of this vector squared
- Source:
Returns:
- Type
- Number
# lerp(vector, percent) → {Vector2}
Returns a new vector that is p percent between this and the vector passed in
Parameters:
Name | Type | Description |
---|---|---|
vector |
Vector2 | |
percent |
Number |
- Source:
Returns:
- Type
- Vector2
# multiply(vector) → {Vector2}
Returns a copy of this vector times the vector passed in
Parameters:
Name | Type | Description |
---|---|---|
vector |
Vector2 |
- Source:
Returns:
- Type
- Vector2
# normalize(lengthopt) → {Vector2}
Returns a new vector in same direction as this one with the length passed in
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
length |
Number |
<optional> |
1 |
- Source:
Returns:
- Type
- Vector2
# rotate(angle) → {Vector2}
Returns copy of this vector rotated by the angle passed in
Parameters:
Name | Type | Description |
---|---|---|
angle |
Number |
- Source:
Returns:
- Type
- Vector2
# scale(scale) → {Vector2}
Returns a copy of this vector scaled by the vector passed in
Parameters:
Name | Type | Description |
---|---|---|
scale |
Number |
- Source:
Returns:
- Type
- Vector2
# setAngle(angleopt, lengthopt)
Sets this vector with angle and length passed in
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
angle |
Number |
<optional> |
0 | |
length |
Number |
<optional> |
1 |
- Source:
# subtract(vector) → {Vector2}
Returns a copy of this vector minus the vector passed in
Parameters:
Name | Type | Description |
---|---|---|
vector |
Vector2 |
- Source:
Returns:
- Type
- Vector2