LittleJS 1.31.0

Box2D

Box2dPlugin

Box2D Global Object

  • Wraps Box2d world and provides global functions

Constructor

new Box2dPlugin(instance)box2d.js:2422

Create the global Box2D plugin object, box2dInit does this

instance Box2dModule

Members

Methods

Members

bodyTypeDynamic : numberbox2d.js:2453

Dynamic, positive mass, non-zero velocity determined by forces, moved by solver

bodyTypeKinematic : numberbox2d.js:2450

Kinematic, zero mass, non-zero velocity set by user, moved by solver

bodyTypeStatic : numberbox2d.js:2447

Static, zero mass, zero velocity, may be manually moved

The Box2d instance

objects : Array<Box2dObject>box2d.js:2440

List of all Box2d objects, a destroyed one stays until the next step with its body undefined

positionIterations : numberbox2d.js:2444

Position iterations per update

velocityIterations : numberbox2d.js:2442

Velocity iterations per update

The Box2d world

Methods

boxCast(pos, size, includeSensors) → Box2dObject|undefinedbox2d.js:2664

The first object whose shapes' bounding boxes overlap a box, not the shapes themselves, so near the corner of a turned box or a circle it finds one the box does not touch; use pointCast or a raycast for exact

pos Vector2
size Vector2
includeSensors boolean optional, default false

Also find sensors, trigger zones are passed through by default

boxCastAll(pos, size, includeSensors) → Array<Box2dObject>box2d.js:2633

Every object whose shapes' bounding boxes overlap a box, not the shapes themselves, so near the corner of a turned box or a circle it finds one the box does not touch; use pointCast or a raycast for exact

pos Vector2
size Vector2
includeSensors boolean optional, default false

Also find sensors, trigger zones are passed through by default

castJointObject(o) → b2Jointbox2d.js:2860

casts a box2d object to a joint type

o b2Joint
Returns
  • The joint as its own Box2D type
castShapeObject(o) → b2Shapebox2d.js:2840

casts a box2d object to a shape type

o b2Shape
Returns
  • The shape as its own Box2D type
circleCast(pos, diameter, includeSensors) → Box2dObject|undefinedbox2d.js:2711

circle cast and return the object whose position is nearest, of those within the circle

pos Vector2
diameter number
includeSensors boolean optional, default false

Also find objects whose shapes are all sensors, like trigger zones

circleCastAll(pos, diameter, includeSensors) → Array<Box2dObject>box2d.js:2696

circle cast and return all the objects whose position is within the circle, wherever their shapes are

pos Vector2
diameter number
includeSensors boolean optional, default false

Also find objects whose shapes are all sensors, like trigger zones

drawFixture(fixture, pos, angle, color, lineColor, lineWidth, useWebGL, context)box2d.js:2772

draws a fixture

fixture b2Fixture
pos Vector2
angle number
color Color optional
lineColor Color optional
lineWidth number optional, default 0.1
useWebGL boolean optional, default glEnable
context CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D optional
isNull(o) → booleanbox2d.js:2835

checks if a box2d object is null

o Object
pointCast(pos, dynamicOnly, includeSensors) → Box2dObject|undefinedbox2d.js:2731

point cast and return the first object

pos Vector2
dynamicOnly boolean optional, default true
includeSensors boolean optional, default false

Also find sensors, so a pickup radius does not grab its object from afar

raycast(start, end, includeSensors) → Box2dRaycastResult|undefinedbox2d.js:2622

raycast and return the first result

start Vector2
end Vector2
includeSensors boolean optional, default false

Also hit sensors, trigger zones are passed through by default

raycastAll(start, end, includeSensors) → Array<Box2dRaycastResult>box2d.js:2586

raycast and return a list of all the results, nearest first

start Vector2
end Vector2
includeSensors boolean optional, default false

Also hit sensors, trigger zones are passed through by default

step(frames)box2d.js:2536

Step the physics world simulation

frames number optional, default 1
vec2dTo(v) → b2Vec2box2d.js:2826

converts a Vector2 to a new box2d vec2, which stays until destroyed with box2d.instance.destroy; the plugin itself passes Box2D reused ones, since Box2D copies every vector it is given

v Vector2
Returns
  • A Box2D vector
vec2From(v) → Vector2box2d.js:2807

converts a box2d vec2 to a Vector2

v b2Vec2
vec2FromPointer(vp) → Vector2box2d.js:2816

converts a box2d vec2 pointer to a Vector2

vp number