LittleJS 1.28.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, FollowCamera, Light, ParticleEmitter, ThreeJSObject

Constructor

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

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:218

Additive color to apply when rendered

angle : numberengineObject.js:213

Angle to rotate the object, in world space on a child too, where setting it turns the child and its localAngle follows

angleDamping : numberengineObject.js:245

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

angleVelocity : numberengineObject.js:258

Angular velocity of the object, in radians per frame

castBackgroundShadow : booleanengineObject.js:226

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:223

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:265

Limit object speed along x and y axis

collideLevel : booleanengineObject.js:305

Object collides with the level, its tile collision layers

collideRaycast : booleanengineObject.js:311

Object collides with raycasts

collideSolidObjects : booleanengineObject.js:307

Object collides with solid objects

Color to apply when rendered

damping : numberengineObject.js:243

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

destroyed : booleanengineObject.js:235

Has object been destroyed?

drawSize : Vector2|undefinedengineObject.js:207

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

emissive : numberengineObject.js:231

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:250

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:252

How much to scale gravity by for this object

groundObject : EngineObject|undefinedengineObject.js:279

Object we are standing on, if any

isBullet : booleanengineObject.js:276

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:309

Object collides with and blocks other objects

localAngle : numberengineObject.js:296

Angle relative to the parent, only while attached to one

localPos : Vector2|undefinedengineObject.js:294

Position relative to the parent, only while attached to one

mass : numberengineObject.js:241

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:233

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:286

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:315

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
placedTransform : Array<number>|undefinedengineObject.js:300

World space position of the object, on a child too, where setting it moves the child there and its localPos follows

renderOrder : numberengineObject.js:254

Objects are sorted by render order

restitution : numberengineObject.js:247

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

shader : Shader|undefinedengineObject.js:221

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

World space width and height of the object

spawnTime : numberengineObject.js:260

Track when object was created

tileInfo : TileInfo|undefinedengineObject.js:210

Tile info to render object (undefined is untextured)

Velocity of the object, in world units per frame

Methods

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

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:815

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:821

Apply angular acceleration to this object

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

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:781

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, isHit) → booleanengineObject.js:769

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 side effects to the calls where isHit is true
  • isHit is true when the tile is one the object overlaps where its move put it, the physics' first test, or one a bullet's ray goes into; a box is asked about the tiles it overlaps until one returns true, so a box against two tiles at once is asked about one of them; false for the places the physics only tries after that, and for the game's own tests and raycasts
  • 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

isHit boolean optional

true when it overlaps the tile where it moved to, false for a place only tried

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

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:795

How long since the object was created

getMirrorSign() → numberengineObject.js:832

Get the direction of the mirror

Returns

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

getRight(scale) → Vector2engineObject.js:791

Get this object's right vector

scale number optional, default 1

length of the vector

getSpeed() → numberengineObject.js:799

Get the speed of this object

Get this object's up vector

scale number optional, default 1

length of the vector

getVelocityAtPoint(pos) → Vector2engineObject.js:805

Get the velocity of a point on this object, its velocity and its spin about pos together, per frame like velocity, so the tip of a spinning blade moves faster than its middle; its length is the point's speed

pos Vector2

the point in world space

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

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:887

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

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

Convert from local space to world space

pos Vector2

local space point

localToWorldVector(vec) → Vector2engineObject.js:747

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

vec Vector2

local space vector

removeChild(child)engineObject.js:874

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:930

Render debug info for this object

renderEmissive()engineObject.js:716

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:705

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:711

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:902

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:914

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:358

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:325

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

  • A child is placed from its parent, and a pos or angle set on it since is kept first, made its localPos or localAngle; the engine does that each frame, call this after setting one to have them right away
updateChildren boolean optional, default true

Also update the children's transforms

worldToLocal(pos) → Vector2engineObject.js:742

Convert from world space to local space

pos Vector2

world space point

worldToLocalVector(vec) → Vector2engineObject.js:752

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

vec Vector2

world space vector