Rigid bodies come in 3 different types:
- ‘static’: This type of body, once added to a World may no longer be mutated in any way (Changing position, changing shapes) except where documented.
- ‘kinematic’: This type of body is equivalent to a static type, except that it is permitted to move (And will always move to exactly where it is told). Kinematic bodies should be moved by modifying their velocity as changing position directly will be equivalent to teleporting the body.
- ‘dynamic’: This type of body has non-infinite mass/inertia (Unless explicitly set as such) and is under the influence of gravity, collision and constraint impulses.
A RigidBody object can be constructed with Physics2DDevice.createRigidBody.
Summary
Returns true, if body is of a dynamic type.
Syntax
var isDynamic = body.isDynamic();
Summary
Returns true, if body is of a kinematic type.
Syntax
var isKinematic = body.isKinematic();
Summary
Returns true, if body is of a static type.
Syntax
var isStatic = body.isStatic();
Summary
Change rigid body type to dynamic. This operation can be made at any time except for during a simulation step.
Syntax
body.setAsDynamic();
Summary
Change rigid body type to kinematic. This operation can be made at any time except for during a simulation step.
Syntax
body.setAsKinematic();
Summary
Change rigid body type to static. This operation can be made at any time except for during a simulation step.
This method will also set the velocity and angularVelocity of the rigid body to 0 as static bodies are not permitted to have velocity.
Syntax
body.setAsStatic();
Summary
Retrieve the current position of this rigid body.
Syntax
var position = body.getPosition();
// or
body.getPosition(position);
Summary
Set the position of this rigid body.
This method will be ignored for static type bodies which are in a World as well as if it is made during a simulation step.
Syntax
body.setPosition(position);
Summary
Retrieve the current rotation of this rigid body.
Syntax
var rotation = body.getRotation();
Summary
Set the rotation of this rigid body.
This method will be ignored for static type bodies which are in a World as well as if it is made during a simulation step.
Syntax
body.setRotation(rotation)
Summary
Retrieve the current velocity of this rigid body.
Syntax
var velocity = body.getVelocity();
// or
body.getVelocity(velocity);
Summary
Set the velocity of this rigid body.
This method will be ignored for static type bodies which are in a World.
Syntax
body.setVelocity(velocity);
Summary
Retrieve the current angular velocity of this rigid body.
Syntax
var angularVelocity = body.getAngularVelocity();
Summary
Set the angular velocity of this rigid body.
This method will be ignored for static type bodies which are in a World.
Syntax
body.setAngularVelocity(angularVelocity)
Summary
Retrieve the current force of this rigid body.
Syntax
var force = body.getForce();
// or
body.getForce(force);
Summary
Set the force of this rigid body.
Static bodies may have their force changed even if they are in a World.
Syntax
body.setForce(force);
Summary
Retrieve the current torque of this rigid body.
Syntax
var torque = body.getTorque();
Summary
Set the torque of this rigid body.
Static bodies may have their torque changed even if they are in a World.
Syntax
body.setTorque(torque)
Summary
Retrieve the current surface velocity of this rigid body.
Syntax
var velocity = body.getSurfaceVelocity();
// or
body.getSurfaceVelocity(velocity);
Summary
Set the surface velocity of this rigid body.
Static bodies are permitted to have their surface velocity changed even if they are inside of a World.
Syntax
body.setSurfaceVelocity(velocity);
Summary
Get the mass of this rigid body. This mass is equal to that set in body constructor, even if body is static or kinematic.
Syntax
var mass = body.getMass();
Summary
Set the mass of this rigid body.
The mass of a static/kinematic body may be set at any time, but it will make no effect on the physics unless that body is later set as dynamic.
Syntax
body.setMass(mass);
Summary
Set body so that its mass will always be computed from its Shapes.
This is not the same as: body.setMass(body.computeMassFromShapes()), as when setting mass from shapes the mass will be recomputed should the shapes be modified.
This may be called for any object at any time.
Syntax
body.setMassFromShapes();
Summary
Get the moment of inertia for this rigid body. This inertia is equal to that set in body constructor, even if body is static or kinematic.
Syntax
var inertia = body.getInertia();
Summary
Set the moment of inertia for this rigid body.
The inertia of a static/kinematic body may be set at any time, but it will make no effect on the physics unless that body is later set as dynamic.
Syntax
body.setInertia(inertia);
Summary
Set body so that its inertia will always be computed from its Shapes.
This is not the same as: body.setInertia(body.computeInertiaFromShapes()), as when setting inertia from shapes the inertia will be recomputed should the shapes be modified.
This may be called for any object at any time.
Syntax
body.setInertiaFromShapes();
Summary
Retrieve the current value for the linear drag applied to this body.
Syntax
var linearDrag = body.getLinearDrag();
Summary
Set the linear drag on this body.
This may be set on any body type at any time though it will have an affect only on dynamic bodies.
Syntax
body.setLinearDrag(linearDrag);
The new linear drag for this body.
This value must be between 0 and 1.
Summary
Retrieve the current value for the angular drag applied to this body.
Syntax
var angularDrag = body.getAngularDrag();
Summary
Set the angular drag on this body.
This may be set on any body type at any time though it will have an affect only on dynamic bodies.
Syntax
body.setAngularDrag(angularDrag);
The new angular drag for this body.
This value must be between 0 and 1.
Summary
Add a Shape to this Body.
This method will fail should the Shape already be in a Body, or this Body is a static type body, and is part of a World or if it is made during a simulation step.
Syntax
var success = body.addShape(shape);
Summary
Remove a Shape from this Body.
This method will fail should the Shape not be in this Body, or this Body is a static type body, and is part of a World or if it is made during a simulation step.
Syntax
var success = body.removeShape(shape);
Summary
Apply an impulse to this rigid body. This has an effect only for dynamic bodies.
Syntax
body.applyImpulse(impulse, position);
The position to apply the impulse at. This position is also defined in world coordinates.
If unspecified, the rigid body’s position is used.
Summary
This function will set the velocity of a body, so that under no other influence it will move to the target position and rotation via its velocity.
This is an aid for animation of kinematic object types.
Syntax
body.setVelocityFromPosition(targetPosition, targetRotation, deltaTime);
The amount of time in which it should take the body to reach its target.
Likely, this will be the same time used in calls to space.step() in animating a kinematic object.
Summary
Transform a point defined in world coordinates, into the local coordinates of this rigid body.
Syntax
var worldPoint = body.transformWorldPointToLocal(localPoint);
// or
body.transformWorldPointToLocal(localPoint, worldPoint);
Summary
Transform a point defined in local coordinates, into the world coordinates of this rigid body.
Syntax
var localPoint = body.transformWorldPointToLocal(worldPoint);
// or
body.transformWorldPointToLocal(worldPoint, localPoint);
Summary
Transform a vector defined in world coordinates, into the local coordinates of this rigid body.
Syntax
var worldVector = body.transformWorldVectorToLocal(localVector);
// or
body.transformWorldVectorToLocal(localVector, worldVector);
Summary
Transform a vector defined in local coordinates, into the world coordinates of this rigid body.
Syntax
var localVector = body.transformWorldVectorToLocal(worldVector);
// or
body.transformWorldVectorToLocal(worldVector, localVector);
Summary
Compute the sum mass of all Shapes in this body as computed based on their area and material densities.
Syntax
var mass = body.computeMassFromShapes();
Summary
Compute the sum moment of inertia of all Shapes in this body as computed based on their material densities and profiles.
Syntax
var inertia = body.computeInertiaFromShapes();
Summary
Manually wake this RigidBody.
Waking a rigid body in Physics2D is not generally required. It is only ever required to manually wake a rigid body, if you manually put it to sleep or added it as a sleeping body.
Mutations to the body, and interactions with other bodies in the world will otherwise automatically wake the body.
Syntax
body.wake();
Summary
Manually put this RigidBody to sleep.
This body will be woken as soon as it is interacted with in a World, or is mutated in any way that would require it to wake up in normal circumstances.
Syntax
body.sleep();
Summary
Compute the center of mass of this RigidBody in its local coordinate system, as based on Shape profiles and material densities.
Syntax
var com = body.computeLocalCenterOfMass();
// or
body.computeLocalCenterOfMass(com);
Summary
Compute the axis-aligned bounding rectangle of this RigidBody in world coordinates.
This method is only well defined if this Body contains at least one shape, and otherwise will have meaningless bounds.
Syntax
var bounds = body.computeWorldBounds();
// or
body.computeWorldBounds(bounds);
Summary
Translate the Shapes of this body, in local coordinate system so that the center of mass of this body as computed by computeLocalCentreOfMass is equal to [0, 0].
It is important for a rigid body to be aligned with the origin to behave intuitively.
This method will be ignored for static type bodies which are inside of a World or if it is made during a simulation step.
Syntax
body.alignWithOrigin();
Summary
Perform an integration of this body’s position and rotation based the bodies current velocities.
This method will be ignored for static type bodies which are inside of a World or if it is made during a simulation step.
Syntax
body.integrate(deltaTime);
Summary
Add a new event listener to this Body.
Syntax
var success = body.addEventListener(eventType, handler);
One of:
Function to be called when this event occurs (Noting that events as usual are deferred until the end of world step()).
Function is called with no arguments, and with its this object as the RigidBody to which the event relates.
This function will fail, and return false if the event type was not accepted, or if the handler already exists for the given event type.
Example:
function wakeHandler() {
console.log("Body named: " + this.userData.name+" woke up!");
}
var body = phys2D.createRigidBody({
...
userData : {
name : "Box no. 1"
}
});
body.addEventListener('wake', wakeHandler);
You may add as many handlers for a given event type as you wish, and handlers will be called in the same order in which they were added.
Summary
Remove existing event listener from this Body.
Syntax
var success = body.removeEventListener(eventType, handler);
This function will fail, and return false if the event type was not accepted, or if the handler was not found on the body for the given event type.
The set of Shape objects assigned to this body.
Adding and removing shapes from this body will mutate this list and should be avoided during iteration. To remove all Shape objects from this Body, you may use the following pattern:
var shapes = body.shapes;
while (shapes.length > 0)
{
body.removeShape(shapes[0]);
}
Note
Read Only.
The set of Constraint objects making use of this body which are presently part of a simulation World.
Adding and removing constraints from the world will mutate this list and should be avoided during iteration. To remove all Constraint objects from the world which make us of this Body, you may use the following pattern:
var constraints = body.constraints;
while (constraints.length > 0)
{
world.removeConstraint(constraints[0]);
}
This action is performed automatically when removing the Body itself from the simulation world.
Note
Read Only.
Whether this body is presently sleeping. If true, and body is added to a simulation World, then it will be added as a sleeping body.
Note
Read Only.
Whether this body is marked for continuous collisions against other dynamic bodies. Due to implementation details it is advised not to create a group of bullet objects that interact together as there may be visual stalling.
Field to which you may assign whatever data you like.