LittleJS 1.28.0

Box2D

Box2dObject

Box2D Object - extend with your own custom physics objects

  • A LittleJS object with Box2D physics, dynamic by default
  • Provides interface for Box2D body and fixture functions
  • Each object can have multiple fixtures and joints
  • Angular values are clockwise like angle: angular velocity, torque, joint angles, limits and motor speeds
  • Box2D moves the body, so the EngineObject physics fields do nothing on it: velocity, angleVelocity, damping, angleDamping, mass, friction, restitution and gravityScale; use setLinearVelocity, setAngularVelocity, setLinearDamping, setAngularDamping, setMass, the friction and restitution of its shapes and setGravityScale. A debug build warns once when one of them is set
  • oneWay makes it one way as in 2D, like a platform jumped up through: up, down, left or right, the way others pass through, turning with the body; it blocks only what first touches it from its far side, and what it lets pass gets no beginContact or endContact, though a sensor does
  • surfaceSpeed makes it a conveyor belt, and preSolve changes a contact before it is solved

Extends EngineObject

Extended by Box2dKinematicObject, Box2dStaticObject

Constructor

new Box2dObject(pos, size, tileInfo, angle, color, bodyType, renderOrder)box2d.js:383

Create a LittleJS object with Box2d physics

pos Vector2 optional
size Vector2 optional
tileInfo TileInfo optional
angle number optional, default 0
color Color optional
bodyType number optional
renderOrder number optional, default 0

Members

Methods

Inherited from EngineObject

Members

body : Objectbox2d.js:405

The Box2d body, undefined once it is destroyed

edgeLists : Array<Array<Vector2>>box2d.js:413

List of all edges for default box2d drawing

edgeLoops : Array<Array<Vector2>>box2d.js:416

List of all edge loops for default box2d drawing

lineColor : Colorbox2d.js:408

Line color used for default box2d drawing

lineWidth : numberbox2d.js:410

Line width used for default box2d drawing

surfaceSpeed : numberbox2d.js:422

How fast its surface moves, clockwise around it, in world units per second, like a conveyor belt: what touches it is carried along by friction, to the right across its top when positive; set it with setSurfaceSpeed, which wakes what rests on it

Methods

addBox(size, offset, angle, density, friction, restitution, isSensor) → Object|undefinedbox2d.js:594

Add a box shape to the body

size Vector2 optional
offset Vector2 optional
angle number optional, default 0

LittleJS convention (clockwise positive). Negated internally to match Box2D's CCW-positive convention so the fixture aligns with the same angle passed to drawRect/drawTile.

density number optional
friction number optional
restitution number optional
isSensor boolean optional
Returns
  • The fixture made, Box2D's own, undefined for a shape too small to make
addCircle(diameter, offset, density, friction, restitution, isSensor) → Object|undefinedbox2d.js:709

Add a circle shape to the body

diameter number optional, default 1
offset Vector2 optional
density number optional
friction number optional
restitution number optional
isSensor boolean optional
Returns
  • The fixture made, Box2D's own, undefined for a circle too big to make
addEdge(point1, point2, density, friction, restitution, isSensor) → Objectbox2d.js:731

Add an edge shape to the body

point1 Vector2
point2 Vector2
density number optional
friction number optional
restitution number optional
isSensor boolean optional
Returns
  • The fixture made, Box2D's own
addEdgeList(points, density, friction, restitution, isSensor) → Array<Object>box2d.js:750

Add an edge list to the body

points Array<Vector2>
density number optional
friction number optional
restitution number optional
isSensor boolean optional
Returns
  • The fixtures made, Box2D's own
addEdgeLoop(points, density, friction, restitution, isSensor) → Array<Object>box2d.js:784

Add an edge loop to the body, an edge loop connects the end points

points Array<Vector2>
density number optional
friction number optional
restitution number optional
isSensor boolean optional
Returns
  • The fixtures made, Box2D's own
addPoly(points, density, friction, restitution, isSensor) → Object|undefinedbox2d.js:620

Add a polygon shape to the body, the convex hull of its points; Box2D takes 3 to 8 points, not all in a line, and no fixture is made from any other; points closer than .001 count as one

points Array<Vector2>
density number optional
friction number optional
restitution number optional
isSensor boolean optional
Returns
  • The fixture made, Box2D's own, undefined for a shape too small to make
addRandomPoly(diameter, density, friction, restitution, isSensor) → Object|undefinedbox2d.js:689

Add a random polygon shape to the body

diameter number optional, default 1
density number optional
friction number optional
restitution number optional
isSensor boolean optional
Returns
  • The fixture made, Box2D's own, undefined for a shape too small to make
addRegularPoly(diameter, sides, density, friction, restitution, isSensor) → Object|undefinedbox2d.js:668

Add a regular polygon shape to the body

diameter number optional, default 1
sides number optional, default 8

3 to 8, the most Box2D polygons have

density number optional
friction number optional
restitution number optional
isSensor boolean optional
Returns
  • The fixture made, Box2D's own, undefined for a shape too small to make
addShape(shape, density, friction, restitution, isSensor) → Objectbox2d.js:564

Add a shape fixture to the body

shape Object
density number optional, default 1
friction number optional, default 0.2
restitution number optional, default 0
isSensor boolean optional, default false
Returns
  • The fixture made, Box2D's own
applyAcceleration(acceleration, pos)box2d.js:1099

Apply acceleration to this object (changes velocity by acceleration, mass-independent like EngineObject.applyAcceleration, but in units per second). Use applyImpulse if you want the mass-dependent velocity change Δv = impulse / mass, or applyForce for a Newton-style sustained force.

acceleration Vector2
pos Vector2 optional
applyAngularAcceleration(acceleration)box2d.js:1129

Apply angular acceleration to this object (changes angular velocity by acceleration, mass-independent, clockwise — matches EngineObject.applyAngularAcceleration).

acceleration number
applyAngularImpulse(impulse)box2d.js:1141

Apply an instantaneous angular impulse. Changes angular velocity by impulse / inertia immediately, clockwise like angle.

impulse number
applyForce(force, pos)box2d.js:1086

Apply force to this object

force Vector2
pos Vector2 optional
applyImpulse(impulse, pos)box2d.js:1111

Apply an instantaneous linear impulse. Changes velocity immediately by impulse / mass (so heavier bodies move less for the same impulse).

impulse Vector2
pos Vector2 optional
applyTorque(torque)box2d.js:1120

Apply torque to this object, clockwise like angle

torque number
beginContact(otherObject, fixture, otherFixture, impactSpeed, contact)box2d.js:526

Called when a contact begins, while the world steps: a destroy or a setter waits until the step is done, and creating objects, fixtures or joints must wait until after the step

  • The fixtures say which shapes touched, the same objects addBox and the others returned, so a small sensor under a player's feet can tell standing on the ground from touching a wall
  • impactSpeed is how fast they were closing along the contact normal, spin included, in world units per second, measured before the solver pushed them apart; 0 for a sensor
  • contact has where they touched, the normal toward this object and the impulse of the hit, which counts their mass and how it is spread: a stick heavy at its ends hits harder than one heavy in the middle, spinning at the same speed
otherObject Box2dObject
fixture Object optional

This object's fixture that touched

otherFixture Object optional

The other object's fixture that touched

impactSpeed number optional

How fast they were closing as they touched

contact Box2dContact optional

Where they touched and how hard they hit

destroy(immediate)box2d.js:433

Destroy this object and its physics body

immediate boolean optional, default false

Remove it now, as EngineObject.destroy does, children included

destroyAllFixtures()box2d.js:832

Destroy all fixtures from the body, from a contact callback once the step is done

destroyFixture(fixture)box2d.js:812

Destroy a fixture from the body, from a contact callback once the step is done

fixture Object
drawFixtures(color, lineColor, lineWidth, useWebGL, context)box2d.js:492

Draw all this object's fixtures

color Color optional
lineColor Color optional
lineWidth number optional, default 0.1
useWebGL boolean optional, default glEnable
context CanvasRenderingContext2D optional
endContact(otherObject, fixture, otherFixture)box2d.js:533

Called when a contact ends, while the world steps or a body is destroyed: a destroy or a setter waits until the step is done, and creating objects, fixtures or joints must wait until after the step

otherObject Box2dObject
fixture Object optional

This object's fixture that touched

otherFixture Object optional

The other object's fixture that touched

getAngularVelocity() → numberbox2d.js:861

Gets the angular velocity, clockwise like angle

getBodyType() → numberbox2d.js:882

Gets the physics body type

getCenterOfMass() → Vector2box2d.js:853

Gets the center of mass in world space

getFixtureList() → Array<Object>box2d.js:1156

Get list of fixtures for this object

getInertia() → numberbox2d.js:869

Gets the rotational inertia about the center of mass

getIsAwake() → booleanbox2d.js:878

Check if this object is awake

getJointList() → Array<(Box2dJoint|Object)>box2d.js:1174

Get list of joints for this object, the Box2dJoint for each one made through LittleJS, and the Box2D joint, cast to its type, for any made on the world directly

getLinearVelocity() → Vector2box2d.js:857

Gets the linear velocity

getMass() → numberbox2d.js:865

Gets the mass

getSpeed() → numberbox2d.js:886

Get the speed of this object

getVelocityAtPoint(pos) → Vector2box2d.js:892

Get the velocity of a point on this object, its linear velocity and its spin about its center of mass together, in Box2D's units like getLinearVelocity

pos Vector2

the point in world space

hasFixtures() → booleanbox2d.js:1152

Check if this object has any fixtures

hasJoints() → booleanbox2d.js:1169

Check if this object has any joints

preSolve(otherObject, fixture, otherFixture, contact)box2d.js:552

Called before Box2D solves a contact, each step while they touch, for an object that overrides it, while the world steps as beginContact is; change the contact to change how they collide

  • enabled false lets them pass through each other until they stop touching, like a platform dropped through
  • friction and restitution are kept while they touch, surfaceSpeed is for this step, like a patch of a belt
  • A sleeping body's contacts are not solved, so wake it with setAwake for this to be called, as for a player standing still on a platform it drops through
otherObject Box2dObject
fixture Object optional

This object's fixture that touches

otherFixture Object optional

The other object's fixture that touches

contact Box2dPreSolveContact optional

Where they touch and how they collide

// drop down through a one way platform
player.preSolve = (other, fixture, otherFixture, contact)=>
{
    if (other.oneWay && keyIsDown('ArrowDown'))
        contact.enabled = false;
}
render()box2d.js:468

Render the object, uses box2d drawing if no tile info exists

renderDebugInfo()box2d.js:478

Draw its fixtures for the debug view: yellow asleep, blue static, both when both

resetMassData()box2d.js:1019

Reset the mass, center of mass, and moment, from a contact callback once the step is done

setAngle(angle)box2d.js:930

Sets the angle, from a contact callback the body turns once the step is done, keeping the position it has then

angle number
setAngularDamping(damping)box2d.js:960

Sets the angular damping, Box2D's: a rate, 0 none and larger slows its turning faster, with no upper limit; angleDamping does nothing on a Box2dObject

damping number
setAngularVelocity(angularVelocity)box2d.js:948

Sets the angular velocity, clockwise like angle

angularVelocity number
setAwake(isAwake)box2d.js:986

Set the sleep state of the body

isAwake boolean optional, default true
setBodyType(type)box2d.js:990

Set the physics body type, from a contact callback it changes once the step is done

type number
setBullet(isBullet)box2d.js:982

Should be like a bullet for continuous collision detection?

isBullet boolean optional, default true
setCenterOfMass(center)box2d.js:1007

Set the center of mass of the body, local to it

center Vector2
setFilterData(categoryBits, ignoreCategoryBits, groupIndex)box2d.js:1060

Set the collision filter data for the fixtures this body has now, a fixture added later has the default filter, category 1 colliding with everything

categoryBits number optional, default 1
ignoreCategoryBits number optional, default 0
groupIndex number optional, default 0
setFixedRotation(isFixed)box2d.js:999

Set whether the body can rotate

isFixed boolean optional, default true
setGravityScale(scale)box2d.js:965

Sets the gravity scale

scale number optional, default 1
setLinearDamping(damping)box2d.js:954

Sets the linear damping, Box2D's: a rate, 0 none and larger slows it faster, with no upper limit, where the damping of an EngineObject is the fraction it keeps each frame and does nothing on a Box2dObject

damping number
setLinearVelocity(velocity)box2d.js:943

Sets the linear velocity

velocity Vector2
setMass(mass)box2d.js:1011

Set the mass of the body

mass number
setMassData(localCenter, mass, momentOfInertia)box2d.js:1027

Set the mass data of the body, from a contact callback once the step is done; a mass of 0 or less becomes 1, use setBodyType for a static body; call it after adding fixtures and after setFixedRotation, both of which put the mass back to what the fixtures give

localCenter Vector2 optional
mass number optional
momentOfInertia number optional

About the center of mass

setMomentOfInertia(momentOfInertia)box2d.js:1015

Set the moment of inertia of the body, about its center of mass

momentOfInertia number
setPosition(pos)box2d.js:916

Sets the position, from a contact callback the body moves once the step is done, keeping the angle it has then

pos Vector2
setSensor(isSensor)box2d.js:1074

Set if this body is a sensor

isSensor boolean optional, default true
setSleepingAllowed(isAllowed)box2d.js:994

Set whether the body is allowed to sleep

isAllowed boolean optional, default true
setSurfaceSpeed(speed)box2d.js:973

Sets how fast its surface moves, clockwise around it, like a conveyor belt, see surfaceSpeed

speed number

World units per second

setTransform(pos, angle)box2d.js:900

Sets the position and angle, from a contact callback the body moves once the step is done

pos Vector2
angle number
updatePhysics()box2d.js:456

Box2d objects updated with Box2d world step

updateTransforms(updateChildren)box2d.js:461

Update the object transform, called automatically by engine even when paused; its body places it, so it can be a parent but not a child, connect it to another with a joint

updateChildren boolean optional, default true

Also update the children's transforms