API Reference

Class List

Vec2

A 2-dimensional vector.

const 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 component of the vector.

y

The second component 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.

addScalar

Adds a number to each element of a vector.

addScaled

Adds a 2-dimensional vector scaled by scalar value.

angle

Returns the angle in degrees of the specified 2-dimensional vector.

angleTo

Returns the shortest Euler angle between two 2-dimensional vectors.

ceil

Each element is rounded up to the next largest integer.

clone

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

copy

Copies 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.

div

Divides a 2-dimensional vector by another in place.

div2

Divides one 2-dimensional vector by another and writes the result to the specified vector.

divScalar

Divides each element of a vector by a number.

dot

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

equals

Reports whether two vectors are equal.

equalsApprox

Reports whether two vectors are equal using an absolute error tolerance.

floor

Each element is set to the largest integer less than or equal to its value.

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.

max

Each element is assigned a value from rhs parameter if it is larger.

min

Each element is assigned a value from rhs parameter if it is smaller.

mul

Multiplies a 2-dimensional vector to another in place.

mul2

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

mulScalar

Multiplies each element of a vector by a number.

normalize

Returns this 2-dimensional vector converted to a unit vector in place.

rotate

Rotate a vector by an angle in degrees.

round

Each element is rounded up or down to the nearest integer.

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.

subScalar

Subtracts a number from each element of a vector.

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]

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

Create a new Vec2 instance.

const v = new pc.Vec2(1, 2);

Parameters

xnumber, number[]

The x value. Defaults to 0. If x is an array of length 2, the array will be used to populate all components.

ynumber

The y value. Defaults to 0.

Properties

numberx

The first component of the vector.

numbery

The second component of the vector.

Methods

add(rhs)

Adds a 2-dimensional vector to another in place.

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

Parameters

rhsVec2

The vector to add to the specified vector.

Returns

Vec2

Self for chaining.

add2(lhs, rhs)

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

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

Parameters

lhsVec2

The first vector operand for the addition.

rhsVec2

The second vector operand for the addition.

Returns

Vec2

Self for chaining.

addScalar(scalar)

Adds a number to each element of a vector.

const vec = new pc.Vec2(3, 4);

vec.addScalar(2);

// Outputs [5, 6]
console.log("The result of the addition is: " + vec.toString());

Parameters

scalarnumber

The number to add.

Returns

Vec2

Self for chaining.

addScaled(rhs, scalar)

Adds a 2-dimensional vector scaled by scalar value. Does not modify the vector being added.

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

Parameters

rhsVec2

The vector to add to the specified vector.

scalarnumber

The number to multiply the added vector with.

Returns

Vec2

Self for chaining.

angle()

Returns the angle in degrees of the specified 2-dimensional vector.

const v = new pc.Vec2(6, 0);
const angle = v.angle();
// Outputs 90..
console.log("The angle of the vector is: " + angle);

Returns

number

The angle in degrees of the specified 2-dimensional vector.

angleTo(rhs)

Returns the shortest Euler angle 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);

Parameters

rhsVec2

The 2-dimensional vector to calculate angle to.

Returns

number

The shortest angle in degrees between two 2-dimensional vectors.

ceil([src])

Each element is rounded up to the next largest integer.

Parameters

srcVec2

The vector to ceil. If not set, the operation is done in place.

Returns

Vec2

Self for chaining.

clone()

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

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

Returns

this

A 2-dimensional vector containing the result of the cloning.

copy(rhs)

Copies the contents of a source 2-dimensional vector to a destination 2-dimensional vector.

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

Parameters

rhsVec2

A vector to copy to the specified vector.

Returns

Vec2

Self for chaining.

cross(rhs)

Returns the result of a cross product operation performed on the two specified 2-dimensional 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);

Parameters

rhsVec2

The second 2-dimensional vector operand of the cross product.

Returns

number

The cross product of the two vectors.

distance(rhs)

Returns the distance between the two specified 2-dimensional 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);

Parameters

rhsVec2

The second 2-dimensional vector to test.

Returns

number

The distance between the two vectors.

div(rhs)

Divides a 2-dimensional vector by another in place.

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

Parameters

rhsVec2

The vector to divide the specified vector by.

Returns

Vec2

Self for chaining.

div2(lhs, rhs)

Divides one 2-dimensional vector by another and writes the result to the specified vector.

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

Parameters

lhsVec2

The dividend vector (the vector being divided).

rhsVec2

The divisor vector (the vector dividing the dividend).

Returns

Vec2

Self for chaining.

divScalar(scalar)

Divides each element of a vector by a number.

const vec = new pc.Vec2(3, 6);

vec.divScalar(3);

// Outputs [1, 2]
console.log("The result of the division is: " + vec.toString());

Parameters

scalarnumber

The number to divide by.

Returns

Vec2

Self for chaining.

dot(rhs)

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

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

Parameters

rhsVec2

The second 2-dimensional vector operand of the dot product.

Returns

number

The result of the dot product operation.

equals(rhs)

Reports whether two vectors are equal.

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

Parameters

rhsVec2

The vector to compare to the specified vector.

Returns

boolean

True if the vectors are equal and false otherwise.

equalsApprox(rhs, [epsilon])

Reports whether two vectors are equal using an absolute error tolerance.

const a = new pc.Vec2();
const b = new pc.Vec2();
console.log("The two vectors are approximately " + (a.equalsApprox(b, 1e-9) ? "equal" : "different"));

Parameters

rhsVec2

The vector to be compared against.

epsilonnumber

The maximum difference between each component of the two vectors. Defaults to 1e-6.

Returns

boolean

True if the vectors are equal and false otherwise.

floor([src])

Each element is set to the largest integer less than or equal to its value.

Parameters

srcVec2

The vector to floor. If not set, the operation is done in place.

Returns

Vec2

Self for chaining.

length()

Returns the magnitude of the specified 2-dimensional vector.

const vec = new pc.Vec2(3, 4);
const len = vec.length();
// Outputs 5
console.log("The length of the vector is: " + len);

Returns

number

The magnitude of the specified 2-dimensional vector.

lengthSq()

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

const vec = new pc.Vec2(3, 4);
const len = vec.lengthSq();
// Outputs 25
console.log("The length squared of the vector is: " + len);

Returns

number

The magnitude of the specified 2-dimensional vector.

lerp(lhs, rhs, alpha)

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

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

Parameters

lhsVec2

The 2-dimensional to interpolate from.

rhsVec2

The 2-dimensional to interpolate to.

alphanumber

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

Vec2

Self for chaining.

max(rhs)

Each element is assigned a value from rhs parameter if it is larger.

Parameters

rhsVec2

The 2-dimensional vector used as the source of elements to compare to.

Returns

Vec2

Self for chaining.

min(rhs)

Each element is assigned a value from rhs parameter if it is smaller.

Parameters

rhsVec2

The 2-dimensional vector used as the source of elements to compare to.

Returns

Vec2

Self for chaining.

mul(rhs)

Multiplies a 2-dimensional vector to another in place.

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

Parameters

rhsVec2

The 2-dimensional vector used as the second multiplicand of the operation.

Returns

Vec2

Self for chaining.

mul2(lhs, rhs)

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

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

Parameters

lhsVec2

The 2-dimensional vector used as the first multiplicand of the operation.

rhsVec2

The 2-dimensional vector used as the second multiplicand of the operation.

Returns

Vec2

Self for chaining.

mulScalar(scalar)

Multiplies each element of a vector by a number.

const vec = new pc.Vec2(3, 6);

vec.mulScalar(3);

// Outputs [9, 18]
console.log("The result of the multiplication is: " + vec.toString());

Parameters

scalarnumber

The number to multiply by.

Returns

Vec2

Self for chaining.

normalize([src])

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.

const v = new pc.Vec2(25, 0);

v.normalize();

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

Parameters

srcVec2

The vector to normalize. If not set, the operation is done in place.

Returns

Vec2

Self for chaining.

rotate(degrees)

Rotate a vector by an angle in degrees.

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

Parameters

degreesnumber

The number to degrees to rotate the vector by.

Returns

Vec2

Self for chaining.

round([src])

Each element is rounded up or down to the nearest integer.

Parameters

srcVec2

The vector to round. If not set, the operation is done in place.

Returns

Vec2

Self for chaining.

set(x, y)

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

const v = new pc.Vec2();
v.set(5, 10);

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

Parameters

xnumber

The value to set on the first component of the vector.

ynumber

The value to set on the second component of the vector.

Returns

Vec2

Self for chaining.

sub(rhs)

Subtracts a 2-dimensional vector from another in place.

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

Parameters

rhsVec2

The vector to subtract from the specified vector.

Returns

Vec2

Self for chaining.

sub2(lhs, rhs)

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

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

Parameters

lhsVec2

The first vector operand for the subtraction.

rhsVec2

The second vector operand for the subtraction.

Returns

Vec2

Self for chaining.

subScalar(scalar)

Subtracts a number from each element of a vector.

const vec = new pc.Vec2(3, 4);

vec.subScalar(2);

// Outputs [1, 2]
console.log("The result of the subtraction is: " + vec.toString());

Parameters

scalarnumber

The number to subtract.

Returns

Vec2

Self for chaining.

toString()

Converts the vector to string form.

const v = new pc.Vec2(20, 10);
// Outputs [20, 10]
console.log(v.toString());

Returns

string

The vector in string form.