Class: Vec3

pc.Vec3

A 3-dimensional vector.

Constructor

new Vec3(xopt, yopt, zopt)

Creates a new Vec3 object.
Parameters:
Name Type Attributes Description
x Number <optional>
The x value. If x is an array of length 3, the array will be used to populate all components.
y Number <optional>
The y value.
z Number <optional>
The z value.
Source:
Example
var v = new pc.Vec3(1, 2, 3);

Members

x :Number

The first component of the vector.
Type:
  • Number
Source:
Example
var vec = new pc.Vec3(10, 20, 30);

// Get x
var x = vec.x;

// Set x
vec.x = 0;

y :Number

The second component of the vector.
Type:
  • Number
Source:
Example
var vec = new pc.Vec3(10, 20, 30);

// Get y
var y = vec.y;

// Set y
vec.y = 0;

z :Number

The third component of the vector.
Type:
  • Number
Source:
Example
var vec = new pc.Vec3(10, 20, 30);

// Get z
var z = vec.z;

// Set z
vec.z = 0;

(static, readonly) BACK :pc.Vec3

A constant vector set to [0, 0, 1].
Type:
Source:

(static, readonly) DOWN :pc.Vec3

A constant vector set to [0, -1, 0].
Type:
Source:

(static, readonly) FORWARD :pc.Vec3

A constant vector set to [0, 0, -1].
Type:
Source:

(static, readonly) LEFT :pc.Vec3

A constant vector set to [-1, 0, 0].
Type:
Source:

(static, readonly) ONE :pc.Vec3

A constant vector set to [1, 1, 1].
Type:
Source:

(static, readonly) RIGHT :pc.Vec3

A constant vector set to [1, 0, 0].
Type:
Source:

(static, readonly) UP :pc.Vec3

A constant vector set to [0, 1, 0].
Type:
Source:

(static, readonly) ZERO :pc.Vec3

A constant vector set to [0, 0, 0].
Type:
Source:

Methods

add(rhs) → {pc.Vec3}

Adds a 3-dimensional vector to another in place.
Parameters:
Name Type Description
rhs pc.Vec3 The vector to add to the specified vector.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var a = new pc.Vec3(10, 10, 10);
var b = new pc.Vec3(20, 20, 20);

a.add(b);

// Should output [30, 30, 30]
console.log("The result of the addition is: " + a.toString());

add2(lhs, rhs) → {pc.Vec3}

Adds two 3-dimensional vectors together and returns the result.
Parameters:
Name Type Description
lhs pc.Vec3 The first vector operand for the addition.
rhs pc.Vec3 The second vector operand for the addition.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var a = new pc.Vec3(10, 10, 10);
var b = new pc.Vec3(20, 20, 20);
var r = new pc.Vec3();

r.add2(a, b);
// Should output [30, 30, 30]

console.log("The result of the addition is: " + r.toString());

clone() → {pc.Vec3}

Returns an identical copy of the specified 3-dimensional vector.
Source:
Returns:
A 3-dimensional vector containing the result of the cloning.
Type
pc.Vec3
Example
var v = new pc.Vec3(10, 20, 30);
var vclone = v.clone();
console.log("The result of the cloning is: " + vclone.toString());

copy(rhs) → {pc.Vec3}

Copied the contents of a source 3-dimensional vector to a destination 3-dimensional vector.
Parameters:
Name Type Description
rhs pc.Vec3 A vector to copy to the specified vector.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var src = new pc.Vec3(10, 20, 30);
var dst = new pc.Vec3();

dst.copy(src);

console.log("The two vectors are " + (dst.equals(src) ? "equal" : "different"));

cross(lhs, rhs) → {pc.Vec3}

Returns the result of a cross product operation performed on the two specified 3-dimensional vectors.
Parameters:
Name Type Description
lhs pc.Vec3 The first 3-dimensional vector operand of the cross product.
rhs pc.Vec3 The second 3-dimensional vector operand of the cross product.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var back = new pc.Vec3().cross(pc.Vec3.RIGHT, pc.Vec3.UP);

// Should print the Z axis (i.e. [0, 0, 1])
console.log("The result of the cross product is: " + back.toString());

dot(rhs) → {Number}

Returns the result of a dot product operation performed on the two specified 3-dimensional vectors.
Parameters:
Name Type Description
rhs pc.Vec3 The second 3-dimensional vector operand of the dot product.
Source:
Returns:
The result of the dot product operation.
Type
Number
Example
var v1 = new pc.Vec3(5, 10, 20);
var v2 = new pc.Vec3(10, 20, 40);
var v1dotv2 = v1.dot(v2);
console.log("The result of the dot product is: " + v1dotv2);

equals(rhs) → {Boolean}

Reports whether two vectors are equal.
Parameters:
Name Type Description
rhs pc.Vec3 The vector to compare to the specified vector.
Source:
Returns:
true if the vectors are equal and false otherwise.
Type
Boolean
Example
var a = new pc.Vec3(1, 2, 3);
var b = new pc.Vec3(4, 5, 6);
console.log("The two vectors are " + (a.equals(b) ? "equal" : "different"));

length() → {Number}

Returns the magnitude of the specified 3-dimensional vector.
Source:
Returns:
The magnitude of the specified 3-dimensional vector.
Type
Number
Example
var vec = new pc.Vec3(3, 4, 0);
var len = vec.length();
// Should output 5
console.log("The length of the vector is: " + len);

lengthSq() → {Number}

Returns the magnitude squared of the specified 3-dimensional vector.
Source:
Returns:
The magnitude of the specified 3-dimensional vector.
Type
Number
Example
var vec = new pc.Vec3(3, 4, 0);
var len = vec.lengthSq();
// Should output 25
console.log("The length squared of the vector is: " + len);

lerp(lhs, rhs, alpha) → {pc.Vec3}

Returns the result of a linear interpolation between two specified 3-dimensional vectors.
Parameters:
Name Type Description
lhs pc.Vec3 The 3-dimensional to interpolate from.
rhs pc.Vec3 The 3-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.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var a = new pc.Vec3(0, 0, 0);
var b = new pc.Vec3(10, 10, 10);
var r = new pc.Vec3();

r.lerp(a, b, 0);   // r is equal to a
r.lerp(a, b, 0.5); // r is 5, 5, 5
r.lerp(a, b, 1);   // r is equal to b

mul(rhs) → {pc.Vec3}

Multiplies a 3-dimensional vector to another in place.
Parameters:
Name Type Description
rhs pc.Vec3 The 3-dimensional vector used as the second multiplicand of the operation.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var a = new pc.Vec3(2, 3, 4);
var b = new pc.Vec3(4, 5, 6);

a.mul(b);

// Should output 8, 15, 24
console.log("The result of the multiplication is: " + a.toString());

mul2(lhs, rhs) → {pc.Vec3}

Returns the result of multiplying the specified 3-dimensional vectors together.
Parameters:
Name Type Description
lhs pc.Vec3 The 3-dimensional vector used as the first multiplicand of the operation.
rhs pc.Vec3 The 3-dimensional vector used as the second multiplicand of the operation.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var a = new pc.Vec3(2, 3, 4);
var b = new pc.Vec3(4, 5, 6);
var r = new pc.Vec3();

r.mul2(a, b);

// Should output 8, 15, 24
console.log("The result of the multiplication is: " + r.toString());

normalize() → {pc.Vec3}

Returns the specified 3-dimensional vector copied and converted to a unit vector. If the vector has a length of zero, the vector's elements will be set to zero.
Source:
Returns:
The result of the normalization.
Type
pc.Vec3
Example
var v = new pc.Vec3(25, 0, 0);

v.normalize();

// Should output 1, 0, 0, 0
console.log("The result of the vector normalization is: " + v.toString());

project(rhs) → {pc.Vec3}

Projects this 3-dimensional vector onto the specified vector.
Parameters:
Name Type Description
rhs pc.Vec3 The vector onto which the original vector will be projected on.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var v = new pc.Vec3(5, 5, 5);
var normal = new pc.Vec3(1, 0, 0);

v.project(normal);

// Should output 5, 0, 0
console.log("The result of the vector projection is: " + v.toString());

scale(scalar) → {pc.Vec3}

Scales each dimension of the specified 3-dimensional vector by the supplied scalar value.
Parameters:
Name Type Description
scalar Number The value by which each vector component is multiplied.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var v = new pc.Vec3(2, 4, 8);

// Multiply by 2
v.scale(2);

// Negate
v.scale(-1);

// Divide by 2
v.scale(0.5);

set(x, y, z) → {pc.Vec3}

Sets the specified 3-dimensional vector to the supplied numerical values.
Parameters:
Name Type Description
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.
z Number The value to set on the third component of the vector.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var v = new pc.Vec3();
v.set(5, 10, 20);

// Should output 5, 10, 20
console.log("The result of the vector set is: " + v.toString());

sub(rhs) → {pc.Vec3}

Subtracts a 3-dimensional vector from another in place.
Parameters:
Name Type Description
rhs pc.Vec3 The vector to add to the specified vector.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var a = new pc.Vec3(10, 10, 10);
var b = new pc.Vec3(20, 20, 20);

a.sub(b);

// Should output [-10, -10, -10]
console.log("The result of the addition is: " + a.toString());

sub2(lhs, rhs) → {pc.Vec3}

Subtracts two 3-dimensional vectors from one another and returns the result.
Parameters:
Name Type Description
lhs pc.Vec3 The first vector operand for the addition.
rhs pc.Vec3 The second vector operand for the addition.
Source:
Returns:
Self for chaining.
Type
pc.Vec3
Example
var a = new pc.Vec3(10, 10, 10);
var b = new pc.Vec3(20, 20, 20);
var r = new pc.Vec3();

r.sub2(a, b);

// Should output [-10, -10, -10]
console.log("The result of the addition is: " + r.toString());

toString() → {String}

Converts the vector to string form.
Source:
Returns:
The vector in string form.
Type
String
Example
var v = new pc.Vec3(20, 10, 5);
// Should output '[20, 10, 5]'
console.log(v.toString());