Bounding box of this body. Update with updateAABB.
Indicates if the AABB needs update. Update it with "Body.updateAABB".
// Force update the AABB
body.aabbNeedsUpdate = true;
body.updateAABB();
console.log(body.aabbNeedsUpdate); // false
If true, the body will automatically fall to sleep. Note that you need to enable sleeping in the World before anything will happen.
true
The angle of the body, in radians.
// 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;
}
The angular force acting on the body. Should be a value between 0 and 1.
0.1
The angular force acting on the body. See force.
The angular velocity of the body, in radians per second.
Bounding circle radius. Update with updateBoundingRadius.
The number of iterations that should be used when searching for the time of impact during CCD. A larger number will assure that there's a small penetration on CCD collision, but a small number will give more performance.
10
If the body speed exceeds this threshold, CCD (continuous collision detection) will be enabled. Set it to a negative number to disable CCD completely for this body.
-1
Whether to produce contact forces when in contact with other bodies. Note that contacts will be generated, but they will be disabled. That means that this body will move through other bodies, but it will still trigger contact events, etc.
The linear damping acting on the body in the velocity direction. Should be a value between 0 and 1.
0.1
Set to true if you want to fix the rotation of the body.
// Fix rotation during runtime
body.fixedRotation = true;
body.updateMassProperties();
Set to true if you want to fix the body movement along the X axis. The body will still be able to move along Y.
// Fix X movement on body creation
var body = new Body({ mass: 1, fixedX: true });
// Fix X movement during runtime
body.fixedX = true;
body.updateMassProperties();
Set to true if you want to fix the body movement along the Y axis. The body will still be able to move along X. See .fixedX
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.
// 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);
}
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.
1
The body identifier
How long the body has been sleeping.
Index of the body in the World .bodies array. Is set to -1 if the body isn't added to a World.
The inertia of the body around the Z axis.
The interpolated angle of the body. Use this for rendering.
The interpolated position of the body. Use this for rendering.
The inverse inertia of the body.
The inverse mass of the body.
The mass of the body. If you change this number, you should call updateMassProperties.
body.mass = 1;
body.updateMassProperties();
The position of the body in the world. Don't use this for rendering, instead use .interpolatedPosition
The previous angle of the body.
The previous position of the body.
The shapes of the body.
If the speed (the norm of the velocity) is smaller than this value, the body is considered sleepy.
0.2
One of AWAKE, SLEEPY and 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).
Body.AWAKE
If the body has been sleepy for this sleepTimeLimit seconds, it is considered sleeping.
1
The last time when the body went to SLEEPY state.
The type of motion this body has. Should be one of: STATIC, DYNAMIC and KINEMATIC.
// Bodies are static by default. Static bodies will never move.
var body = new Body();
console.log(body.type == Body.STATIC); // true
// By setting the mass of a body to a nonzero number, the body
// will become dynamic and will move and interact with other bodies.
var dynamicBody = new Body({
mass : 1
});
console.log(dynamicBody.type == Body.DYNAMIC); // true
// Kinematic bodies will only move if you change their velocity.
var kinematicBody = new Body({
type: Body.KINEMATIC // Type can be set via the options object.
});
The current velocity of the body.
Constraint velocity that was added to the body during the last step.
Whether the body wants to sleep
Angular constraint velocity that was added to the body during last step.
The world that this body is added to (read only). This property is set to NULL if the body is not added to any world.
Static
AWAKEAwake sleep state.
Static
DYNAMICDynamic body.
Static
KINEMATICKinematic body.
Static
SLEEPINGSleeping sleep state.
Static
SLEEPYSleepy sleep state.
Static
STATICStatic body.
Static
_idId counter for Body instances
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.
var body = new Body(),
shape = new Circle({ radius: 1 });
// 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);
Moves the shape offsets so their center of mass becomes the body center of mass.
var body = new Body({ position: [0, 0] });
var shape = new Circle({ radius: 1 });
body.addShape(shape, [1, 0], 0);
body.adjustCenterOfMass();
console.log(body.position); // [1, 0]
console.log(shape.position); // [0, 0]
Apply damping, see this for details.
Current time step
Apply force to a point relative to the center of mass of the body. This could for example be a point on the Body surface. Applying force this way will add to Body.force and Body.angularForce.
var body = new Body({ mass: 1 });
var relativePoint = [1, 0]; // Will apply the force at [body.position[0] + 1, body.position[1]]
var force = [0, 1]; // up
body.applyForce(force, relativePoint);
console.log(body.force); // [0, 1]
console.log(body.angularForce); // 1
Apply force to a point relative to the center of mass of the body. This could for example be a point on the Body surface. Applying force this way will add to Body.force and Body.angularForce.
var body = new Body({ mass: 1 });
var localPoint = [1, 0]; // x=1 locally in the body
var localForce = [0, 1]; // up, locally in the body
body.applyForceLocal(localForce, localPoint);
console.log(body.force); // [0, 1]
console.log(body.angularForce); // 1
Apply impulse to a point relative to the body. This could for example be a point on the Body surface. An impulse is a force added to a body during a short period of time (impulse = force * time). Impulses will be added to Body.velocity and Body.angularVelocity.
var body = new Body({ mass: 1 });
var relativePoint = [0, 0]; // center of the body
var impulseVector = [0, 1]; // world up
body.applyImpulse(impulseVector, relativePoint);
Apply impulse to a point relative to the body. This could for example be a point on the Body surface. An impulse is a force added to a body during a short period of time (impulse = force * time). Impulses will be added to Body.velocity and Body.angularVelocity.
var body = new Body({ mass: 1 });
var localPoint = [1, 0]; // x=1, locally in the body
var localImpulse = [0, 1]; // up, locally in the body
body.applyImpulseLocal(localImpulse, localPoint);
console.log(body.velocity); // [1, 0]
console.log(body.angularVelocity); // 1
Emit an event.
The self object, for chainability.
emitter.emit({
type: 'myEvent',
customData: 123
});
Reads a polygon shape path, and assembles convex shapes from that and puts them at proper offset points.
An 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.
Optional
optimalOptional
removeOptional
skipTrue on success, else false.
var body = new Body();
var path = [
[-1, 1],
[-1, 0],
[1, 0],
[1, 1],
[0.5, 0.5]
];
body.fromPolygon(path);
console.log(body.shapes); // [Convex, Convex, ...]
Get velocity of a point in the body.
The result vector
var body = new Body({
mass: 1,
velocity: [1, 0],
angularVelocity: 1
});
var result = [];
var point = [1, 0];
body.getVelocityAtPoint(result, point);
console.log(result); // [1, 1]
Check if an event listener is added
Optional
listener: ((e) => void)Remove an event listener
The self object, for chainability.
emitter.on('myEvent', handler); // Add handler
emitter.off('myEvent', handler); // Remove handler
Add an event listener
The self object, for chainability.
emitter.on('myEvent', function(evt){
console.log('myEvt was triggered!');
});
Check if the body is overlapping another body. Note that this method only works if the body was added to a World and if at least one step was taken.
if the body overlaps the given body
Remove a shape.
True if the shape was found and removed, else false.
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 AWAKE and emits the wakeUp event if the body wasn't awake before.
A rigid body. Has got a center of mass, position, velocity and a number of shapes that are used for collisions.
Example
Example
Example