Body Class
A rigid body. Has got a center of mass, position, velocity and a number of shapes that are used for collisions.
Constructor
Body
-
[options]
Parameters:
-
[options]
Object optional-
[mass=0]
Number optionalA number >= 0. If zero, the .motionState will be set to Body.STATIC.
-
[position]
Array optional -
[velocity]
Array optional -
[angle=0]
Number optional -
[angularVelocity=0]
Number optional -
[force]
Array optional -
[angularForce=0]
Number optional -
[fixedRotation=false]
Number optional
-
Item Index
Methods
Properties
- aabb
- aabbNeedsUpdate
- allowSleep
- angle
- angularDamping
- angularForce
- angularVelocity
- AWAKE static
- boundingRadius
- damping
- DYNAMIC static
- fixedRotation
- force
- gravityScale
- id
- inertia
- interpolatedAngle
- interpolatedPosition
- invInertia
- invMass
- KINEMATIC static
- mass
- motionState
- position
- previousAngle
- previousPosition
- shapeAngles
- shapeOffsets
- shapes
- SLEEPING static
- sleepSpeedLimit
- sleepState
- sleepTimeLimit
- SLEEPY static
- STATIC static
- timeLastSleepy
- velocity
- vlambda
- wlambda
- world
Methods
addShape
-
shape
-
[offset]
-
[angle]
Add a shape to the body. You can pass a local transform when adding a shape, so that the shape gets an offset and angle relative to the body center of mass. Will automatically update the mass properties and bounding radius.
Parameters:
-
shape
Shape -
[offset]
Array optionalLocal body offset of the shape.
-
[angle]
Number optionalLocal body angle.
Example:
var body = new Body(),
shape = new Circle();
// Add the shape to the body, positioned in the center
body.addShape(shape);
// Add another shape to the body, positioned 1 unit length from the body center of mass along the local x-axis.
body.addShape(shape,[1,0]);
// Add another shape to the body, positioned 1 unit length from the body center of mass along the local y-axis, and rotated 90 degrees CCW.
body.addShape(shape,[0,1],Math.PI/2);
adjustCenterOfMass
()
Moves the shape offsets so their center of mass becomes the body center of mass.
applyForce
-
force
-
worldPoint
Apply force to a world point. This could for example be a point on the RigidBody surface. Applying force this way will add to Body.force and Body.angularForce.
Parameters:
-
force
ArrayThe force to add.
-
worldPoint
ArrayA world point to apply the force on.
emit
-
event
Emit an event.
Parameters:
-
event
Object-
type
String
-
Returns:
The self object, for chainability.
fromPolygon
-
path
-
[options]
Reads a polygon shape path, and assembles convex shapes from that and puts them at proper offset points.
Parameters:
-
path
ArrayAn array of 2d vectors, e.g. [[0,0],[0,1],...] that resembles a concave or convex polygon. The shape must be simple and without holes.
-
[options]
Object optional-
[optimalDecomp=false]
Boolean optionalSet to true if you need optimal decomposition. Warning: very slow for polygons with more than 10 vertices.
-
[skipSimpleCheck=false]
Boolean optionalSet to true if you already know that the path is not intersecting itself.
-
[removeCollinearPoints=false]
Boolean | Number optionalSet to a number (angle threshold value) to remove collinear points, or false to keep all points.
-
Returns:
True on success, else false.
getArea
()
Number
Get the total area of all shapes in the body
Returns:
has
-
type
-
listener
Check if an event listener is added
Parameters:
-
type
String -
listener
Function
Returns:
off
-
type
-
listener
Remove an event listener
Parameters:
-
type
String -
listener
Function
Returns:
The self object, for chainability.
on
-
type
-
listener
Add an event listener
Parameters:
-
type
String -
listener
Function
Returns:
The self object, for chainability.
removeShape
-
shape
Remove a shape
Parameters:
-
shape
Shape
Returns:
True if the shape was found and removed, else false.
setDensity
()
Set the total density of the body
setZeroForce
()
Sets the force on the body to zero.
sleep
()
Force body sleep
sleepTick
-
float
Parameters:
-
float
Objecttime The world time in seconds
toLocalFrame
-
out
-
worldPoint
Transform a world point to local body frame.
Parameters:
-
out
ArrayThe vector to store the result in
-
worldPoint
ArrayThe input world vector
toWorldFrame
-
out
-
localPoint
Transform a local point to world frame.
Parameters:
-
out
ArrayThe vector to store the result in
-
localPoint
ArrayThe input local vector
updateAABB
()
Updates the AABB of the Body
updateBoundingRadius
()
Update the bounding radius of the body. Should be done if any of the shapes are changed.
updateMassProperties
()
Updates .inertia, .invMass, .invInertia for this Body. Should be called when changing the structure or mass of the Body.
Example:
body.mass += 1;
body.updateMassProperties();
wakeUp
()
Wake the body up. Normally you should not need this, as the body is automatically awoken at events such as collisions. Sets the sleepState to Body.AWAKE and emits the wakeUp event if the body wasn't awake before.
Properties
aabbNeedsUpdate
Boolean
Indicates if the AABB needs update. Update it with .updateAABB().
Example:
// Force update the AABB
body.aabbNeedsUpdate = true;
body.updateAABB();
console.log(body.aabbNeedsUpdate); // false
allowSleep
Boolean
If true, the body will automatically fall to sleep. Note that you need to enable sleeping in the World before anything will happen.
Default: true
angle
Number
The angle of the body, in radians.
Example:
// The angle property is not normalized to the interval 0 to 2*pi, it can be any value.
// If you need a value between 0 and 2*pi, use the following function to normalize it.
function normalizeAngle(angle){
angle = angle % (2*Math.PI);
if(angle < 0){
angle += (2*Math.PI);
}
return angle;
}
angularDamping
Number
The angular force acting on the body. Should be a value between 0 and 1.
Default: 0.1
angularVelocity
Number
The angular velocity of the body, in radians per second.
AWAKE
Number
static
boundingRadius
Number
Bounding circle radius.
damping
Number
The linear damping acting on the body in the velocity direction. Should be a value between 0 and 1.
Default: 0.1
DYNAMIC
Number
static
Dynamic body.
fixedRotation
Boolean
Set to true if you want to fix the rotation of the body.
force
Array
The force acting on the body. Since the body force (and angularForce) will be zeroed after each step, so you need to set the force before each step.
Example:
// This produces a forcefield of 1 Newton in the positive x direction.
for(var i=0; i<numSteps; i++){
body.force[0] = 1;
world.step(1/60);
}
// This will apply a rotational force on the body
for(var i=0; i<numSteps; i++){
body.angularForce = -3;
world.step(1/60);
}
gravityScale
Number
Gravity scaling factor. If you want the body to ignore gravity, set this to zero. If you want to reverse gravity, set it to -1.
Default: 1
id
Number
The body identifyer
inertia
Number
The inertia of the body around the Z axis.
interpolatedAngle
Number
The interpolated angle of the body.
interpolatedPosition
Array
The interpolated position of the body.
invInertia
Number
The inverse inertia of the body.
invMass
Number
The inverse mass of the body.
KINEMATIC
Number
static
Kinematic body.
mass
Number
The mass of the body.
motionState
Number
The type of motion this body has. Should be one of: Body.STATIC, Body.DYNAMIC and Body.KINEMATIC.
- Static bodies do not move, and they do not respond to forces or collision.
- Dynamic bodies body can move and respond to collisions and forces.
- Kinematic bodies only moves according to its .velocity, and does not respond to collisions or force.
Example:
// This body will move and interact with other bodies
var dynamicBody = new Body({
mass : 1 // If mass is nonzero, the body becomes dynamic automatically
});
dynamicBody.motionState == Body.DYNAMIC // true
// This body will not move at all
var staticBody = new Body({
mass : 0 // Will make the body static
});
staticBody.motionState == Body.STATIC // true
// This body will only move if you change its velocity
var kinematicBody = new Body();
kinematicBody.motionState = Body.KINEMATIC;
position
Array
The position of the body
previousAngle
Number
The previous angle of the body.
previousPosition
Array
The previous position of the body.
shapeAngles
Array
The body-local shape angle transforms. This is an array of numbers (angles).
shapeOffsets
Array
The local shape offsets, relative to the body center of mass. This is an array of Array.
shapes
Array
The shapes of the body. The local transform of the shape in .shapes[i] is defined by .shapeOffsets[i] and .shapeAngles[i].
SLEEPING
Number
static
sleepSpeedLimit
Number
If the speed (the norm of the velocity) is smaller than this value, the body is considered sleepy.
Default: 0.2
sleepState
Number
One of Body.AWAKE, Body.SLEEPY and Body.SLEEPING.
The body is initially Body.AWAKE. If its velocity norm is below .sleepSpeedLimit, the sleepState will become Body.SLEEPY. If the body continues to be Body.SLEEPY for .sleepTimeLimit seconds, it will fall asleep (Body.SLEEPY).
Default: Body.AWAKE
sleepTimeLimit
Number
If the body has been sleepy for this sleepTimeLimit seconds, it is considered sleeping.
Default: 1
SLEEPY
Number
static
STATIC
Number
static
Static body.
timeLastSleepy
Number
private
The last time when the body went to SLEEPY state.
velocity
Array
The velocity of the body
vlambda
Array
Constraint velocity that was added to the body during the last step.
wlambda
Array
Angular constraint velocity that was added to the body during last step.
world
World
The world that this body is added to. This property is set to NULL if the body is not added to any world.