new Box2dObject(pos, size, tileInfo, angle, color, bodyType, renderOrder)box2d.js:460Create a LittleJS object with Box2d physics
Box2D Object - extend with your own custom physics objects
Extends EngineObject
Extended by Box2dKinematicObject, Box2dStaticObject
new Box2dObject(pos, size, tileInfo, angle, color, bodyType, renderOrder)box2d.js:460Create a LittleJS object with Box2d physics
body : b2Bodybox2d.js:485The Box2d body, undefined once it is destroyed
edgeListFixtures : Map<number, Array<Vector2>>box2d.js:499edgeLists : Array<Array<Vector2>>box2d.js:493List of all edges for default box2d drawing
edgeLoops : Array<Array<Vector2>>box2d.js:496List of all edge loops for default box2d drawing
lineColor : Colorbox2d.js:488Line color used for default box2d drawing
lineWidth : numberbox2d.js:490Line width used for default box2d drawing
surfaceSpeed : numberbox2d.js:503How 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
addBox(size, offset, angle, density, friction, restitution, isSensor) → b2Fixture|undefinedbox2d.js:689Add a box shape to the body
size Vector2 optionaloffset Vector2 optionalangle number optional, default 0LittleJS 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 optionalfriction number optionalrestitution number optionalisSensor boolean optionaladdCircle(diameter, offset, density, friction, restitution, isSensor) → b2Fixture|undefinedbox2d.js:807Add a circle shape to the body
diameter number optional, default 1offset Vector2 optionaldensity number optionalfriction number optionalrestitution number optionalisSensor boolean optionaladdEdge(point1, point2, density, friction, restitution, isSensor) → b2Fixturebox2d.js:831Add an edge shape to the body
point1 Vector2point2 Vector2density number optionalfriction number optionalrestitution number optionalisSensor boolean optionaladdEdgeList(points, density, friction, restitution, isSensor) → Array<b2Fixture>box2d.js:852Add an edge list to the body
points Array<Vector2>density number optionalfriction number optionalrestitution number optionalisSensor boolean optionaladdEdgeLoop(points, density, friction, restitution, isSensor) → Array<b2Fixture>box2d.js:887Add an edge loop to the body, an edge loop connects the end points
points Array<Vector2>density number optionalfriction number optionalrestitution number optionalisSensor boolean optionaladdPoly(points, density, friction, restitution, isSensor) → b2Fixture|undefinedbox2d.js:717Add 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 optionalfriction number optionalrestitution number optionalisSensor boolean optionaladdRandomPoly(diameter, density, friction, restitution, isSensor) → b2Fixture|undefinedbox2d.js:787Add a random polygon shape to the body
diameter number optional, default 1density number optionalfriction number optionalrestitution number optionalisSensor boolean optionaladdRegularPoly(diameter, sides, density, friction, restitution, isSensor) → b2Fixture|undefinedbox2d.js:766Add a regular polygon shape to the body
diameter number optional, default 1sides number optional, default 83 to 8, the most Box2D polygons have
density number optionalfriction number optionalrestitution number optionalisSensor boolean optionaladdShape(shape, density, friction, restitution, isSensor) → b2Fixturebox2d.js:659Add a shape fixture to the body
shape b2Shapedensity number optional, default 1friction number optional, default 0.2restitution number optional, default 0isSensor boolean optional, default falseapplyAcceleration(acceleration, pos)box2d.js:1209Apply 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.
applyAngularAcceleration(acceleration)box2d.js:1239Apply angular acceleration to this object (changes angular velocity by acceleration, mass-independent, clockwise — matches EngineObject.applyAngularAcceleration).
acceleration numberapplyAngularImpulse(impulse)box2d.js:1251Apply an instantaneous angular impulse. Changes angular velocity by impulse / inertia immediately, clockwise like angle.
impulse numberapplyImpulse(impulse, pos)box2d.js:1221Apply an instantaneous linear impulse. Changes velocity immediately by impulse / mass (so heavier bodies move less for the same impulse).
beginContact(otherObject, fixture, otherFixture, impactSpeed, contact)box2d.js:620Called when a contact begins, while the world steps: a destroy, and a setter that moves a body or changes its type or mass, waits until the step is done, and creating objects, fixtures or joints must wait until after the step
otherObject Box2dObjectfixture b2Fixture optionalThis object's fixture that touched
otherFixture b2Fixture optionalThe other object's fixture that touched
impactSpeed number optionalHow fast they were closing as they touched
contact Box2dContact optionalWhere they touched and how hard they hit
destroy(immediate)box2d.js:516Destroy this object and its physics body
immediate boolean optional, default falsetrue removes attached effects like particle emitters at once, false lets them finish first, as EngineObject.destroy does, children included
destroyAllFixtures()box2d.js:936Destroy all fixtures from the body, from a contact callback once the step is done
destroyFixture(fixture)box2d.js:916Destroy a fixture from the body, from a contact callback once the step is done
fixture b2FixturedrawFixtures(color, lineColor, lineWidth, useWebGL, context)box2d.js:585Draw all this object's fixtures
endContact(otherObject, fixture, otherFixture)box2d.js:628Called when a contact ends, while the world steps or a body is destroyed: a destroy, and a setter that moves a body or changes its type or mass, waits until the step is done, and creating objects, fixtures or joints must wait until after the step
otherObject Box2dObjectfixture b2Fixture optionalThis object's fixture that touched
otherFixture b2Fixture optionalThe other object's fixture that touched
getAngularVelocity() → numberbox2d.js:965Gets the angular velocity, clockwise like angle
getBodyType() → numberbox2d.js:986Gets the physics body type
getCenterOfMass() → Vector2box2d.js:957Gets the center of mass in world space
getFixtureList() → Array<b2Fixture>box2d.js:1266Get list of fixtures for this object
getInertia() → numberbox2d.js:973Gets the rotational inertia about the center of mass
getIsAwake() → booleanbox2d.js:982Check if this object is awake
getJointList() → Array<(Box2dJoint|b2Joint)>box2d.js:1284Get 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:961Gets the linear velocity
getMass() → numberbox2d.js:969Gets the mass
getSpeed() → numberbox2d.js:990Get the speed of this object
getVelocityAtPoint(pos) → Vector2box2d.js:996Get 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 Vector2the point in world space
hasFixtures() → booleanbox2d.js:1262Check if this object has any fixtures
hasJoints() → booleanbox2d.js:1279Check if this object has any joints
preSolve(otherObject, fixture, otherFixture, contact)box2d.js:647Called 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
otherObject Box2dObjectfixture b2Fixture optionalThis object's fixture that touches
otherFixture b2Fixture optionalThe other object's fixture that touches
contact Box2dPreSolveContact optionalWhere 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:561Render the object, uses box2d drawing if no tile info exists
renderDebugInfo()box2d.js:571Draw its fixtures for the debug view: yellow asleep, blue static, both when both
resetMassData()box2d.js:1129Reset the mass, center of mass, and moment, from a contact callback once the step is done
setAngle(angle)box2d.js:1039Sets the angle, from a contact callback the body turns once the step is done, keeping the position it has then
angle numbersetAngularDamping(damping)box2d.js:1070Sets 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 numbersetAngularVelocity(angularVelocity)box2d.js:1058Sets the angular velocity, clockwise like angle
angularVelocity numbersetBodyType(type)box2d.js:1100Set the physics body type, from a contact callback it changes once the step is done
type numbersetBullet(isBullet)box2d.js:1092Should be like a bullet for continuous collision detection?
isBullet boolean optional, default truesetFilterData(categoryBits, ignoreCategoryBits, groupIndex)box2d.js:1170Set 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 1ignoreCategoryBits number optional, default 0groupIndex number optional, default 0setFixedRotation(isFixed)box2d.js:1109Set whether the body can rotate
isFixed boolean optional, default truesetLinearDamping(damping)box2d.js:1064Sets 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 numbersetMassData(localCenter, mass, momentOfInertia)box2d.js:1137Set 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 optionalmass number optionalmomentOfInertia number optionalAbout the center of mass
setMomentOfInertia(momentOfInertia)box2d.js:1125Set the moment of inertia of the body, about its center of mass
momentOfInertia numbersetPosition(pos)box2d.js:1024Sets the position, from a contact callback the body moves once the step is done, keeping the angle it has then
pos Vector2setSleepingAllowed(isAllowed)box2d.js:1104Set whether the body is allowed to sleep
isAllowed boolean optional, default truesetSurfaceSpeed(speed)box2d.js:1083Sets how fast its surface moves, clockwise around it, like a conveyor belt, see surfaceSpeed
speed numberWorld units per second
setTransform(pos, angle)box2d.js:1004Sets the position and angle, from a contact callback the body moves once the step is done
pos Vector2angle number optionalLeft out, the angle it has when the move happens, as setPosition keeps
updatePhysics()box2d.js:549Box2d objects updated with Box2d world step
updateTransforms(updateChildren)box2d.js:554Update 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 trueAlso update the children's transforms