API Reference

Class List

pc.Vec3

A 3-dimensional vector.

var v = new pc.Vec3(1,2,3);

Summary

Static Properties

BACKA constant vector set to [0, 0, 1].[read only]
DOWNA constant vector set to [0, -1, 0].[read only]
FORWARDA constant vector set to [0, 0, -1].[read only]
LEFTA constant vector set to [-1, 0, 0].[read only]
ONEA constant vector set to [1, 1, 1].[read only]
RIGHTA constant vector set to [1, 0, 0].[read only]
UPA constant vector set to [0, 1, 0].[read only]
ZEROA constant vector set to [0, 0, 0].[read only]

Properties

xThe first component of the vector.
yThe second component of the vector.
zThe third component of the vector.

Methods

addAdds a 3-dimensional vector to another in place.
add2Adds two 3-dimensional vectors together and returns the result.
cloneReturns an identical copy of the specified 3-dimensional vector.
copyCopied the contents of a source 3-dimensional vector to a destination 3-dimensional vector.
crossReturns the result of a cross product operation performed on the two specified 3-dimensional vectors.
dotReturns the result of a dot product operation performed on the two specified 3-dimensional vectors.
equalsReports whether two vectors are equal.
lengthReturns the magnitude of the specified 3-dimensional vector.
lengthSqReturns the magnitude squared of the specified 3-dimensional vector.
lerpReturns the result of a linear interpolation between two specified 3-dimensional vectors.
mulReturns the result of multiplying the specified 3-dimensional vectors together.
mul2Returns the result of multiplying the specified 3-dimensional vectors together.
normalizeReturns the specified 3-dimensional vector copied and converted to a unit vector.
projectProjects this 3-dimensional vector onto the specified vector.
scaleScales each dimension of the specified 3-dimensional vector by the supplied scalar value.
setSets the specified 3-dimensional vector to the supplied numerical values.
subSubtracts a 3-dimensional vector from another in place.
sub2Subtracts two 3-dimensional vectors from one another and returns the result.
toStringConverts the vector to string form.

Details

Static Properties

BACKA constant vector set to [0, 0, 1].[read only]
DOWNA constant vector set to [0, -1, 0].[read only]
FORWARDA constant vector set to [0, 0, -1].[read only]
LEFTA constant vector set to [-1, 0, 0].[read only]
ONEA constant vector set to [1, 1, 1].[read only]
A constant vector set to [1, 0, 0].[read only]
UPA constant vector set to [0, 1, 0].[read only]
ZEROA constant vector set to [0, 0, 0].[read only]

Constructor

Vec3([x], [y], [z])

Creates a new Vec3 object

var v = new pc.Vec3(1,2,3);

Parameters

xNumberThe x value. If x is an array of length 3, the array will be used to populate all components.
yNumberThe y value
zNumberThe z value

Properties

Numberx

The first component of the vector.

var vec = new pc.Vec3(10, 20, 30);

// Get x
var x = vec.x;

// Set x
vec.x = 0;
Numbery

The second component of the vector.

var vec = new pc.Vec3(10, 20, 30);

// Get y
var y = vec.y;

// Set y
vec.y = 0;
Numberz

The third component of the vector.

var vec = new pc.Vec3(10, 20, 30);

// Get z
var z = vec.z;

// Set z
vec.z = 0;

Methods

add(rhs)

Adds a 3-dimensional vector to another in place.

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());

Parameters

rhspc.Vec3The vector to add to the specified vector.

Returns

pc.Vec3 Self for chaining.

add2(lhs, rhs)

Adds two 3-dimensional vectors together and returns the result.

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());

Parameters

lhspc.Vec3The first vector operand for the addition.
rhspc.Vec3The second vector operand for the addition.

Returns

pc.Vec3 Self for chaining.

clone()

Returns an identical copy of the specified 3-dimensional vector.

var v = new pc.Vec3(10, 20, 30);
var vclone = v.clone();
console.log("The result of the cloning is: " + vclone.toString());

Returns

pc.Vec3 A 3-dimensional vector containing the result of the cloning.

copy(rhs)

Copied the contents of a source 3-dimensional vector to a destination 3-dimensional vector.

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"));

Parameters

rhspc.Vec3A vector to copy to the specified vector.

Returns

pc.Vec3 Self for chaining.

cross(lhs, rhs)

Returns the result of a cross product operation performed on the two specified 3-dimensional vectors.

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());

Parameters

lhspc.Vec3The first 3-dimensional vector operand of the cross product.
rhspc.Vec3The second 3-dimensional vector operand of the cross product.

Returns

pc.Vec3 Self for chaining.

dot(rhs)

Returns the result of a dot product operation performed on the two specified 3-dimensional vectors.

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);

Parameters

rhspc.Vec3The second 3-dimensional vector operand of the dot product.

Returns

Number The result of the dot product operation.

equals(rhs)

Reports whether two vectors are equal.

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"));

Parameters

rhspc.Vec3The vector to compare to the specified vector.

Returns

Boolean true if the vectors are equal and false otherwise.

length()

Returns the magnitude of the specified 3-dimensional vector.

var vec = new pc.Vec3(3, 4, 0);
var len = vec.length();
// Should output 5
console.log("The length of the vector is: " + len);

Returns

Number The magnitude of the specified 3-dimensional vector.

lengthSq()

Returns the magnitude squared of the specified 3-dimensional vector.

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);

Returns

Number The magnitude of the specified 3-dimensional vector.

lerp(lhs, rhs, alpha)

Returns the result of a linear interpolation between two specified 3-dimensional vectors.

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

Parameters

lhspc.Vec3The 3-dimensional to interpolate from.
rhspc.Vec3The 3-dimensional to interpolate to.
alphaNumberThe 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

pc.Vec3 Self for chaining.

mul(rhs)

Returns the result of multiplying the specified 3-dimensional vectors together.

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());

Parameters

rhspc.Vec3The 3-dimensional vector used as the second multiplicand of the operation.

Returns

pc.Vec3 Self for chaining.

mul2(lhs, rhs)

Returns the result of multiplying the specified 3-dimensional vectors together.

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());

Parameters

lhspc.Vec3The 3-dimensional vector used as the first multiplicand of the operation.
rhspc.Vec3The 3-dimensional vector used as the second multiplicand of the operation.

Returns

pc.Vec3 Self for chaining.

normalize()

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.

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());

Returns

pc.Vec3 The result of the normalization.

project(rhs)

Projects this 3-dimensional vector onto the specified vector.

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());

Parameters

rhspc.Vec3The vector onto which the original vector will be projected on.

Returns

pc.Vec3 Self for chaining.

scale(scalar)

Scales each dimension of the specified 3-dimensional vector by the supplied scalar value.

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);

Parameters

scalarNumberThe value by which each vector component is multiplied.

Returns

pc.Vec3 Self for chaining.

set(x, y, z)

Sets the specified 3-dimensional vector to the supplied numerical values.

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());

Parameters

xNumberThe value to set on the first component of the vector.
yNumberThe value to set on the second component of the vector.
zNumberThe value to set on the third component of the vector.

sub(rhs)

Subtracts a 3-dimensional vector from another in place.

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());

Parameters

rhspc.Vec3The vector to add to the specified vector.

Returns

pc.Vec3 Self for chaining.

sub2(lhs, rhs)

Subtracts two 3-dimensional vectors from one another and returns the result.

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());

Parameters

lhspc.Vec3The first vector operand for the addition.
rhspc.Vec3The second vector operand for the addition.

Returns

pc.Vec3 Self for chaining.

toString()

Converts the vector to string form.

var v = new pc.Vec3(20, 10, 5);
// Should output '[20, 10, 5]'
console.log(v.toString());

Returns

String The vector in string form.