Vec2
A 2-dimensional vector.
var v = new pc.Vec2(1, 2);
Summary
Static Properties
DOWN | A constant vector set to [0, -1].[read only] |
LEFT | A constant vector set to [-1, 0].[read only] |
ONE | A constant vector set to [1, 1].[read only] |
RIGHT | A constant vector set to [1, 0].[read only] |
UP | A constant vector set to [0, 1].[read only] |
ZERO | A constant vector set to [0, 0].[read only] |
Properties
x | The first element of the vector. |
y | The second element of the vector. |
Methods
add | Adds a 2-dimensional vector to another in place. |
add2 | Adds two 2-dimensional vectors together and returns the result. |
clone | Returns an identical copy of the specified 2-dimensional vector. |
copy | Copied the contents of a source 2-dimensional vector to a destination 2-dimensional vector. |
cross | Returns the result of a cross product operation performed on the two specified 2-dimensional vectors. |
distance | Returns the distance between the two specified 2-dimensional vectors. |
dot | Returns the result of a dot product operation performed on the two specified 2-dimensional vectors. |
equals | Reports whether two vectors are equal. |
length | Returns the magnitude of the specified 2-dimensional vector. |
lengthSq | Returns the magnitude squared of the specified 2-dimensional vector. |
lerp | Returns the result of a linear interpolation between two specified 2-dimensional vectors. |
mul | Multiplies a 2-dimensional vector to another in place. |
mul2 | Returns the result of multiplying the specified 2-dimensional vectors together. |
normalize | Returns this 2-dimensional vector converted to a unit vector in place. |
scale | Scales each component of the specified 2-dimensional vector by the supplied scalar value. |
set | Sets the specified 2-dimensional vector to the supplied numerical values. |
sub | Subtracts a 2-dimensional vector from another in place. |
sub2 | Subtracts two 2-dimensional vectors from one another and returns the result. |
toString | Converts the vector to string form. |
Details
Static Properties
DOWN | A constant vector set to [0, -1]. [read only] |
LEFT | A constant vector set to [-1, 0]. [read only] |
ONE | A constant vector set to [1, 1]. [read only] |
RIGHT | A constant vector set to [1, 0]. [read only] |
UP | A constant vector set to [0, 1]. [read only] |
ZERO | A constant vector set to [0, 0]. [read only] |
Constructor
Vec2([x], [y])
Creates a new Vec2 object.
var v = new pc.Vec2(1, 2);
Parameters
x | number, number[] | The x value. If x is an array of length 2, the array will be used to populate all components. |
y | number | The y value. |
Properties
The first element of the vector.
var vec = new pc.Vec2(10, 20);
// Get x
var x = vec.x;
// Set x
vec.x = 0;
The second element of the vector.
var vec = new pc.Vec2(10, 20);
// Get y
var y = vec.y;
// Set y
vec.y = 0;
Methods
add(rhs)
Adds a 2-dimensional vector to another in place.
var a = new pc.Vec2(10, 10);
var b = new pc.Vec2(20, 20);
a.add(b);
// Should output [30, 30]
console.log("The result of the addition is: " + a.toString());
Parameters
rhs | Vec2 | The vector to add to the specified vector. |
Returns
Vec2Self for chaining.
add2(lhs, rhs)
Adds two 2-dimensional vectors together and returns the result.
var a = new pc.Vec2(10, 10);
var b = new pc.Vec2(20, 20);
var r = new pc.Vec2();
r.add2(a, b);
// Should output [30, 30]
console.log("The result of the addition is: " + r.toString());
Parameters
lhs | Vec2 | The first vector operand for the addition. |
rhs | Vec2 | The second vector operand for the addition. |
Returns
Vec2Self for chaining.
clone()
Returns an identical copy of the specified 2-dimensional vector.
var v = new pc.Vec2(10, 20);
var vclone = v.clone();
console.log("The result of the cloning is: " + vclone.toString());
Returns
Vec2A 2-dimensional vector containing the result of the cloning.
copy(rhs)
Copied the contents of a source 2-dimensional vector to a destination 2-dimensional vector.
var src = new pc.Vec2(10, 20);
var dst = new pc.Vec2();
dst.copy(src);
console.log("The two vectors are " + (dst.equals(src) ? "equal" : "different"));
Parameters
rhs | Vec2 | A vector to copy to the specified vector. |
Returns
Vec2Self for chaining.
cross(rhs)
Returns the result of a cross product operation performed on the two specified 2-dimensional vectors.
var right = new pc.Vec2(1, 0);
var up = new pc.Vec2(0, 1);
var crossProduct = right.cross(up);
// Should print 1
console.log("The result of the cross product is: " + crossProduct);
Parameters
rhs | Vec2 | The second 2-dimensional vector operand of the cross product. |
Returns
numberThe cross product of the two vectors.
distance(rhs)
Returns the distance between the two specified 2-dimensional vectors.
var v1 = new pc.Vec2(5, 10);
var v2 = new pc.Vec2(10, 20);
var d = v1.distance(v2);
console.log("The between v1 and v2 is: " + d);
Parameters
rhs | Vec2 | The second 2-dimensional vector to test. |
Returns
numberThe distance between the two vectors.
dot(rhs)
Returns the result of a dot product operation performed on the two specified 2-dimensional vectors.
var v1 = new pc.Vec2(5, 10);
var v2 = new pc.Vec2(10, 20);
var v1dotv2 = v1.dot(v2);
console.log("The result of the dot product is: " + v1dotv2);
Parameters
rhs | Vec2 | The second 2-dimensional vector operand of the dot product. |
Returns
numberThe result of the dot product operation.
equals(rhs)
Reports whether two vectors are equal.
var a = new pc.Vec2(1, 2);
var b = new pc.Vec2(4, 5);
console.log("The two vectors are " + (a.equals(b) ? "equal" : "different"));
Parameters
rhs | Vec2 | The vector to compare to the specified vector. |
Returns
booleanTrue if the vectors are equal and false otherwise.
length()
Returns the magnitude of the specified 2-dimensional vector.
var vec = new pc.Vec2(3, 4);
var len = vec.length();
// Should output 5
console.log("The length of the vector is: " + len);
Returns
numberThe magnitude of the specified 2-dimensional vector.
lengthSq()
Returns the magnitude squared of the specified 2-dimensional vector.
var vec = new pc.Vec2(3, 4);
var len = vec.lengthSq();
// Should output 25
console.log("The length squared of the vector is: " + len);
Returns
numberThe magnitude of the specified 2-dimensional vector.
lerp(lhs, rhs, alpha)
Returns the result of a linear interpolation between two specified 2-dimensional vectors.
var a = new pc.Vec2(0, 0);
var b = new pc.Vec2(10, 10);
var 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
Parameters
lhs | Vec2 | The 2-dimensional to interpolate from. |
rhs | Vec2 | The 2-dimensional to interpolate to. |
alpha | number | 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. |
Returns
Vec2Self for chaining.
mul(rhs)
Multiplies a 2-dimensional vector to another in place.
var a = new pc.Vec2(2, 3);
var b = new pc.Vec2(4, 5);
a.mul(b);
// Should output 8, 15
console.log("The result of the multiplication is: " + a.toString());
Parameters
rhs | Vec2 | The 2-dimensional vector used as the second multiplicand of the operation. |
Returns
Vec2Self for chaining.
mul2(lhs, rhs)
Returns the result of multiplying the specified 2-dimensional vectors together.
var a = new pc.Vec2(2, 3);
var b = new pc.Vec2(4, 5);
var r = new pc.Vec2();
r.mul2(a, b);
// Should output 8, 15
console.log("The result of the multiplication is: " + r.toString());
Parameters
lhs | Vec2 | The 2-dimensional vector used as the first multiplicand of the operation. |
rhs | Vec2 | The 2-dimensional vector used as the second multiplicand of the operation. |
Returns
Vec2Self for chaining.
normalize()
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.
var v = new pc.Vec2(25, 0);
v.normalize();
// Should output 1, 0
console.log("The result of the vector normalization is: " + v.toString());
Returns
Vec2Self for chaining.
scale(scalar)
Scales each component of the specified 2-dimensional vector by the supplied scalar value.
var v = new pc.Vec2(2, 4);
// Multiply by 2
v.scale(2);
// Negate
v.scale(-1);
// Divide by 2
v.scale(0.5);
Parameters
scalar | number | The value by which each vector component is multiplied. |
Returns
Vec2Self for chaining.
set(x, y)
Sets the specified 2-dimensional vector to the supplied numerical values.
var v = new pc.Vec2();
v.set(5, 10);
// Should output 5, 10
console.log("The result of the vector set is: " + v.toString());
Parameters
x | number | The value to set on the first component of the vector. |
y | number | The value to set on the second component of the vector. |
Returns
Vec2Self for chaining.
sub(rhs)
Subtracts a 2-dimensional vector from another in place.
var a = new pc.Vec2(10, 10);
var b = new pc.Vec2(20, 20);
a.sub(b);
// Should output [-10, -10]
console.log("The result of the subtraction is: " + a.toString());
Parameters
rhs | Vec2 | The vector to add to the specified vector. |
Returns
Vec2Self for chaining.
sub2(lhs, rhs)
Subtracts two 2-dimensional vectors from one another and returns the result.
var a = new pc.Vec2(10, 10);
var b = new pc.Vec2(20, 20);
var r = new pc.Vec2();
r.sub2(a, b);
// Should output [-10, -10]
console.log("The result of the subtraction is: " + r.toString());
Parameters
lhs | Vec2 | The first vector operand for the addition. |
rhs | Vec2 | The second vector operand for the addition. |
Returns
Vec2Self for chaining.