Math3D. Vector3

3D Vector object, right handed with Y up

  • Methods return new vectors except set and setFrom

Constructor

new Vector3(xopt, yopt, zopt)

Create a 3D vector

Parameters:
NameTypeAttributesDefaultDescription
xnumber<optional>
0
ynumber<optional>
0
znumber<optional>
0
Example
const a = vec3(1, 2, 3);
const b = a.add(vec3(0, 1, 0)).normalize();

Members

x

Properties
TypeDescription
number

X axis location

y

Properties
TypeDescription
number

Y axis location

z

Properties
TypeDescription
number

Z axis location

Methods

abs() → {Vector3}

Returns a new vector with the absolute value of each component

Returns:
Type: 
Vector3

add(v) → {Vector3}

Returns a copy of this vector plus the vector passed in

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Vector3

clampLength(lengthopt) → {Vector3}

Returns a new vector clamped to the length passed in

Parameters:
NameTypeAttributesDefaultDescription
lengthnumber<optional>
1
Returns:
Type: 
Vector3

copy() → {Vector3}

Returns a new vector that is a copy of this

Returns:
Type: 
Vector3

cross(v) → {Vector3}

Returns a vector at right angles to both this and the one passed in

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Vector3

distance(v) → {number}

Returns the distance from this vector to the vector passed in

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
number

distanceSquared(v) → {number}

Returns the distance squared from this vector to the vector passed in

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
number

divide(v) → {Vector3}

Returns a copy of this vector divided by the vector passed in

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Vector3

dot(v) → {number}

Returns the dot product of this vector and the vector passed in

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
number

floor() → {Vector3}

Returns a new vector with each component floored

Returns:
Type: 
Vector3

isValid() → {boolean}

Checks if this is a valid vector

Returns:
Type: 
boolean

length() → {number}

Returns the length of this vector

Returns:
Type: 
number

lengthSquared() → {number}

Returns the length of this vector squared

Returns:
Type: 
number

lerp(v, percent) → {Vector3}

Returns a new vector interpolated between this and the vector passed in, percent is clamped to 0-1

Parameters:
NameTypeDescription
vVector3
percentnumber
Returns:
Type: 
Vector3

multiply(v) → {Vector3}

Returns a copy of this vector times the vector passed in

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Vector3

normalize(lengthopt) → {Vector3}

Returns a new vector in the same direction with the length passed in, zero stays zero

Parameters:
NameTypeAttributesDefaultDescription
lengthnumber<optional>
1
Returns:
Type: 
Vector3

reflect(normal, restitutionopt) → {Vector3}

Returns a copy of this vector reflected by a surface normal

Parameters:
NameTypeAttributesDefaultDescription
normalVector3

Surface normal, should be normalized

restitutionnumber<optional>
1

How much to bounce, 1 is a perfect bounce, 0 slides along the surface

Returns:
Type: 
Vector3

rotate(axis, angle) → {Vector3}

Returns a new vector turned around an axis, counter clockwise when the axis points at you

Parameters:
NameTypeDescription
axisVector3

Unit length

anglenumber

Radians

Returns:
Type: 
Vector3

rotateX(angle) → {Vector3}

Returns a new vector turned around the X axis, the way a positive pitch in rotation3D turns things

Parameters:
NameTypeDescription
anglenumber

Radians

Returns:
Type: 
Vector3

rotateY(angle) → {Vector3}

Returns a new vector turned around the Y axis, the way a positive yaw in rotation3D turns things

Parameters:
NameTypeDescription
anglenumber

Radians

Returns:
Type: 
Vector3

rotateZ(angle) → {Vector3}

Returns a new vector turned around the Z axis, the way a positive roll in rotation3D turns things

Parameters:
NameTypeDescription
anglenumber

Radians

Returns:
Type: 
Vector3

round() → {Vector3}

Returns a new vector with each component rounded

Returns:
Type: 
Vector3

scale(s) → {Vector3}

Returns a copy of this vector scaled by the number passed in

Parameters:
NameTypeDescription
snumber
Returns:
Type: 
Vector3

set(xopt, yopt, zopt) → {Vector3}

Sets values of this vector and returns self

Parameters:
NameTypeAttributesDefaultDescription
xnumber<optional>
0
ynumber<optional>
0
znumber<optional>
0
Returns:
Type: 
Vector3

setFrom(v) → {Vector3}

Copies the values of another vector into this one and returns self

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Vector3

snap(grid) → {Vector3}

Returns a new vector snapped down to a grid, grid is the number of steps per unit like Vector2.snap

Parameters:
NameTypeDescription
gridnumber

Snap steps per unit, 2 snaps to halves

Returns:
Type: 
Vector3

subtract(v) → {Vector3}

Returns a copy of this vector minus the vector passed in

Parameters:
NameTypeDescription
vVector3
Returns:
Type: 
Vector3

toString(digitsopt) → {string}

Returns a string representation of this vector for debugging

Parameters:
NameTypeAttributesDefaultDescription
digitsnumber<optional>
3

Number of digits to display

Returns:
Type: 
string

transform(matrix) → {Vector3}

Returns this point transformed by a matrix, translation included

Parameters:
NameTypeDescription
matrixMatrix4
Returns:
Type: 
Vector3

transformDirection(matrix) → {Vector3}

Returns this direction transformed by a matrix, rotation and scale only

Parameters:
NameTypeDescription
matrixMatrix4
Returns:
Type: 
Vector3