LittleJS 1.30.0

Math3D

Vector3

3D Vector object, right handed with Y up

  • Methods return new vectors except set and setFrom

Constructor

new Vector3(x, y, z)math3d.js:75

Create a 3D vector

x number optional, default 0
y number optional, default 0
z number optional, default 0
const a = vec3(1, 2, 3);
const b = a.add(vec3(0, 1, 0)).normalize();

Members

Methods

Members

x : numbermath3d.js:85

X axis location

y : numbermath3d.js:87

Y axis location

z : numbermath3d.js:89

Z axis location

Methods

Returns a new vector with the absolute value of each component

Returns a copy of this vector plus the vector passed in

v Vector3
clampLength(length) → Vector3math3d.js:169

Returns a new vector clamped to the length passed in

length number optional, default 1

Returns a new vector that is a copy of this

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

v Vector3
distance(v) → numbermath3d.js:150

Returns the distance from this vector to the vector passed in

v Vector3
distanceSquared(v) → numbermath3d.js:155

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

v Vector3
divide(v) → Vector3math3d.js:126

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

v Vector3
dot(v) → numbermath3d.js:178

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

v Vector3

Returns a new vector with each component floored

isValid() → booleanmath3d.js:273

Checks if this is a valid vector

length() → numbermath3d.js:135

Returns the length of this vector

lengthSquared() → numbermath3d.js:139

Returns the length of this vector squared

lerp(v, percent) → Vector3math3d.js:195

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

v Vector3
percent number
multiply(v) → Vector3math3d.js:121

Returns a copy of this vector times the vector passed in

v Vector3
normalize(length) → Vector3math3d.js:160

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

length number optional, default 1
reflect(normal, restitution) → Vector3math3d.js:145

Returns a copy of this vector reflected by a surface normal

normal Vector3

Surface normal, should be normalized

restitution number optional, default 1

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

rotate(axis, angle) → Vector3math3d.js:205

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

axis Vector3

Unit length

angle number

Radians

rotateX(angle) → Vector3math3d.js:216

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

angle number

Radians

rotateY(angle) → Vector3math3d.js:225

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

angle number

Radians

rotateZ(angle) → Vector3math3d.js:234

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

angle number

Radians

Returns a new vector with each component rounded

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

s number
set(x, y, z) → Vector3math3d.js:97

Sets values of this vector and returns self

x number optional, default 0
y number optional, default 0
z number optional, default 0
setFrom(v) → Vector3math3d.js:102

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

v Vector3
snap(grid) → Vector3math3d.js:255

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

grid number

Snap steps per unit, 2 snaps to halves

subtract(v) → Vector3math3d.js:116

Returns a copy of this vector minus the vector passed in

v Vector3
toString(digits) → stringmath3d.js:278

Returns a string representation of this vector for debugging

digits number optional, default 3

Number of digits to display

transform(matrix) → Vector3math3d.js:264

Returns this point transformed by a matrix, translation included

matrix Matrix4
transformDirection(matrix) → Vector3math3d.js:269

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

matrix Matrix4