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

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

  • EngineObject

Members

body

Properties
TypeDescription
Object

The Box2d body

edgeLists

Properties
TypeDescription
Array.<Object>

List of all edges for default box2d drawing

edgeLoops

Properties
TypeDescription
Array.<Object>

List of all edge loops for default box2d drawing

lineColor

Properties
TypeDescription
Color

Line color used for default box2d drawing

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>

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

destroy()

Destroy this object and its physics body

destroyFixture(fixtureopt)

Destroy a fixture from the body

Parameters:
NameTypeAttributesDescription
fixtureObject<optional>

drawFixtures(coloropt, lineColoropt, lineWidthopt, useWebGLopt, contextopt)

Draws all this object's fixtures

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

endContact(otherObject)

Called when a contact ends

Parameters:
NameTypeDescription
otherObjectBox2dObject

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

getSpeed() → {number}

Get the speed of this object

Returns:
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

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 be 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

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

updatePhysics()

Box2d objects updated with Box2d world step