LittleJS 1.28.0

Math

LittleJS Math Classes and Functions

  • Comprehensive math utilities for game development
  • Vector2 class for 2D positions, directions, and math operations
  • Color class for RGBA colors with interpolation and manipulation
  • RandomGenerator for seeded pseudo-random number generation
  • Math shortcuts (PI, abs, floor, ceil, min, max, sin, cos, etc.)
  • Interpolation functions (lerp, smoothStep, percent)
  • Clamping, wrapping, and modulo operations
  • Angle utilities with wrap-around support
  • Collision detection (overlapping, intersection, line tests)
  • Random number generation and seeding
  • Type checking utilities

Functions

Constants

Typedefs

Functions

clamp(value, min, max) → numberengineMath.js:117

Clamps the value between max and min

value number
min number optional, default 0
max number optional, default 1
collideBoxBox(posA, sizeA, posB, sizeB) → Vector2|undefinedengineMath.js:323

Returns the vector to move box A by so it no longer overlaps box B, the shortest way out, or undefined

  • isOverlapping is the yes or no version of this
posA Vector2

Center of box A

sizeA Vector2

Full size of box A

posB Vector2

Center of box B

sizeB Vector2

Full size of box B

collideCircleBox(pos, radius, boxPos, boxSize) → Vector2|undefinedengineMath.js:302

Returns the vector to move a circle out of an axis aligned box, or undefined

pos Vector2

Center of the circle

radius number
boxPos Vector2

Center of the box

boxSize Vector2

Full size of the box

collideCircleCircle(posA, radiusA, posB, radiusB) → Vector2|undefinedengineMath.js:285

Returns the vector to move circle A by so it no longer overlaps circle B, or undefined

posA Vector2

Center of circle A

radiusA number
posB Vector2

Center of circle B

radiusB number
distanceAngle(angleA, angleB) → numberengineMath.js:177

Returns signed wrapped distance between the two angles passed in

angleA number
angleB number
distanceWrap(valueA, valueB, wrapSize) → numberengineMath.js:155

Returns signed wrapped distance between the two values passed in

valueA number
valueB number
wrapSize number optional, default 1
hsl(h, s, l, a) → ColorengineMath.js:970

Create a color object with HSLA values, white by default

h number optional, default 0

hue

s number optional, default 0

saturation

l number optional, default 1

lightness

a number optional, default 1

alpha

isArray(a) → a is Array<any>engineMath.js:387

Check if object is an array

a any
isColor(c) → booleanengineMath.js:981

Check if object is a valid Color

c any
isIntersecting(start, end, pos, size) → booleanengineMath.js:238

Returns true if a line segment is intersecting an axis aligned box

start Vector2

Start of raycast

end Vector2

End of raycast

pos Vector2

Center of box

size Vector2

Size of box

Returns
  • True if intersecting
isNumber(n) → booleanengineMath.js:369

Check if object is a valid number, not NaN or undefined, but it may be infinite

n any
isOverlapping(posA, sizeA, posB, sizeB) → booleanengineMath.js:221

Returns true if two axis aligned bounding boxes are overlapping this can be used for simple collision detection between objects

posA Vector2

Center of box A

sizeA Vector2

Size of box A

posB Vector2

Center of box B

sizeB Vector2 optional, default vec2()

Size of box B, uses a point if undefined

Returns
  • True if overlapping
isPowerOfTwo(value) → booleanengineMath.js:201

Checks if the value passed in is a power of two

value number
isStringLike(s) → booleanengineMath.js:380

Check if a value is stringifiable — i.e. it has a toString that returns a string. Use this for ASSERTs and inputs that will be coerced to text; use typeof x === 'string' inline if you need strict-string semantics.

  • Returns true for strings, numbers, and most objects
  • Returns false for null and undefined
s any
isVector2(v) → booleanengineMath.js:687

Check if object is a valid Vector2

v any
lerp(valueA, valueB, percent) → numberengineMath.js:134

Linearly interpolates between values passed in using percent, percent is clamped to 0-1

valueA number
valueB number
percent number
lerpAngle(angleA, angleB, percent) → numberengineMath.js:185

Linearly interpolates between the angles passed in with wrapping, percent is clamped to 0-1

angleA number
angleB number
percent number
lerpWrap(valueA, valueB, percent, wrapSize) → numberengineMath.js:169

Linearly interpolates between values passed in with wrapping, percent is clamped to 0-1

valueA number
valueB number
percent number
wrapSize number optional, default 1
lineTest(posStart, posEnd, testFunction, normal) → Vector2|undefinedengineMath.js:403

Casts a ray and returns position of the first collision found, or undefined if none are found

posStart Vector2
posEnd Vector2
testFunction LineTestFunction

Check if colliding

normal Vector2 optional

Optional vector to store the normal

Returns
  • Position of the collision or undefined if none found
log2(x) → numberengineMath.js:77

Returns log2 of value passed in

x number
mod(dividend, divisor) → numberengineMath.js:109

Returns first param modulo the second param, but adjusted so negative numbers work as expected

dividend number
divisor number optional, default 1
nearestPowerOfTwo(value) → numberengineMath.js:207

Returns the nearest whole power of two not less than the value, 1 for a value of 1 or less

value number
oscillate(frequency, amplitude, t, offset, type) → numberengineMath.js:348

Returns an oscillating wave between 0 and amplitude with frequency of 1 Hz by default

frequency number optional, default 1

Frequency of the wave in Hz

amplitude number optional, default 1

Amplitude (max height) of the wave

t number optional, default time

Value to use for time of the wave

offset number optional, default 0

Value to use for time offset of the wave

type number optional, default 0

Wave type: 0=sine, 1=triangle, 2=square, 3=sawtooth

Returns
  • Value waving between 0 and amplitude
percent(value, valueA, valueB) → numberengineMath.js:125

Returns what percentage the value is between valueA and valueB, clamped to 0-1

value number
valueA number
valueB number
percentLerp(value, percentA, percentB, lerpA, lerpB) → numberengineMath.js:146

Gets percent between percentA and percentB and linearly interpolates between lerpA and lerpB A shortcut for lerp(lerpA, lerpB, percent(value, percentA, percentB))

value number
percentA number
percentB number
lerpA number
lerpB number
rgb(r, g, b, a) → ColorengineMath.js:946

Create a color object with RGBA values, white by default

r number optional, default 1

red

g number optional, default 1

green

b number optional, default 1

blue

a number optional, default 1

alpha

sign(x) → numberengineMath.js:65

Returns the sign of value passed in

x number
smoothStep(percent) → numberengineMath.js:191

Applies smoothstep function to the percentage value, clamped between 0 and 1

percent number

Create a 2d vector, can take 1 or 2 scalar values

x number optional, default 0
y number optional

if y is undefined, x is used for both

let a = vec2(0, 1); // vector with coordinates (0, 1)
a = vec2(5);        // set a to (5, 5)
b = vec2();         // set b to (0, 0)

Constants

abs : numberengineMath.js:29

Returns absolute value of value passed in

x number
atan2 : numberengineMath.js:102

Returns atan2 of values passed in

y number
x number

Color - Black #000000

Color - Blue #0000ff

ceil : numberengineMath.js:41

Returns ceiled value of value passed in

x number

Color - Clear Black #00000000 with 0 alpha

Color - Clear White #ffffff00 with 0 alpha

cos : numberengineMath.js:89

Returns cos of value passed in

x number

Color - Cyan #00ffff

floor : numberengineMath.js:35

Returns floored value of value passed in

x number

Color - Gray #808080

Color - Green #00ff00

hypot : numberengineMath.js:71

Returns hypotenuse of values passed in

…values number

Color - Magenta #ff00ff

max : numberengineMath.js:59

Returns highest value passed in

…values number
min : numberengineMath.js:53

Returns lowest value passed in

…values number

Color - Orange #ff8000

PI : number default Math.PIengineMath.js:23

The value of PI

Color - Purple #8000ff

Color - Red #ff0000

round : numberengineMath.js:47

Returns rounded value passed in

x number
sin : numberengineMath.js:83

Returns sin of value passed in

x number
tan : numberengineMath.js:95

Returns tan of value passed in

x number

Color - White #ffffff

Color - Yellow #ffff00

Typedefs

LineTestFunction(pos)engineMath.js:389
pos Vector2