LittleJS 1.28.0

Engine

Vector2

2D Vector object with vector math library

  • Most functions return a new vector so they can be chained, the set functions change this one

Constructor

new Vector2(x, y)engineMath.js:703

Create a 2D vector with the x and y passed in, can also be created with vec2()

x number optional, default 0

X axis location

y number optional, default 0

Y axis location

let a = new Vector2(2, 3); // vector with coordinates (2, 3)
let b = new Vector2;       // vector with coordinates (0, 0)
let c = vec2(4, 2);        // use the vec2 function to make a Vector2
let d = a.add(b).scale(5); // operators can be chained

Members

Methods

Members

X axis location

Y axis location

Methods

Returns a copy of this vector with absolute values

Returns a copy of this vector plus the vector passed in

v Vector2

other vector

angle() → numberengineMath.js:820

Returns the clockwise angle of this vector, up is angle 0

area() → numberengineMath.js:898

Returns the area this vector covers as a rectangle

arrayCheck(arraySize) → booleanengineMath.js:915

Returns true if this vector is within the bounds of an array size passed in

arraySize Vector2
clampLength(length) → Vector2engineMath.js:795

Returns a new vector clamped to length passed in

length number optional, default 1

Returns a new vector that is a copy of this

cross(v) → numberengineMath.js:809

Returns the cross product of this and the vector passed in

v Vector2

other vector

direction() → numberengineMath.js:864

Returns the integer direction of this vector, corresponding to multiples of 90 degree rotation (0-3)

distance(v) → numberengineMath.js:775

Returns the distance from this vector to vector passed in

v Vector2

other vector

distanceSquared(v) → numberengineMath.js:780

Returns the distance squared from this vector to vector passed in

v Vector2

other vector

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

v Vector2

other vector

dot(v) → numberengineMath.js:804

Returns the dot product of this and the vector passed in

v Vector2

other vector

Returns a copy of this vector with each axis floored

isValid() → booleanengineMath.js:932

Checks if this is a valid vector

length() → numberengineMath.js:766

Returns the length of this vector

lengthSquared() → numberengineMath.js:770

Returns the length of this vector squared

lerp(v, percent) → Vector2engineMath.js:904

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

v Vector2

other vector

percent number
mod(divisor) → Vector2engineMath.js:893

Returns new vec2 with modded values

divisor number optional, default 1
multiply(v) → Vector2engineMath.js:752

Returns a copy of this vector times the vector passed in

v Vector2

other vector

normalize(length) → Vector2engineMath.js:786

Returns a new vector in same direction as this one with the length passed in

  • A zero vector has no direction, so it normalizes to straight up
length number optional, default 1
reflect(normal, restitution) → Vector2engineMath.js:815

Returns a copy this vector reflected by the surface normal

normal Vector2

surface normal (should be normalized)

restitution number optional, default 1

how much to bounce, 1 is perfect bounce, 0 is no bounce

rotate(angle) → Vector2engineMath.js:838

Returns copy of this vector rotated by the clockwise angle passed in

angle number

Returns a copy of this vector with each axis rounded

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

s number

scale

Sets values of this vector and returns self

x number optional, default 0

X axis location

y number optional, default 0

Y axis location

setAngle(angle, length) → Vector2engineMath.js:826

Sets this vector with clockwise angle and length passed in

angle number optional, default 0
length number optional, default 1
setDirection(direction, length) → Vector2engineMath.js:849

Sets this vector to point in the specified integer direction (0-3), corresponding to multiples of 90 degree rotation

direction number
length number optional, default 1

Sets this vector from another vector and returns self

v Vector2

other vector

Returns a copy of this vector snapped down to a grid. Note that grid is the number of snap steps per unit (so grid=2 snaps to halves and grid=0.5 snaps to twos), not the cell size.

grid number

snap steps per unit

subtract(v) → Vector2engineMath.js:747

Returns a copy of this vector minus the vector passed in

v Vector2

other vector

toString(digits) → stringengineMath.js:921

Returns this vector expressed as a string

digits number optional, default 3

precision to display