Constructor
new Box2dObject(posopt, sizeopt, tileInfoopt, angleopt, coloropt, bodyTypeopt, renderOrderopt)
Create a LittleJS object with Box2d physics
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
pos | Vector2 | <optional> | ||
size | Vector2 | <optional> | ||
tileInfo | TileInfo | <optional> | ||
angle | number | <optional> | 0 | |
color | Color | <optional> | ||
bodyType | number | <optional> | ||
renderOrder | number | <optional> | 0 |
- Source
Extends
- EngineObject
Members
body
Properties| Type | Description |
|---|---|
| Object | The Box2d body, undefined once it is destroyed |
- Source
edgeLists :Array.<Array.<Vector2>>
- Array.<Array.<Vector2>>
| Type | Description |
|---|---|
| Array.<Array.<Vector2>> | List of all edges for default box2d drawing |
- Source
edgeLoops :Array.<Array.<Vector2>>
- Array.<Array.<Vector2>>
| Type | Description |
|---|---|
| Array.<Array.<Vector2>> | List of all edge loops for default box2d drawing |
- Source
lineColor
Properties| Type | Description |
|---|---|
| Color | Line color used for default box2d drawing |
- Source
lineWidth
Properties| Type | Description |
|---|---|
| number | Line width used for default box2d drawing |
- Source
Methods
addBox(sizeopt, offsetopt, angleopt, densityopt, frictionopt, restitutionopt, isSensoropt)
Add a box shape to the body
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
size | Vector2 | <optional> | ||
offset | Vector2 | <optional> | ||
angle | number | <optional> | 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> |
- Source
addCircle(diameteropt, offsetopt, densityopt, frictionopt, restitutionopt, isSensoropt)
Add a circle shape to the body
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
diameter | number | <optional> | 1 | |
offset | Vector2 | <optional> | ||
density | number | <optional> | ||
friction | number | <optional> | ||
restitution | number | <optional> | ||
isSensor | boolean | <optional> |
- Source
addEdge(point1, point2, densityopt, frictionopt, restitutionopt, isSensoropt)
Add an edge shape to the body
| Name | Type | Attributes | Description |
|---|---|---|---|
point1 | Vector2 | ||
point2 | Vector2 | ||
density | number | <optional> | |
friction | number | <optional> | |
restitution | number | <optional> | |
isSensor | boolean | <optional> |
- Source
addEdgeList(points, densityopt, frictionopt, restitutionopt, isSensoropt)
Add an edge list to the body
| Name | Type | Attributes | Description |
|---|---|---|---|
points | Array.<Vector2> | ||
density | number | <optional> | |
friction | number | <optional> | |
restitution | number | <optional> | |
isSensor | boolean | <optional> |
- Source
addEdgeLoop(points, densityopt, frictionopt, restitutionopt, isSensoropt)
Add an edge loop to the body, an edge loop connects the end points
| Name | Type | Attributes | Description |
|---|---|---|---|
points | Array.<Vector2> | ||
density | number | <optional> | |
friction | number | <optional> | |
restitution | number | <optional> | |
isSensor | boolean | <optional> |
- Source
addPoly(points, densityopt, frictionopt, restitutionopt, isSensoropt)
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
| Name | Type | Attributes | Description |
|---|---|---|---|
points | Array.<Vector2> | ||
density | number | <optional> | |
friction | number | <optional> | |
restitution | number | <optional> | |
isSensor | boolean | <optional> |
- Source
addRandomPoly(diameteropt, densityopt, frictionopt, restitutionopt, isSensoropt)
Add a random polygon shape to the body
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
diameter | number | <optional> | 1 | |
density | number | <optional> | ||
friction | number | <optional> | ||
restitution | number | <optional> | ||
isSensor | boolean | <optional> |
- Source
addRegularPoly(diameteropt, sidesopt, densityopt, frictionopt, restitutionopt, isSensoropt)
Add a regular polygon shape to the body
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
diameter | number | <optional> | 1 | |
sides | number | <optional> | 8 | 3 to 8, the most Box2D polygons have |
density | number | <optional> | ||
friction | number | <optional> | ||
restitution | number | <optional> | ||
isSensor | boolean | <optional> |
- Source
addShape(shape, densityopt, frictionopt, restitutionopt, isSensoropt)
Add a shape fixture to the body
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
shape | Object | |||
density | number | <optional> | 1 | |
friction | number | <optional> | 0.2 | |
restitution | number | <optional> | 0 | |
isSensor | boolean | <optional> | false |
- Source
applyAcceleration(acceleration, posopt)
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.
| Name | Type | Attributes | Description |
|---|---|---|---|
acceleration | Vector2 | ||
pos | Vector2 | <optional> |
- Source
applyAngularAcceleration(acceleration)
Apply angular acceleration to this object (changes angular velocity by acceleration, mass-independent, clockwise — matches EngineObject.applyAngularAcceleration).
| Name | Type | Description |
|---|---|---|
acceleration | number |
- Source
applyAngularImpulse(impulse)
Apply an instantaneous angular impulse. Changes angular velocity by impulse / inertia immediately, clockwise like angle.
| Name | Type | Description |
|---|---|---|
impulse | number |
- Source
applyForce(force, posopt)
Apply force to this object
| Name | Type | Attributes | Description |
|---|---|---|---|
force | Vector2 | ||
pos | Vector2 | <optional> |
- Source
applyImpulse(impulse, posopt)
Apply an instantaneous linear impulse. Changes velocity immediately by impulse / mass (so heavier bodies move less for the same impulse).
| Name | Type | Attributes | Description |
|---|---|---|---|
impulse | Vector2 | ||
pos | Vector2 | <optional> |
- Source
applyTorque(torque)
Apply torque to this object, clockwise like angle
| Name | Type | Description |
|---|---|---|
torque | number |
- Source
beginContact(otherObject, fixtureopt, otherFixtureopt)
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
| Name | Type | Attributes | Description |
|---|---|---|---|
otherObject | Box2dObject | ||
fixture | Object | <optional> | This object's fixture that touched |
otherFixture | Object | <optional> | The other object's fixture that touched |
- Source
destroy(immediateopt)
Destroy this object and its physics body
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
immediate | boolean | <optional> | false | Remove it now, as EngineObject.destroy does, children included |
- Source
destroyAllFixtures()
Destroy all fixtures from the body, from a contact callback once the step is done
- Source
destroyFixture(fixture)
Destroy a fixture from the body, from a contact callback once the step is done
| Name | Type | Description |
|---|---|---|
fixture | Object |
- Source
drawFixtures(coloropt, lineColoropt, lineWidthopt, useWebGLopt, contextopt)
Draws all this object's fixtures
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
color | Color | <optional> | ||
lineColor | Color | <optional> | ||
lineWidth | number | <optional> | 0.1 | |
useWebGL | boolean | <optional> | glEnable | |
context | CanvasRenderingContext2D | <optional> |
- Source
endContact(otherObject, fixtureopt, otherFixtureopt)
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
| Name | Type | Attributes | Description |
|---|---|---|---|
otherObject | Box2dObject | ||
fixture | Object | <optional> | This object's fixture that touched |
otherFixture | Object | <optional> | The other object's fixture that touched |
- Source
getAngularVelocity() → {number}
Gets the angular velocity, clockwise like angle
- Source
- Type:
- number
getBodyType() → {number}
Gets the physics body type
- Source
- Type:
- number
getCenterOfMass() → {Vector2}
Gets the center of mass in world space
- Source
- Type:
- Vector2
getFixtureList() → {Array.<Object>}
Get list of fixtures for this object
- Source
- Type:
- Array.<Object>
getInertia() → {number}
Gets the rotational inertia about the center of mass
- Source
- Type:
- number
getIsAwake() → {boolean}
Check if this object is awake
- Source
- Type:
- boolean
getJointList() → {Array.<(Box2dJoint|Object)>}
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
- Source
- Type:
- Array.<(Box2dJoint|Object)>
getLinearVelocity() → {Vector2}
Gets the linear velocity
- Source
- Type:
- Vector2
getMass() → {number}
Gets the mass
- Source
- Type:
- number
getSpeed() → {number}
Get the speed of this object
- Source
- Type:
- number
hasFixtures() → {boolean}
Check if this object has any fixtures
- Source
- Type:
- boolean
hasJoints() → {boolean}
Check if this object has any joints
- Source
- Type:
- boolean
render()
Render the object, uses box2d drawing if no tile info exists
- Source
renderDebugInfo()
Render debug info
- Source
resetMassData()
Reset the mass, center of mass, and moment, from a contact callback once the step is done
- Source
setAngle(angle)
Sets the angle, from a contact callback the body turns once the step is done, keeping the position it has then
| Name | Type | Description |
|---|---|---|
angle | number |
- Source
setAngularDamping(damping)
Sets the angular damping
| Name | Type | Description |
|---|---|---|
damping | number |
- Source
setAngularVelocity(angularVelocity)
Sets the angular velocity, clockwise like angle
| Name | Type | Description |
|---|---|---|
angularVelocity | number |
- Source
setAwake(isAwakeopt)
Set the sleep state of the body
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
isAwake | boolean | <optional> | true |
- Source
setBodyType(type)
Set the physics body type, from a contact callback it changes once the step is done
| Name | Type | Description |
|---|---|---|
type | number |
- Source
setBullet(isBulletopt)
Should be like a bullet for continuous collision detection?
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
isBullet | boolean | <optional> | true |
- Source
setCenterOfMass(center)
Set the center of mass of the body, local to it
| Name | Type | Description |
|---|---|---|
center | Vector2 |
- Source
setFilterData(categoryBitsopt, ignoreCategoryBitsopt, groupIndexopt)
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
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
categoryBits | number | <optional> | 1 | |
ignoreCategoryBits | number | <optional> | 0 | |
groupIndex | number | <optional> | 0 |
- Source
setFixedRotation(isFixedopt)
Set whether the body can rotate
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
isFixed | boolean | <optional> | true |
- Source
setGravityScale(scaleopt)
Sets the gravity scale
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
scale | number | <optional> | 1 |
- Source
setLinearDamping(damping)
Sets the linear damping
| Name | Type | Description |
|---|---|---|
damping | number |
- Source
setLinearVelocity(velocity)
Sets the linear velocity
| Name | Type | Description |
|---|---|---|
velocity | Vector2 |
- Source
setMass(mass)
Set the mass of the body
| Name | Type | Description |
|---|---|---|
mass | number |
- Source
setMassData(localCenteropt, massopt, momentOfInertiaopt)
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
| Name | Type | Attributes | Description |
|---|---|---|---|
localCenter | Vector2 | <optional> | |
mass | number | <optional> | |
momentOfInertia | number | <optional> | About the center of mass |
- Source
setMomentOfInertia(momentOfInertia)
Set the moment of inertia of the body, about its center of mass
| Name | Type | Description |
|---|---|---|
momentOfInertia | number |
- Source
setPosition(pos)
Sets the position, from a contact callback the body moves once the step is done, keeping the angle it has then
| Name | Type | Description |
|---|---|---|
pos | Vector2 |
- Source
setSensor(isSensoropt)
Set if this body is a sensor
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
isSensor | boolean | <optional> | true |
- Source
setSleepingAllowed(isAllowedopt)
Set whether the body is allowed to sleep
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
isAllowed | boolean | <optional> | true |
- Source
setTransform(pos, angle)
Sets the position and angle, from a contact callback the body moves once the step is done
| Name | Type | Description |
|---|---|---|
pos | Vector2 | |
angle | number |
- Source
updatePhysics()
Box2d objects updated with Box2d world step
- Source
updateTransforms(updateChildrenopt)
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
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
updateChildren | boolean | <optional> | true | Also update the children's transforms |
- Source