The first component of the vector.
The second component of the vector.
Static
Readonly
DOWNA constant vector set to [0, -1].
Static
Readonly
LEFTA constant vector set to [-1, 0].
Static
Readonly
ONEA constant vector set to [1, 1].
Static
Readonly
RIGHTA constant vector set to [1, 0].
Static
Readonly
UPA constant vector set to [0, 1].
Static
Readonly
ZEROA constant vector set to [0, 0].
Adds a 2-dimensional vector to another in place.
The vector to add to the specified vector.
Self for chaining.
const a = new pc.Vec2(10, 10);
const b = new pc.Vec2(20, 20);
a.add(b);
// Outputs [30, 30]
console.log("The result of the addition is: " + a.toString());
Adds two 2-dimensional vectors together and returns the result.
Self for chaining.
const a = new pc.Vec2(10, 10);
const b = new pc.Vec2(20, 20);
const r = new pc.Vec2();
r.add2(a, b);
// Outputs [30, 30]
console.log("The result of the addition is: " + r.toString());
Adds a 2-dimensional vector scaled by scalar value. Does not modify the vector being added.
The vector to add to the specified vector.
The number to multiply the added vector with.
Self for chaining.
const vec = new pc.Vec2(1, 2);
vec.addScaled(pc.Vec2.UP, 2);
// Outputs [1, 4]
console.log("The result of the addition is: " + vec.toString());
Returns the shortest Euler angle between two 2-dimensional vectors.
The 2-dimensional vector to calculate angle to.
The shortest angle in degrees between two 2-dimensional vectors.
const a = new pc.Vec2(0, 10); // up
const b = new pc.Vec2(1, -1); // down-right
const angle = a.angleTo(b);
// Outputs 135..
console.log("The angle between vectors a and b: " + angle);
Copies the contents of a source 2-dimensional vector to a destination 2-dimensional vector.
A vector to copy to the specified vector.
Self for chaining.
const src = new pc.Vec2(10, 20);
const dst = new pc.Vec2();
dst.copy(src);
console.log("The two vectors are " + (dst.equals(src) ? "equal" : "different"));
Returns the result of a cross product operation performed on the two specified 2-dimensional vectors.
The second 2-dimensional vector operand of the cross product.
The cross product of the two vectors.
const right = new pc.Vec2(1, 0);
const up = new pc.Vec2(0, 1);
const crossProduct = right.cross(up);
// Prints 1
console.log("The result of the cross product is: " + crossProduct);
Returns the distance between the two specified 2-dimensional vectors.
The second 2-dimensional vector to test.
The distance between the two vectors.
const v1 = new pc.Vec2(5, 10);
const v2 = new pc.Vec2(10, 20);
const d = v1.distance(v2);
console.log("The distance between v1 and v2 is: " + d);
Divides a 2-dimensional vector by another in place.
The vector to divide the specified vector by.
Self for chaining.
const a = new pc.Vec2(4, 9);
const b = new pc.Vec2(2, 3);
a.div(b);
// Outputs [2, 3]
console.log("The result of the division is: " + a.toString());
Divides one 2-dimensional vector by another and writes the result to the specified vector.
Self for chaining.
const a = new pc.Vec2(4, 9);
const b = new pc.Vec2(2, 3);
const r = new pc.Vec2();
r.div2(a, b);
// Outputs [2, 3]
console.log("The result of the division is: " + r.toString());
Returns the result of a dot product operation performed on the two specified 2-dimensional vectors.
The second 2-dimensional vector operand of the dot product.
The result of the dot product operation.
const v1 = new pc.Vec2(5, 10);
const v2 = new pc.Vec2(10, 20);
const v1dotv2 = v1.dot(v2);
console.log("The result of the dot product is: " + v1dotv2);
Reports whether two vectors are equal.
The vector to compare to the specified vector.
True if the vectors are equal and false otherwise.
const a = new pc.Vec2(1, 2);
const b = new pc.Vec2(4, 5);
console.log("The two vectors are " + (a.equals(b) ? "equal" : "different"));
Reports whether two vectors are equal using an absolute error tolerance.
The vector to be compared against.
Optional
epsilon: number = 1e-6The maximum difference between each component of the two vectors. Defaults to 1e-6.
True if the vectors are equal and false otherwise.
const a = new pc.Vec2();
const b = new pc.Vec2();
console.log("The two vectors are approximately " + (a.equalsApprox(b, 1e-9) ? "equal" : "different"));
Returns the result of a linear interpolation between two specified 2-dimensional vectors.
The 2-dimensional to interpolate from.
The 2-dimensional to interpolate to.
The value controlling the point of interpolation. Between 0 and 1, the linear interpolant will occur on a straight line between lhs and rhs. Outside of this range, the linear interpolant will occur on a ray extrapolated from this line.
Self for chaining.
const a = new pc.Vec2(0, 0);
const b = new pc.Vec2(10, 10);
const r = new pc.Vec2();
r.lerp(a, b, 0); // r is equal to a
r.lerp(a, b, 0.5); // r is 5, 5
r.lerp(a, b, 1); // r is equal to b
Multiplies a 2-dimensional vector to another in place.
The 2-dimensional vector used as the second multiplicand of the operation.
Self for chaining.
const a = new pc.Vec2(2, 3);
const b = new pc.Vec2(4, 5);
a.mul(b);
// Outputs 8, 15
console.log("The result of the multiplication is: " + a.toString());
Returns the result of multiplying the specified 2-dimensional vectors together.
Self for chaining.
const a = new pc.Vec2(2, 3);
const b = new pc.Vec2(4, 5);
const r = new pc.Vec2();
r.mul2(a, b);
// Outputs 8, 15
console.log("The result of the multiplication is: " + r.toString());
Returns this 2-dimensional vector converted to a unit vector in place. If the vector has a length of zero, the vector's elements will be set to zero.
Optional
src: Vec2 = ...The vector to normalize. If not set, the operation is done in place.
Self for chaining.
const v = new pc.Vec2(25, 0);
v.normalize();
// Outputs 1, 0
console.log("The result of the vector normalization is: " + v.toString());
Rotate a vector by an angle in degrees.
The number to degrees to rotate the vector by.
Self for chaining.
const v = new pc.Vec2(0, 10);
v.rotate(45); // rotates by 45 degrees
// Outputs [7.071068.., 7.071068..]
console.log("Vector after rotation is: " + v.toString());
Sets the specified 2-dimensional vector to the supplied numerical values.
The value to set on the first component of the vector.
The value to set on the second component of the vector.
Self for chaining.
const v = new pc.Vec2();
v.set(5, 10);
// Outputs 5, 10
console.log("The result of the vector set is: " + v.toString());
Subtracts a 2-dimensional vector from another in place.
The vector to subtract from the specified vector.
Self for chaining.
const a = new pc.Vec2(10, 10);
const b = new pc.Vec2(20, 20);
a.sub(b);
// Outputs [-10, -10]
console.log("The result of the subtraction is: " + a.toString());
Subtracts two 2-dimensional vectors from one another and returns the result.
Self for chaining.
const a = new pc.Vec2(10, 10);
const b = new pc.Vec2(20, 20);
const r = new pc.Vec2();
r.sub2(a, b);
// Outputs [-10, -10]
console.log("The result of the subtraction is: " + r.toString());
A 2-dimensional vector.