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
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 optionalTile info to render object (undefined is untextured)
angle number optional, default 0Angle the object is rotated by
color Color optional, default WHITEColor to apply to tile when rendered
renderOrder number optional, default 0Objects 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);
Additive color to apply when rendered
Angle to rotate the object, in world space on a child too, where setting it turns
the child and its localAngle follows
Fraction of angular velocity kept each frame, 1 keeps all of it, 0 stops at once
Angular velocity of the object, in radians per frame
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
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
Limit object speed along x and y axis
Object collides with the level, its tile collision layers
Object collides with raycasts
Object collides with solid objects
Color to apply when rendered
Fraction of velocity kept each frame, 1 keeps all of it, 0 stops at once
Has object been destroyed?
Size of object used for drawing, uses size if not set
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
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
How much to scale gravity by for this object
Object we are standing on, if any
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
Object collides with and blocks other objects
Angle relative to the parent, only while attached to one
Position relative to the parent, only while attached to one
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
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.
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
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, on a child too, where setting it moves the
child there and its localPos follows
Objects are sorted by render order
How bouncy the object is when colliding (0-1)
Custom shader to render with, undefined for the engine's own
World space width and height of the object
Track when object was created
Tile info to render object (undefined is untextured)
Velocity of the object, in world units per frame
Attaches a child to this with a local transform, returns child for chaining
child EngineObjectlocalPos Vector2 optional, default vec2()localAngle number optional, default 0
- Returns
The child object added
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
Apply angular acceleration to this object
- Does nothing on a static object (mass 0), set its angleVelocity instead
acceleration number
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
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 EngineObjectthe object to test against
push Vector3 optionalwhat 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
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 numberthe value of the tile at the position
pos Vector2the tile's bottom left corner in world space
isHit boolean optionaltrue 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 this object, destroy its children, detach its parent, and mark it for removal
immediate boolean optional, default falsetrue removes attached effects like particle emitters at once, false lets them finish first
How long since the object was created
Get the direction of the mirror
- Returns
-1 if this.mirror is true, or 1 if not mirrored
Get this object's right vector
scale number optional, default 1length of the vector
Get the speed of this object
Get this object's up vector
scale number optional, default 1length of the vector
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 Vector2the point in world space
Check if overlapping a point or aligned bounding box
pos Vector2Center of box
size Vector2 optional, default vec2()Size of box, uses a point if undefined
Check if this object's box overlaps another object's box
object EngineObject
Convert from local space to world space
pos Vector2local space point
Convert from local space to world space for a vector (rotation only)
vec Vector2local space vector
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
Render debug info for this object
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
Optional hook called during the light system plugin's lightmap pass to draw this object's lightmap contribution. Does nothing by default
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
Set how this object collides
collideSolidObjects boolean optional, default trueDoes it collide with solid objects?
isSolid boolean optional, default trueDoes it collide with and block other objects? (expensive in large numbers)
collideLevel boolean optional, default trueDoes it collide with the level, its tile collision layers?
collideRaycast boolean optional, default trueDoes it collide with raycasts?
Returns string containing info about this object for debugging
Update the object, called automatically by engine once each frame. Does nothing by default
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
Convert from world space to local space
pos Vector2world space point
Convert from world space to local space for a vector (rotation only)
vec Vector2world space vector