Vector
A 2d vector with an x and a y, for positions, velocities and directions.
Every method changes the vector in place and returns nothing, except clone(),
which returns a new vector, and equal(), which returns a boolean. Call
clone() first when you need to keep the original.
Constructor
new THRUNIUM.Vector(x, y)
- x number
The x component.
- y number
The y component.
Creates a vector. Must be called with new.
var vector = new THRUNIUM.Vector(10, 20);General
x: number
The x component. Read and write it directly.
var vector = new THRUNIUM.Vector(10, 20);
vector.x = 15;
console.log(vector.x); // 15y: number
The y component. Read and write it directly.
var vector = new THRUNIUM.Vector(10, 20);
vector.y = 25;
console.log(vector.y); // 25clone()
Returns a new vector with the same x and y. Changing the copy does not change the original.
var vector = new THRUNIUM.Vector(10, 20);
var copy = vector.clone();
copy.scale(2);
// vector is still (10, 20), copy is (20, 40)equal(vector)
- vector Vector
The vector to compare with.
returns boolean
Returns true if both components equal those of vector. The comparison is loose (==), as in 1.0.0.
var a = new THRUNIUM.Vector(10, 20);
var b = new THRUNIUM.Vector(10, 20);
a.equal(b); // truescale(scale)
- scale number
The factor to multiply by.
Multiplies both components by scale.
var vector = new THRUNIUM.Vector(10, 20);
vector.scale(2);
// vector is now (20, 40)scaleX(scale)
- scale number
The factor to multiply by.
Multiplies the x component by scale.
var vector = new THRUNIUM.Vector(10, 20);
vector.scaleX(2);
// vector is now (20, 20)scaleY(scale)
- scale number
The factor to multiply by.
Multiplies the y component by scale.
var vector = new THRUNIUM.Vector(10, 20);
vector.scaleY(2);
// vector is now (10, 40)add(vector)
- vector Vector
The vector to add.
Adds vector to this vector.
var vector = new THRUNIUM.Vector(10, 20);
vector.add(new THRUNIUM.Vector(5, 2));
// vector is now (15, 22)addX(vector)
- vector Vector
The vector whose x component is added.
Adds the x component of vector to this vector's x component.
var vector = new THRUNIUM.Vector(10, 20);
vector.addX(new THRUNIUM.Vector(5, 2));
// vector is now (15, 20)addY(vector)
- vector Vector
The vector whose y component is added.
Adds the y component of vector to this vector's y component.
var vector = new THRUNIUM.Vector(10, 20);
vector.addY(new THRUNIUM.Vector(5, 2));
// vector is now (10, 22)subtract(vector)
- vector Vector
The vector to subtract.
Subtracts vector from this vector.
var vector = new THRUNIUM.Vector(10, 20);
vector.subtract(new THRUNIUM.Vector(2, 10));
// vector is now (8, 10)subtractX(vector)
- vector Vector
The vector whose x component is subtracted.
Subtracts the x component of vector from this vector's x component.
var vector = new THRUNIUM.Vector(10, 20);
vector.subtractX(new THRUNIUM.Vector(2, 10));
// vector is now (8, 20)subtractY(vector)
- vector Vector
The vector whose y component is subtracted.
Subtracts the y component of vector from this vector's y component.
var vector = new THRUNIUM.Vector(10, 20);
vector.subtractY(new THRUNIUM.Vector(2, 10));
// vector is now (10, 10)divide(vector)
- vector Vector
The vector to divide by.
Divides each component by the matching component of vector.
var vector = new THRUNIUM.Vector(10, 20);
vector.divide(new THRUNIUM.Vector(2, 4));
// vector is now (5, 5)divideX(vector)
- vector Vector
The vector whose x component is the divisor.
Divides the x component by the x component of vector.
var vector = new THRUNIUM.Vector(10, 20);
vector.divideX(new THRUNIUM.Vector(2, 4));
// vector is now (5, 20)divideY(vector)
- vector Vector
The vector whose y component is the divisor.
Divides the y component by the y component of vector.
var vector = new THRUNIUM.Vector(10, 20);
vector.divideY(new THRUNIUM.Vector(2, 4));
// vector is now (10, 5)multiply(vector)
- vector Vector
The vector to multiply by.
Multiplies each component by the matching component of vector.
var vector = new THRUNIUM.Vector(10, 20);
vector.multiply(new THRUNIUM.Vector(4, 2));
// vector is now (40, 40)multiplyX(vector)
- vector Vector
The vector whose x component is the factor.
Multiplies the x component by the x component of vector.
var vector = new THRUNIUM.Vector(10, 20);
vector.multiplyX(new THRUNIUM.Vector(4, 2));
// vector is now (40, 20)multiplyY(vector)
- vector Vector
The vector whose y component is the factor.
Multiplies the y component by the y component of vector.
var vector = new THRUNIUM.Vector(10, 20);
vector.multiplyY(new THRUNIUM.Vector(4, 2));
// vector is now (10, 40)invert()
Flips the sign of both components, so the vector points the other way.
var vector = new THRUNIUM.Vector(10, 20);
vector.invert();
// vector is now (-10, -20)invertX()
Flips the sign of the x component.
var vector = new THRUNIUM.Vector(10, 20);
vector.invertX();
// vector is now (-10, 20)invertY()
Flips the sign of the y component.
var vector = new THRUNIUM.Vector(10, 20);
vector.invertY();
// vector is now (10, -20)normalize()
Scales the vector to length 1, keeping its direction. The zero vector stays (0, 0). Since 1.1.0 this returns a real unit vector; 1.0.0 computed something else.
var vector = new THRUNIUM.Vector(3, 4);
vector.normalize();
// vector is now (0.6, 0.8)limit(vector)
- vector Vector
The largest allowed size of each component.
Clamps each component to the range from -vector to +vector on that axis: x to between -vector.x and vector.x, y to between -vector.y and vector.y. Useful for capping a velocity. Pass a vector with non-negative components.
var vector = new THRUNIUM.Vector(10, 20);
vector.limit(new THRUNIUM.Vector(10, 10));
// vector is now (10, 10)limitLength(len)
- len number
Ignored.
Does nothing. It is kept so games written for 1.0.0, where it was also empty, keep working. Use limit() to clamp a vector.
var vector = new THRUNIUM.Vector(10, 20);
vector.limitLength(5);
// vector is still (10, 20)