Vector2
A simple Vector2 object
A Vector representing X and Y values in 2D space
-
Vector2
- new Vector2(x, y)
-
.add(vector) ⇒
Vector2
-
.addScalar(scalar) ⇒
Vector2
-
.addVectors(vector1, vector2) ⇒
Vector2
-
.angle() ⇒
Number
-
.angleBetween(vector) ⇒
Number
-
.angleTo(vector) ⇒
Number
-
.cartesianToPolar(x, y) ⇒
Object
-
.clone() ⇒
Vector2
-
.cross() ⇒
Number
-
.distanceTo(vector2) ⇒
Number
-
.divide(vector) ⇒
Vector2
-
.divideScalar(scalar) ⇒
Vector2
-
.dot(vector) ⇒
Number
-
.length(x, y) ⇒
Number
-
.limit(value) ⇒
Vector2
-
.mag() ⇒
Number
-
.magnitude() ⇒
Number
- .magSq() ⇒
-
.midpoint(vector) ⇒
Vector2
-
.multiply(vector) ⇒
Vector2
-
.multiplyScalar(scalar) ⇒
Vector2
- .normalize() ⇒
- .set(props)
-
.setFromPolar(length, angle) ⇒
Vector2
-
.sub(vector) ⇒
Vector2
-
.subScalar(scalar) ⇒
Vector2
-
.subVectors(vector1, vector2) ⇒
Vector2
new Vector2(x, y)
Constructor
Param | Type | Default | Description |
---|---|---|---|
x | Number |
0 |
x value for vector |
y | Number |
0 |
y value for vector |
Vector2
vector2.add(vector) ⇒ Add another vector2 to this vector2
Returns: Vector2
- - the modified vector (this)
Param | Type | Description |
---|---|---|
vector | Vector2 |
Vector2 to add to this one |
Example
- vector1.add(vector2);
Vector2
vector2.addScalar(scalar) ⇒ Add a scalar value to our vector
Returns: Vector2
- - the modified vector (this)
Param | Type | Description |
---|---|---|
scalar | Number |
number to add to both x and y |
Example
- vector.addScalar(5);
Vector2
vector2.addVectors(vector1, vector2) ⇒ Set this x and y vector to be the addition of two Vector2(s)
Returns: Vector2
- - the modified vector (this)
Param | Type | Description |
---|---|---|
vector1 | Vector2 |
first vector to add to this |
vector2 | Vector2 |
second vector to add to this |
Example
- let vector = new Vector2().addVectors(vector1, vector2);
Number
vector2.angle() ⇒ Calculate the angle to the coordinates 0, 0
Returns: Number
- - the angle from (0, 0) of this vector
Number
vector2.angleBetween(vector) ⇒ Calculate the angle between this vector and another vector
Returns: Number
- - the angle between this vector and another vector
Param | Type | Description |
---|---|---|
vector | Vector2 |
a vector to get the angle of |
Example
- let angle = vector1.angleBetween(vector2);
Number
vector2.angleTo(vector) ⇒ Calculate the angle in radians from this vector to another
Returns: Number
- - the angle in radians from this vector to another
Param | Type | Description |
---|---|---|
vector | Vector2 |
Vector2 to get the angle too |
Example
- let angle = vector1.angleTo(vector2);
Object
vector2.cartesianToPolar(x, y) ⇒ Calculate polar coordinates from our current x and y properties OR from a passed in x and y value
Returns: Object
- - The vector representation in polar coordinates (length and angle)
Param | Type | Description |
---|---|---|
x | Number |
x cartesian value - defaults to this Vector2 |
y | Number |
y cartesian value - defaults to this Vector2 |
Example
- let polar = vector.cartesianToPolar();
Vector2
vector2.clone() ⇒ Create a new vector of this vector
Returns: Vector2
- - the copy of this vector
Number
vector2.cross() ⇒ Calculate the cross product between this Vector2 and another Vector2
Returns: Number
- - the cross product
Example
- let crossProduct = vector1.cross(vector2);
Number
vector2.distanceTo(vector2) ⇒ Calculate the distance between the 2 Vector2 Objects
Returns: Number
- - the distance between this vector and another
Param | Type | Description |
---|---|---|
vector2 | Vector2 |
vector 2 to measure distance to this vector |
Example
- let distance = vector1.distanceTo(vector2);
Vector2
vector2.divide(vector) ⇒ Divide a Vector2 from this
Returns: Vector2
- - the modified vector (this)
Param | Type | Description |
---|---|---|
vector | Vector2 |
Vector2 to divide from this |
Example
- vector1.divide(vector2);
Vector2
vector2.divideScalar(scalar) ⇒ Divide a scalar value to our vector
Returns: Vector2
- - the modified vector (this)
Param | Type | Description |
---|---|---|
scalar | Number |
scalar number to divide to both x and y |
Example
- vector.divide(5);
Number
vector2.dot(vector) ⇒ The dot product gives a number as a "scalar"
Returns: Number
- - A number or scalar value which is the dot product
Param | Type | Description |
---|---|---|
vector | Vector2 |
{x: 0, y: 0} |
Example
- let dot = vector1.dot(vector2);
Number
vector2.length(x, y) ⇒ Return the length of the x and y value
Returns: Number
- - the length or magnitude of this vector
Param | Type | Description |
---|---|---|
x | Number |
the x value - defaults to this.x |
y | Number |
the y value - defaults to this.y |
Example
- let length = vector.length();
Vector2
vector2.limit(value) ⇒ Set or limit the magnitude to a value
Returns: Vector2
- - the modified vector (this)
Param | Type | Description |
---|---|---|
value | Number |
Value to limit the magnitude too |
Example
- vector.limit(5);
Number
vector2.mag() ⇒ Calculate the magnitude or length of the vector (shorthand function)
Returns: Number
- - the length or magnitude of this vector
Example
- let mag = vector.mag();
Number
vector2.magnitude() ⇒ Calculate the magnitude or length of the vector
Returns: Number
- - the length or magnitude of this vector
Example
- let mag = vector.magnitude();
vector2.magSq() ⇒
Calculate the magnitude squared
Returns: - Vector2 magnitude squared
Example
= let magSq = vector.magSq();
Vector2
vector2.midpoint(vector) ⇒ Get the midpoint between this Vector2 to another Vector2
Returns: Vector2
- - {x: x, y: y} midpoint values
Param | Type | Description |
---|---|---|
vector | Vector2 |
{x: 0, y: 0} |
Example
- let midpoint = vector1.midpoint(vector2);
Vector2
vector2.multiply(vector) ⇒ Multiply a Vector2 from this
Returns: Vector2
- - The modified Vector (this)
Param | Type | Description |
---|---|---|
vector | Vector2 |
Vector2 to multiply from this |
Example
- vector1.multiply(vector2);
Vector2
vector2.multiplyScalar(scalar) ⇒ Multiply a scalar value to our vector
Returns: Vector2
- - The modified Vector (this)
Param | Type | Description |
---|---|---|
scalar | Number |
scalar number to multiply to both x and y |
Example
- vector.multiplyScalar(5);
vector2.normalize() ⇒
The normalized vector or keep the direction of the Vector2 the same but the length is 1
Returns: normalized value
Example
- vector.noramlize();
vector2.set(props)
Set the properties on this to be the properties of a passed in Object
Param | Type | Description |
---|---|---|
props | Object |
object with properties to set on this |
Example
- {x: 50, y: 100}
Vector2
vector2.setFromPolar(length, angle) ⇒ Set our X and Y Cartesian Coordiantes based off the provided Polar Coordinates
Returns: Vector2
- - The modified Vector (this)
Param | Type | Description |
---|---|---|
length | Number |
the length or radius of the Polar Coordinates |
angle | Number |
the angle or theta of the Polar Coordinates |
Example
- vector.setFromPolar(5, Math.PI() / 2);
Vector2
vector2.sub(vector) ⇒ Subtract a Vector2 from this
Returns: Vector2
- - The modified Vector (this)
Param | Type | Description |
---|---|---|
vector | Vector2 |
Vector2 to subtract from this |
Example
- vector1.sub(vector2);
Vector2
vector2.subScalar(scalar) ⇒ Subtract a scalar value to our vector
Returns: Vector2
- - The modified Vector (this)
Param | Type | Description |
---|---|---|
scalar | Number |
number to subtract to both x and y |
Example
- vector.subScalar(5);
Vector2
vector2.subVectors(vector1, vector2) ⇒ Set this x and y vector to be the subtraction of two Vector2(s)
Returns: Vector2
- - The modified Vector (this)
Param | Type | Description |
---|---|---|
vector1 | Vector2 |
first vector to subtract to this |
vector2 | Vector2 |
second vector to subtract to this |
Example
- let vector = new Vector2().subVectors(vector1, vector2);