Box2dObject

Box2D Object - extend with your own custom physics objects

  • A LittleJS object with Box2D physics
  • Each object has a Box2D body which can have multiple fixtures and joints
  • Provides interface for Box2D body and fixture functions

Constructor

new Box2dObject(posopt, sizeopt, tileInfoopt, angleopt, coloropt, bodyTypeopt, renderOrderopt)

Create a LittleJS object with Box2d physics

Parameters:
NameTypeAttributesDefaultDescription
posVector2<optional>
sizeVector2<optional>
tileInfoTileInfo<optional>
anglenumber<optional>
0
colorColor<optional>
bodyTypenumber<optional>
renderOrdernumber<optional>
0

Extends

Members

additiveColor

Properties
TypeDescription
Color

Additive color to apply when rendered

angle

Properties
TypeDescription
number

Angle to rotate the object

angleDamping

Properties
NameTypeAttributesDefaultDescription
angleDampingnumber<optional>
objectDefaultAngleDamping

How much to slow down rotation each frame (0-1)

angleVelocity

Properties
TypeDescription
number

Angular velocity of the object

children

Properties
TypeDescription
Array.<EngineObject>

List of children of this object

clampSpeed

Properties
TypeDescription
boolean

Limit object speed along x and y axis

collideRaycast

Properties
TypeDescription
boolean

Object collides with raycasts

collideSolidObjects

Properties
TypeDescription
boolean

Object collides with solid objects

collideTiles

Properties
TypeDescription
boolean

Object collides with the tile collision

color

Properties
TypeDescription
Color

Color to apply when rendered

damping

Properties
NameTypeAttributesDefaultDescription
dampingnumber<optional>
objectDefaultDamping

How much to slow down velocity each frame (0-1)

drawSize

Properties
TypeDescription
Vector2

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

friction

Properties
NameTypeAttributesDefaultDescription
frictionnumber<optional>
objectDefaultFriction

How much friction to apply when sliding (0-1)

gravityScale

Properties
TypeDescription
number

How much to scale gravity by for this object

groundObject

Properties
TypeDescription
EngineObject

Object we are standing on, if any

isSolid

Properties
TypeDescription
boolean

Object collides with and blocks other objects

localAngle

Properties
TypeDescription
number

Local angle if child

localPos

Properties
TypeDescription
Vector2

Local position if child

mass

Properties
NameTypeAttributesDefaultDescription
massnumber<optional>
objectDefaultMass

How heavy the object is, static if 0

mirror

Properties
TypeDescription
boolean

Should it flip along y axis when rendered

parent

Properties
TypeDescription
EngineObject

Parent of object if in local space

pos

Properties
TypeDescription
Vector2

World space position of the object

renderOrder

Properties
TypeDescription
number

Objects are sorted by render order

restitution

Properties
NameTypeAttributesDefaultDescription
restitutionnumber<optional>
objectDefaultRestitution

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

size

Properties
TypeDescription
Vector2

World space width and height of the object

spawnTime

Properties
TypeDescription
number

Track when object was created

tileInfo

Properties
TypeDescription
TileInfo

Tile info to render object (undefined is untextured)

velocity

Properties
TypeDescription
Vector2

Velocity of the object

Methods

addBox(sizeopt, offsetopt, angleopt, densityopt, frictionopt, restitutionopt, isSensoropt)

Add a box shape to the body

Parameters:
NameTypeAttributesDefaultDescription
sizeVector2<optional>
offsetVector2<optional>
anglenumber<optional>
0
densitynumber<optional>
frictionnumber<optional>
restitutionnumber<optional>
isSensorboolean<optional>

addChild(child, localPosopt, localAngleopt)

Attaches a child to this with a given local transform

Parameters:
NameTypeAttributesDefaultDescription
childEngineObject
localPosVector2<optional>
(0,0)
localAnglenumber<optional>
0

addCircle(diameteropt, offsetopt, densityopt, frictionopt, restitutionopt, isSensoropt)

Add a circle shape to the body

Parameters:
NameTypeAttributesDefaultDescription
diameternumber<optional>
1
offsetVector2<optional>
densitynumber<optional>
frictionnumber<optional>
restitutionnumber<optional>
isSensorboolean<optional>

addEdge(point1, point2, densityopt, frictionopt, restitutionopt, isSensoropt)

Add an edge shape to the body

Parameters:
NameTypeAttributesDescription
point1Vector2
point2Vector2
densitynumber<optional>
frictionnumber<optional>
restitutionnumber<optional>
isSensorboolean<optional>

addEdgeList(points, densityopt, frictionopt, restitutionopt, isSensoropt)

Add an edge list to the body

Parameters:
NameTypeAttributesDescription
pointsArray.<Vector2>
densitynumber<optional>
frictionnumber<optional>
restitutionnumber<optional>
isSensorboolean<optional>

addEdgeLoop(points, densityopt, frictionopt, restitutionopt, isSensoropt)

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

Parameters:
NameTypeAttributesDescription
pointsArray.<Vector2>
densitynumber<optional>
frictionnumber<optional>
restitutionnumber<optional>
isSensorboolean<optional>

addPoly(points, densityopt, frictionopt, restitutionopt, isSensoropt)

Add a polygon shape to the body

Parameters:
NameTypeAttributesDescription
pointsArray.<Vector2>
densitynumber<optional>
frictionnumber<optional>
restitutionnumber<optional>
isSensorboolean<optional>

addRandomPoly(diameteropt, densityopt, frictionopt, restitutionopt, isSensoropt)

Add a random polygon shape to the body

Parameters:
NameTypeAttributesDefaultDescription
diameternumber<optional>
1
densitynumber<optional>
frictionnumber<optional>
restitutionnumber<optional>
isSensorboolean<optional>

addRegularPoly(diameteropt, sidesopt, densityopt, frictionopt, restitutionopt, isSensoropt)

Add a regular polygon shape to the body

Parameters:
NameTypeAttributesDefaultDescription
diameternumber<optional>
1
sidesnumber<optional>
8
densitynumber<optional>
frictionnumber<optional>
restitutionnumber<optional>
isSensorboolean<optional>

addShape(shape, densityopt, frictionopt, restitutionopt, isSensoropt)

Add a shape fixture to the body

Parameters:
NameTypeAttributesDefaultDescription
shapeObject
densitynumber<optional>
1
frictionnumber<optional>
0.2
restitutionnumber<optional>
0
isSensorboolean<optional>
false

applyAcceleration(acceleration, posopt)

Apply acceleration to this object

Parameters:
NameTypeAttributesDescription
accelerationVector2
posVector2<optional>

applyAngularAcceleration(acceleration)

Apply angular acceleration to this object

Parameters:
NameTypeDescription
accelerationnumber

applyForce(force, posopt)

Apply force to this object

Parameters:
NameTypeAttributesDescription
forceVector2
posVector2<optional>

applyTorque(torque)

Apply torque to this object

Parameters:
NameTypeDescription
torquenumber

beginContact(otherObject)

Called when a contact begins

Parameters:
NameTypeDescription
otherObjectBox2dObject

collideWithObject(object) → {boolean}

Called to check if a object collision should be resolved

Parameters:
NameTypeDescription
objectEngineObject

the object to test against

Returns:
  • true if the collision should be resolved
Type: 
boolean

collideWithTile(tileData, pos) → {boolean}

Called to check if a tile collision should be resolved

Parameters:
NameTypeDescription
tileDatanumber

the value of the tile at the position

posVector2

tile where the collision occurred

Returns:
  • true if the collision should be resolved
Type: 
boolean

destroy()

Destroy this object and it's physics body

drawFixtures(coloropt, lineColoropt, lineWidthopt, contextopt)

Draws all this object's fixtures

Parameters:
NameTypeAttributesDefaultDescription
colorColor<optional>
lineColorColor<optional>
lineWidthnumber<optional>
0.1
contextCanvasRenderingContext2D<optional>

endContact(otherObject)

Called when a contact ends

Parameters:
NameTypeDescription
otherObjectBox2dObject

getAliveTime() → {number}

How long since the object was created

Returns:
Type: 
number

getAngularVelocity() → {Vector2}

Gets the angular velocity

Returns:
Type: 
Vector2

getBodyType() → {number}

Gets the physics body type

Returns:
Type: 
number

getCenterOfMass() → {Vector2}

Gets the center of mass

Returns:
Type: 
Vector2

getFixtureList() → {Array.<Object>}

Get list of fixtures for this object

Returns:
Type: 
Array.<Object>

getInertia() → {number}

Gets the rotational inertia

Returns:
Type: 
number

getIsAwake() → {boolean}

Check if this object is awake

Returns:
Type: 
boolean

getJointList() → {Array.<Object>}

Get list of joints for this object

Returns:
Type: 
Array.<Object>

getLinearVelocity() → {Vector2}

Gets the linear velocity

Returns:
Type: 
Vector2

getMass() → {number}

Gets the mass

Returns:
Type: 
number

getMirrorSign() → {number}

Get the direction of the mirror

Returns:

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

Type: 
number

hasFixtures() → {boolean}

Check if this object has any fixtures

Returns:
Type: 
boolean

hasJoints() → {boolean}

Check if this object has any joints

Returns:
Type: 
boolean

localToWorld(pos)

Convert from local space to world space

Parameters:
NameTypeDescription
posVector2

local space point

localToWorldVector(vec)

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

Parameters:
NameTypeDescription
vecVector2

local space vector

removeChild(child)

Removes a child from this one

Parameters:
NameTypeDescription
childEngineObject

render()

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

renderDebugInfo()

Render debug info

resetMassData()

Reset the mass, center of mass, and moment

setAngle(angle)

Sets the angle

Parameters:
NameTypeDescription
anglenumber

setAngularDamping(damping)

Sets the angular damping

Parameters:
NameTypeDescription
dampingnumber

setAngularVelocity(angularVelocity)

Sets the angular velocity

Parameters:
NameTypeDescription
angularVelocitynumber

setAwake(isAwakeopt)

Set the sleep state of the body

Parameters:
NameTypeAttributesDefaultDescription
isAwakeboolean<optional>
true

setBodyType(type)

Set the physics body type

Parameters:
NameTypeDescription
typenumber

setBullet(isBulletopt)

Should this body be treated like a bullet for continuous collision detection?

Parameters:
NameTypeAttributesDefaultDescription
isBulletboolean<optional>
true

setCenterOfMass(center)

Set the center of mass of the body

Parameters:
NameTypeDescription
centerVector2

setCollision(collideSolidObjectsopt, isSolidopt, collideTilesopt, collideRaycastopt)

Set how this object collides

Parameters:
NameTypeAttributesDefaultDescription
collideSolidObjectsboolean<optional>
true

Does it collide with solid objects?

isSolidboolean<optional>
true

Does it collide with and block other objects? (expensive in large numbers)

collideTilesboolean<optional>
true

Does it collide with the tile collision?

collideRaycastboolean<optional>
true

Does it collide with raycasts?

setFilterData(categoryBitsopt, ignoreCategoryBitsopt, groupIndexopt)

Set the collision filter data for this body

Parameters:
NameTypeAttributesDefaultDescription
categoryBitsnumber<optional>
0
ignoreCategoryBitsnumber<optional>
0
groupIndexnumber<optional>
0

setFixedRotation(isFixedopt)

Set whether the body can rotate

Parameters:
NameTypeAttributesDefaultDescription
isFixedboolean<optional>
true

setGravityScale(scaleopt)

Sets the gravity scale

Parameters:
NameTypeAttributesDefaultDescription
scalenumber<optional>
1

setLinearDamping(damping)

Sets the linear damping

Parameters:
NameTypeDescription
dampingnumber

setLinearVelocity(velocity)

Sets the linear velocity

Parameters:
NameTypeDescription
velocityVector2

setMass(mass)

Set the mass of the body

Parameters:
NameTypeDescription
massnumber

setMassData(localCenteropt, massopt, momentOfInertiaopt)

Set the mass data of the body

Parameters:
NameTypeAttributesDescription
localCenterVector2<optional>
massnumber<optional>
momentOfInertianumber<optional>

setMomentOfInertia(momentOfInertia)

Set the moment of inertia of the body

Parameters:
NameTypeDescription
momentOfInertianumber

setPosition(pos)

Sets the position

Parameters:
NameTypeDescription
posVector2

setSensor(isSensoropt)

Set if this body is a sensor

Parameters:
NameTypeAttributesDefaultDescription
isSensorboolean<optional>
true

setSleepingAllowed(isAllowedopt)

Set whether the body is allowed to sleep

Parameters:
NameTypeAttributesDefaultDescription
isAllowedboolean<optional>
true

setTransform(pos, angle)

Sets the position and angle

Parameters:
NameTypeDescription
posVector2
anglenumber

toString() → {string}

Returns string containing info about this object for debugging

Returns:
Type: 
string

update()

Copy box2d update sim data

updateTransforms()

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

worldToLocal(pos)

Convert from world space to local space

Parameters:
NameTypeDescription
posVector2

world space point

worldToLocalVector(vec)

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

Parameters:
NameTypeDescription
vecVector2

world space vector