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
Members
additiveColor
PropertiesType | Description |
---|---|
Color | Additive color to apply when rendered |
- Overrides
- Source
angle
PropertiesType | Description |
---|---|
number | Angle to rotate the object |
- Overrides
- Source
angleDamping
PropertiesName | Type | Attributes | Default | Description |
---|---|---|---|---|
angleDamping | number | <optional> | objectDefaultAngleDamping | How much to slow down rotation each frame (0-1) |
- Overrides
- Source
angleVelocity
PropertiesType | Description |
---|---|
number | Angular velocity of the object |
- Overrides
- Source
children
PropertiesType | Description |
---|---|
Array.<EngineObject> | List of children of this object |
- Overrides
- Source
clampSpeed
PropertiesType | Description |
---|---|
boolean | Limit object speed along x and y axis |
- Overrides
- Source
collideRaycast
PropertiesType | Description |
---|---|
boolean | Object collides with raycasts |
- Overrides
- Source
collideSolidObjects
PropertiesType | Description |
---|---|
boolean | Object collides with solid objects |
- Overrides
- Source
collideTiles
PropertiesType | Description |
---|---|
boolean | Object collides with the tile collision |
- Overrides
- Source
color
PropertiesType | Description |
---|---|
Color | Color to apply when rendered |
- Overrides
- Source
damping
PropertiesName | Type | Attributes | Default | Description |
---|---|---|---|---|
damping | number | <optional> | objectDefaultDamping | How much to slow down velocity each frame (0-1) |
- Overrides
- Source
drawSize
PropertiesType | Description |
---|---|
Vector2 | Size of object used for drawing, uses size if not set |
- Overrides
- Source
friction
PropertiesName | Type | Attributes | Default | Description |
---|---|---|---|---|
friction | number | <optional> | objectDefaultFriction | How much friction to apply when sliding (0-1) |
- Overrides
- Source
gravityScale
PropertiesType | Description |
---|---|
number | How much to scale gravity by for this object |
- Overrides
- Source
groundObject
PropertiesType | Description |
---|---|
EngineObject | Object we are standing on, if any |
- Overrides
- Source
isSolid
PropertiesType | Description |
---|---|
boolean | Object collides with and blocks other objects |
- Overrides
- Source
localAngle
PropertiesType | Description |
---|---|
number | Local angle if child |
- Overrides
- Source
localPos
PropertiesType | Description |
---|---|
Vector2 | Local position if child |
- Overrides
- Source
mass
PropertiesName | Type | Attributes | Default | Description |
---|---|---|---|---|
mass | number | <optional> | objectDefaultMass | How heavy the object is, static if 0 |
- Overrides
- Source
mirror
PropertiesType | Description |
---|---|
boolean | Should it flip along y axis when rendered |
- Overrides
- Source
parent
PropertiesType | Description |
---|---|
EngineObject | Parent of object if in local space |
- Overrides
- Source
pos
PropertiesType | Description |
---|---|
Vector2 | World space position of the object |
- Overrides
- Source
renderOrder
PropertiesType | Description |
---|---|
number | Objects are sorted by render order |
- Overrides
- Source
restitution
PropertiesName | Type | Attributes | Default | Description |
---|---|---|---|---|
restitution | number | <optional> | objectDefaultRestitution | How bouncy the object is when colliding (0-1) |
- Overrides
- Source
size
PropertiesType | Description |
---|---|
Vector2 | World space width and height of the object |
- Overrides
- Source
spawnTime
PropertiesType | Description |
---|---|
number | Track when object was created |
- Overrides
- Source
tileInfo
PropertiesType | Description |
---|---|
TileInfo | Tile info to render object (undefined is untextured) |
- Overrides
- Source
velocity
PropertiesType | Description |
---|---|
Vector2 | Velocity of the object |
- Overrides
- 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 | |
density | number | <optional> | ||
friction | number | <optional> | ||
restitution | number | <optional> | ||
isSensor | boolean | <optional> |
- Source
addChild(child, localPosopt, localAngleopt)
Attaches a child to this with a given local transform
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
child | EngineObject | |||
localPos | Vector2 | <optional> | (0,0) | |
localAngle | number | <optional> | 0 |
- Overrides
- 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
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 | |
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
- Overrides
- Source
applyAngularAcceleration(acceleration)
Apply angular acceleration to this object
Name | Type | Description |
---|---|---|
acceleration | number |
- Source
applyForce(force, posopt)
Apply force to this object
- Overrides
- Source
applyTorque(torque)
Apply torque to this object
Name | Type | Description |
---|---|---|
torque | number |
- Source
beginContact(otherObject)
Called when a contact begins
Name | Type | Description |
---|---|---|
otherObject | Box2dObject |
- Source
collideWithObject(object) → {boolean}
Called to check if a object collision should be resolved
Name | Type | Description |
---|---|---|
object | EngineObject | the object to test against |
- Overrides
- Source
- true if the collision should be resolved
- Type:
- boolean
collideWithTile(tileData, pos) → {boolean}
Called to check if a tile collision should be resolved
Name | Type | Description |
---|---|---|
tileData | number | the value of the tile at the position |
pos | Vector2 | tile where the collision occurred |
- Overrides
- Source
- true if the collision should be resolved
- Type:
- boolean
destroy()
Destroy this object and it's physics body
- Overrides
- Source
drawFixtures(coloropt, lineColoropt, lineWidthopt, contextopt)
Draws all this object's fixtures
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
color | Color | <optional> | ||
lineColor | Color | <optional> | ||
lineWidth | number | <optional> | 0.1 | |
context | CanvasRenderingContext2D | <optional> |
- Source
endContact(otherObject)
Called when a contact ends
Name | Type | Description |
---|---|---|
otherObject | Box2dObject |
- Source
getAliveTime() → {number}
How long since the object was created
- Overrides
- Source
- Type:
- number
getAngularVelocity() → {Vector2}
Gets the angular velocity
- Source
- Type:
- Vector2
getBodyType() → {number}
Gets the physics body type
- Source
- Type:
- number
getCenterOfMass() → {Vector2}
Gets the center of mass
- Source
- Type:
- Vector2
getFixtureList() → {Array.<Object>}
Get list of fixtures for this object
- Source
- Type:
- Array.<Object>
getInertia() → {number}
Gets the rotational inertia
- Source
- Type:
- number
getIsAwake() → {boolean}
Check if this object is awake
- Source
- Type:
- boolean
getJointList() → {Array.<Object>}
Get list of joints for this object
- Source
- Type:
- Array.<Object>
getLinearVelocity() → {Vector2}
Gets the linear velocity
- Source
- Type:
- Vector2
getMass() → {number}
Gets the mass
- Source
- Type:
- number
getMirrorSign() → {number}
Get the direction of the mirror
- Overrides
- Source
-1 if this.mirror is true, or 1 if not mirrored
- 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
localToWorld(pos)
Convert from local space to world space
Name | Type | Description |
---|---|---|
pos | Vector2 | local space point |
- Overrides
- Source
localToWorldVector(vec)
Convert from local space to world space for a vector (rotation only)
Name | Type | Description |
---|---|---|
vec | Vector2 | local space vector |
- Overrides
- Source
removeChild(child)
Removes a child from this one
Name | Type | Description |
---|---|---|
child | EngineObject |
- Overrides
- Source
render()
Render the object, uses box2d drawing if no tile info exists
- Overrides
- Source
renderDebugInfo()
Render debug info
- Overrides
- Source
resetMassData()
Reset the mass, center of mass, and moment
- Source
setAngle(angle)
Sets the angle
Name | Type | Description |
---|---|---|
angle | number |
- Source
setAngularDamping(damping)
Sets the angular damping
Name | Type | Description |
---|---|---|
damping | number |
- Source
setAngularVelocity(angularVelocity)
Sets the angular velocity
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
Name | Type | Description |
---|---|---|
type | number |
- Source
setBullet(isBulletopt)
Should this body be treated 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
Name | Type | Description |
---|---|---|
center | Vector2 |
- Source
setCollision(collideSolidObjectsopt, isSolidopt, collideTilesopt, collideRaycastopt)
Set how this object collides
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
collideSolidObjects | boolean | <optional> | true | Does it collide with solid objects? |
isSolid | boolean | <optional> | true | Does it collide with and block other objects? (expensive in large numbers) |
collideTiles | boolean | <optional> | true | Does it collide with the tile collision? |
collideRaycast | boolean | <optional> | true | Does it collide with raycasts? |
- Overrides
- Source
setFilterData(categoryBitsopt, ignoreCategoryBitsopt, groupIndexopt)
Set the collision filter data for this body
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
categoryBits | number | <optional> | 0 | |
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
Name | Type | Attributes | Description |
---|---|---|---|
localCenter | Vector2 | <optional> | |
mass | number | <optional> | |
momentOfInertia | number | <optional> |
- Source
setMomentOfInertia(momentOfInertia)
Set the moment of inertia of the body
Name | Type | Description |
---|---|---|
momentOfInertia | number |
- Source
setPosition(pos)
Sets the position
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
Name | Type | Description |
---|---|---|
pos | Vector2 | |
angle | number |
- Source
toString() → {string}
Returns string containing info about this object for debugging
- Overrides
- Source
- Type:
- string
update()
Copy box2d update sim data
- Overrides
- Source
updateTransforms()
Update the object transform, called automatically by engine even when paused
- Overrides
- Source
worldToLocal(pos)
Convert from world space to local space
Name | Type | Description |
---|---|---|
pos | Vector2 | world space point |
- Overrides
- Source
worldToLocalVector(vec)
Convert from world space to local space for a vector (rotation only)
Name | Type | Description |
---|---|---|
vec | Vector2 | world space vector |
- Overrides
- Source