LittleJS 1.27.0

Engine

EngineObject

LittleJS Object Base Object Class

  • Top level object class used by the engine
  • Automatically adds self to object list
  • Will be updated and rendered each frame
  • Renders as a sprite from a tilesheet by default
  • Can have color and additive color applied
  • 2D Physics and collision system
  • Sorted by renderOrder
  • Objects can have children attached
  • Parents are updated before children, and set child transform
  • Call destroy() to get rid of objects

The physics system used by objects is simple and fast with some caveats...

  • Collision uses the axis aligned size, the object's rotation angle is only for rendering
  • Objects are guaranteed to not intersect tile collision from physics
  • If an object starts or is moved inside tile collision, it will not collide with that tile
  • Collision for objects can be set to be solid to block other objects
  • Objects may get pushed into overlapping other solid objects, if so they will push away
  • A static solid (mass 0) moved by its velocity, like a door or an elevator, pushes objects out of its way
  • Solid objects are more performance intensive and should be used sparingly

Extended by Box2dObject, CanvasLayer, DirectionalLight, EngineObject3D, Light, ParticleEmitter, ThreeJSObject

Constructor

new EngineObject(pos, size, tileInfo, angle, color, renderOrder)engineObject.js:145

Create an engine object and adds it to the list of objects

pos Vector2 optional, default vec2()

World space position of the object

size Vector2 optional, default vec2(1)

World space size of the object

tileInfo TileInfo optional

Tile info to render object (undefined is untextured)

angle number optional, default 0

Angle the object is rotated by

color Color optional, default WHITE

Color to apply to tile when rendered

renderOrder number optional, default 0

Objects sorted by renderOrder before being rendered

// create an engine object, normally you would first extend the class with your own
const pos = vec2(2,3);
const object = new EngineObject(pos);

Members

Methods

Members

additiveColor : Color|undefinedengineObject.js:181

Additive color to apply when rendered

angle : numberengineObject.js:176

Angle to rotate the object

angleDamping : numberengineObject.js:208

Fraction of angular velocity kept each frame, 1 keeps all of it, 0 stops at once

angleVelocity : numberengineObject.js:221

Angular velocity of the object, in radians per frame

castBackgroundShadow : booleanengineObject.js:189

Does this object draw into the light system's background map, which a DirectionalLight lights only at its edges facing the light; for a background layer, with castShadow false

castShadow : booleanengineObject.js:186

Does this object draw into the light system's shadow map; false for a floor layer, a background, a pickup

List of children of this object

clampSpeed : booleanengineObject.js:228

Limit object speed along x and y axis

collideLevel : booleanengineObject.js:264

Object collides with the level, its tile collision layers

collideRaycast : booleanengineObject.js:270

Object collides with raycasts

collideSolidObjects : booleanengineObject.js:266

Object collides with solid objects

Color to apply when rendered

damping : numberengineObject.js:206

Fraction of velocity kept each frame, 1 keeps all of it, 0 stops at once

destroyed : booleanengineObject.js:198

Has object been destroyed?

drawSize : Vector2|undefinedengineObject.js:171

Size of object used for drawing, uses size if not set

emissive : numberengineObject.js:194

With the light system, how much it lights itself: 0 lit only by the lights, 1 full brightness in its own colors whatever the lights do, between partly; drawn into the lightmap through renderEmissive, as 3D's emissive. Exact for solid pixels; a partly transparent one is self lit by its alpha too, so a half alpha pixel shows at a quarter and a fading emissive sprite fades a little faster

friction : numberengineObject.js:213

Fraction of sliding speed kept each frame on the ground, 1 is no friction, 0 stops at once, the more slippery of the object and its ground is used

gravityScale : numberengineObject.js:215

How much to scale gravity by for this object

groundObject : EngineObject|undefinedengineObject.js:242

Object we are standing on, if any

isBullet : booleanengineObject.js:239

Move through the tiles as a point along a ray, so it can not pass through one at any speed, for small fast things like bullets: its speed is not held to objectMaxSpeed for the tiles, collideWithTile is asked with this.pos where the ray goes into the tile, and on a hit it stops at the surface, bounces off it by restitution and goes on with the rest of its move, so it slides along a floor

  • Its collision with solid objects is the usual one, so one with a size that collides with them is still held to objectMaxSpeed; one of no size is not, it never collides with them
  • this.pos is set for each tile asked about and put where the move ends after, so a change collideWithTile makes to it is not kept; move it after the physics, in update
  • Once destroyed, as by its own collideWithTile, it is asked about no more tiles that frame, where an object that is not a bullet still is
isSolid : booleanengineObject.js:268

Object collides with and blocks other objects

localAngle : numberengineObject.js:259

Local angle if child

localPos : Vector2|undefinedengineObject.js:257

Position relative to the parent, only while attached to one

mass : numberengineObject.js:204

How heavy the object is, static if 0: a static object moves by its velocity but does not collide on its own, the moving ones collide with it

mirror : booleanengineObject.js:196

Should the rendered tile flip along the y axis. Affects rendering and the local→world transform of attached children (a mirrored parent flips its children's localPos.x and localAngle). Does not affect this object's own physics, collision, or localToWorld/worldToLocal.

oneWay : Vector2|undefinedengineObject.js:249

Makes a solid object one way, like a platform jumped up through: up, down, left or right, the way others pass through it; it blocks only what was wholly on its far side before it moved, or stood on it, and the rest are not stopped or asked through collideWithObject; with no mass it moves before the other objects each frame, so a lift works the same at any render order; tiles are made one way by the layer's setOneWay

Parent of object if in local space

persistent : booleanengineObject.js:274

Object is skipped by engineObjectsDestroy, for things that outlive a level like a camera

  • Calling destroy on it still destroys it, and its children go with it either way

World space position of the object

renderOrder : numberengineObject.js:217

Objects are sorted by render order

restitution : numberengineObject.js:210

How bouncy the object is when colliding (0-1)

shader : Shader|undefinedengineObject.js:184

Custom shader to render with, undefined for the engine's own

World space width and height of the object

spawnTime : numberengineObject.js:223

Track when object was created

tileInfo : TileInfo|undefinedengineObject.js:174

Tile info to render object (undefined is untextured)

Velocity of the object, in world units per frame

Methods

addChild(child, localPos, localAngle) → EngineObjectengineObject.js:767

Attaches a child to this with a local transform, returns child for chaining

child EngineObject
localPos Vector2 optional, default vec2()
localAngle number optional, default 0
Returns

The child object added

applyAcceleration(acceleration)engineObject.js:743

Apply acceleration to this object (adjust velocity, not affected by mass)

  • Does nothing on a static object (mass 0), set its velocity instead
acceleration Vector2
applyAngularAcceleration(acceleration)engineObject.js:749

Apply angular acceleration to this object

  • Does nothing on a static object (mass 0), set its angleVelocity instead
acceleration number
applyForce(force)engineObject.js:755

Apply force to this object (adjust velocity, affected by mass)

  • Does nothing on a static object (mass 0), set its velocity instead
force Vector2

Attaches a child to this without moving it: the local transform is worked out from where the child is now, where addChild takes one; a child of something else is moved over, returns child for chaining

child EngineObject
Returns

The child object attached

collideWithObject(object, push) → booleanengineObject.js:720

Called by the engine to check if an object collision should be resolved. Return true for physics to resolve the collision or false to ignore and resolve it manually

  • Both objects of a touching pair are asked once a frame, whichever order they update in; an object that destroys itself here is gone at the end of the frame and is still asked about the pairs left this frame, so a bullet that should hit one thing checks its own destroyed flag first
  • With many solids each mover finds those near it through a grid, which follows the other object after this returns; a third object moved here is found where it is now from the next update on
object EngineObject

the object to test against

push Vector3 optional

what it would take to move this object clear, a Vector3 from the 3D plugin, undefined in 2D

Returns
  • true if the collision should be resolved by modifying it's position and velocity
collideWithTile(tileData, pos) → booleanengineObject.js:708

Called to check if a tile collision should be resolved. Return true for physics to resolve the collision or false to ignore and resolve it manually

  • Called for each solid tile the physics tests, which can be several times a frame for the same tile, and for positions it only tries, so keep it free of side effects or guard them to once a frame
  • this.pos has already moved, so a check on where it came from needs the position saved in update; for a one way platform the layer's setOneWay does that, and this is not asked about a one way tile it passes through
  • For the point of impact, like sparks where a bullet hit, set isBullet: this.pos is then where it meets the tile, as the platformer's Bullet uses it
tileData number

the value of the tile at the position

pos Vector2

the tile's bottom left corner in world space

Returns
  • true if the collision should be resolved by modifying it's position and velocity
destroy(immediate)engineObject.js:664

Destroy this object, destroy its children, detach its parent, and mark it for removal

immediate boolean optional, default false

true removes attached effects like particle emitters at once, false lets them finish first

getAliveTime() → numberengineObject.js:734

How long since the object was created

getMirrorSign() → numberengineObject.js:760

Get the direction of the mirror

Returns

-1 if this.mirror is true, or 1 if not mirrored

getRight(scale) → Vector2engineObject.js:730

Get this object's right vector

scale number optional, default 1

length of the vector

getSpeed() → numberengineObject.js:738

Get the speed of this object

Get this object's up vector

scale number optional, default 1

length of the vector

isOverlapping(pos, size) → booleanengineObject.js:821

Check if overlapping a point or aligned bounding box

pos Vector2

Center of box

size Vector2 optional, default vec2()

Size of box, uses a point if undefined

isOverlappingObject(object) → booleanengineObject.js:814

Check if this object's box overlaps another object's box

object EngineObject
localToWorld(pos) → Vector2engineObject.js:681

Convert from local space to world space

pos Vector2

local space point

localToWorldVector(vec) → Vector2engineObject.js:691

Convert from local space to world space for a vector (rotation only)

vec Vector2

local space vector

removeChild(child)engineObject.js:801

Removes a child from this one, it stays where it is in the world

child EngineObject

Render the object, draws a tile by default, automatically called each frame, sorted by renderOrder

renderDebugInfo()engineObject.js:857

Render debug info for this object

renderEmissive()engineObject.js:660

Draw this object's shape into the light system's lightmap, called during its light pass when emissive is above 0; what it draws shows in its own colors that much brighter. Calls render() by default so the whole object glows; override to glow a part, like a robot's eyes

renderLight()engineObject.js:649

Optional hook called during the light system plugin's lightmap pass to draw this object's lightmap contribution. Does nothing by default

renderShadow()engineObject.js:655

Draw this object into the light system's shadow map, called during its shadow pass when castShadow is set, and into its background map when castBackgroundShadow is set and a DirectionalLight is out. Calls render() by default so the object casts its own shape; override to cast a different one, like a blob at a character's feet so its body stays lit; screen space WebGL draws in render() are skipped during the pass

setCollision(collideSolidObjects, isSolid, collideLevel, collideRaycast)engineObject.js:829

Set how this object collides

collideSolidObjects boolean optional, default true

Does it collide with solid objects?

isSolid boolean optional, default true

Does it collide with and block other objects? (expensive in large numbers)

collideLevel boolean optional, default true

Does it collide with the level, its tile collision layers?

collideRaycast boolean optional, default true

Does it collide with raycasts?

toString() → stringengineObject.js:841

Returns string containing info about this object for debugging

Update the object, called automatically by engine once each frame. Does nothing by default

updatePhysics()engineObject.js:311

Update the object physics, called automatically by engine once each frame. Can be overridden to stop or change how physics works for an object

  • With many solids each mover finds those near it through a grid, which follows a solid after its own physics update: a solid an override moves for another object, a lift carrying a crate, is found there from the next update on
updateTransforms(updateChildren)engineObject.js:282

Update the object transform, called automatically by engine even when paused

updateChildren boolean optional, default true

Also update the children's transforms

worldToLocal(pos) → Vector2engineObject.js:686

Convert from world space to local space

pos Vector2

world space point

worldToLocalVector(vec) → Vector2engineObject.js:696

Convert from world space to local space for a vector (rotation only)

vec Vector2

world space vector