Table Of Contents

Previous topic

18.11. The Shape Object

Next topic

19. Low Level 3D Physics API

This Page

18.12. The World Object

18.12.1. Constructor

A World object can be constructed with Physics2DDevice.createWorld.

18.12.2. Methods

18.12.2.1. step

Summary

Step forwards in simulation time.

Syntax

world.step(deltaTime);
deltaTime

The amount of time (in seconds) to be simulated.

This value must be strictly positive.

This methods performs a single simulation step regardless of the input deltaTime.

To use a fixed time step in your simulations, but remain flexible to differing run-time performance (Assuming physics is not the bottleneck), you would use a system similar to:

// executed each frame
while (world.simulatedTime < realTime)
{
    world.step(fixedTimeStep);
}

18.12.2.2. getGravity

Summary

Retrieve gravity set on world.

Syntax

var gravity = world.getGravity();
// or
world.getGravity(gravity);
gravity (Optional)

If specified then the gravity of the world will be stored into this array. Otherwise a new array will be created.

Modifications to the return value of this function will not effect the space. Setting gravity must be done through the setGravity method.

18.12.2.3. setGravity

Summary

Set gravity on world.

Syntax

world.setGravity(gravity);
gravity
New value for gravity of world.

18.12.2.4. addRigidBody

Summary

Add a RigidBody to the simulation world.

Syntax

var success = world.addRigidBody(body);
body
The RigidBody to add to the world

This method will fail if the body is already in a World.

18.12.2.5. removeRigidBody

Summary

Remove a RigidBody from the simulation world.

Any Constraint objects in the world which make use of the body will also be removed.

Syntax

var success = world.removeRigidBody(body);
body
The RigidBody to remove from the world

This method will fail if the body is not in this World.

18.12.2.6. addConstraint

Summary

Add a Constraint to the simulation world.

Syntax

var success = world.addConstraint(constraint);
constraint
The Constraint to add to the world

This method will fail if the constraint is already in a World.

18.12.2.7. removeConstraint

Summary

Remove a Constraint from the simulation world.

Syntax

var success = world.removeConstraint(constraint);
constraint
The Constraint to remove from the world

This method will fail if the constraint is not in this World.

18.12.2.8. clear

Summary

Clear the simulation world of all rigid bodies and constraints.

Syntax

world.clear();

18.12.2.9. shapeRectangleQuery

Summary

Sample world to find all Shape‘s intersecting the given axis aligned rectangle.

Syntax

var store = [];
var count = world.shapeRectangleQuery(rectangle, store);
rectangle
The rectangle in Physics2D coordinates to sample Shapes.
store
The array in which to store intersected shapes.

The return value count is the number of shapes which were intersected.

18.12.2.10. bodyRectangleQuery

Summary

Sample world to find all RigidBody‘s intersecting the given axis aligned rectangle.

Syntax

var store = [];
var count = world.bodyRectangleQuery(rectangle, store);
rectangle
The rectangle in Physics2D coordinates to sample rigid bodies.
store
The array in which to store intersected bodies.

The return value count is the number of bodies which were intersected.

18.12.2.11. shapeCircleQuery

Summary

Sample world to find all Shape‘s intersecting the given circle.

Syntax

var store = [];
var count = world.shapeCircleQuery(center, radius, store);
center
The point in Physics2D coordinates defining center of the circle.
radius
The radius in Physics2D coordinates for sample circle.
store
The array in which to store intersected shapes.

The return value count is the number of shapes which were intersected.

18.12.2.12. bodyCircleQuery

Summary

Sample world to find all RigidBody‘s intersecting the given point.

Syntax

var store = [];
var count = world.bodyCircleQuery(center, radius, store);
center
The point in Physics2D coordinates defining center of the circle.
radius
The radius in Physics2D coordinates for sample circle.
store
The array in which to store intersected bodies.

The return value count is the number of bodies which were intersected.

18.12.2.13. shapePointQuery

Summary

Sample world to find all Shape‘s intersecting the given point.

Syntax

var store = [];
var count = world.shapePointQuery(point, store);
point
The point in Physics2D coordinates to sample Shapes.
store
The array in which to store intersected shapes.

The return value count is the number of shapes which were intersected.

18.12.2.14. bodyPointQuery

Summary

Sample world to find all RigidBody‘s intersecting the given point.

Syntax

var store = [];
var count = world.bodyPointQuery(point, store);
point
The point in Physics2D coordinates to sample rigid bodies.
store
The array in which to store intersected bodies.

The return value count is the number of bodies which were intersected.

18.12.2.15. rayCast

Summary

Sample world for the first intersection of the given parametric ray.

Syntax

var ray = {
    origin : [-1, 0],
    direction : [10, 0],
    maxFactor : 2
};
var callback = {
    ignored : someShape,
    filter : function(ray, temporaryResult)
    {
        if (this.ignored === temporaryResult.shape)
        {
            return false;
        }

        if (temporaryResult.hitNormal[1] > 0.5)
        {
            return false;
        }

        return true;
    }
};

var result = world.rayCast(ray, ignoreInnerSurfaces, callback.filter, callback);
if (result !== null)
{
    console.log("Ray intersected!");
    console.log("Distance to intersection = " + (result.factor / mathDevice.v2Length(ray.direction)));
}
ray
Parametric ray to be cast through the world. Ray cast will be limited to a factor of maxFactor of the ray direction.
ignoreInnerSurfaces (Optional)

If true, then intersections with the inner surfaces of Shapes will be ignored.

Default value is false.

callback (Optional)

If supplied, this function will be called after each intersection test with the input ray and a temporary results object detailing the intersection.

This result object is re-used and you should not keep any references to it.

Any intersection for which the callback function returns false, will be ignored.

thisObject (Optional)
If supplied, the callback function supplied will be called with thisObject as its this value.

The return value of this function is a new results result object detailing the closest intersection to the ray.

This results object has the following fields:

{
    hitPoint : [x, y],  // point of intersection
    hitNormal : [x, y], // normal at intersection on intersected shape.
    shape : intersectedShape,
    factor : // factor corresponding to ray intersection
}

18.12.2.16. convexCast

Summary

Sample world for the first intersection of the given Shape as determined by its RigidBody‘s velocities.

Syntax

var sweepShape = phys2D.createCircleShape({
    radius : 1
});
var sweepBody = phys2D.createRigidBody({
    shapes : [circle],
    position : [-1, 0],
    velocity : [100, 0],
    angularVelocity : 20
});

var callback = {
    ignored : someShape,
    filter : function(shape, temporaryResult)
    {
        if (this.ignored === temporaryResult.shape)
        {
            return false;
        }

        if (temporaryResult.hitNormal[1] > 0.5)
        {
            return false;
        }

        return true;
    }
};

var maxTime = 2; // seconds
var result = world.convexCast(sweepShape, maxTime, callback.filter, callback);
if (result !== null)
{
    console.log("Shape intersected!");
    console.log("Time of Impact = " + result.factor);
}
shape

The Shape to be swept through the world. This shape must belong to a RigidBody which defines the sweep start position/rotation and sweep velocities.

This shape/body pair is permitted belong to the world, in which case it will ignore itself automatically.

deltaTime
The amount of time shape will be swept through before returning failure.
callback (Optional)

If supplied, this function will be called after each intersection test with the input shape and a temporary results object detailing the intersection.

This result object is re-used and you should not keep any references to it.

Any intersection for which the callback function returns false, will be ignored.

thisObject (Optional)
If supplied, the callback function supplied will be called with thisObject as its this value.

The return value of this function is a new results result object detailing the first intersection of swept shape in the world.

This results object has the same fields as that of the rayCast method.

18.12.3. Properties

18.12.3.1. simulatedTime

The amount of time in seconds that has been simulated since world creation.

Note

Read Only

18.12.3.2. timeStamp

The current time stamp for this World: equal to the number of times step() has been executed.

Note

Read Only

18.12.3.3. rigidBodies

Array of all RigidBody that are in the World.

Removing, or adding body from the world will modify this array, and should not be performed during iteration. If you wish to remove all rigid bodies from the world you may use the following pattern:

var rigidBodies = world.rigidBodies;
while (rigidBodies.length !== 0)
{
    world.removeRigidBody(rigidBodies[0]);
}

Note

Read Only

18.12.3.4. constraints

Array of all Constraint that are in the World.

Removing, or adding constraint from the world will modify this array, and should not be performed during iteration. If you wish to remove all constraints from the world you may use the following pattern:

var constraints = world.constraints;
while (constraints.length !== 0)
{
    world.removeConstraint(constraints[0]);
}

Note

Read Only

18.12.3.5. liveDynamics

Array of all non-sleeping dynamic type Rigid Bodies that are in the World.

Dynamic bodies are put to sleep when the island of objects formed by contacts with other dynamic bodies, and constraints are all sufficiently slow moving for a sufficient amount of time.

Any operation that causes a RigidBody or Constraint to be woken, or forced to sleep may modify this array, you should be carefully not to perform any such mutation of any such objects in the world during iteration.

Note

Read Only

18.12.3.6. liveKinematics

Array of all non-sleeping kinematic type Rigid Bodies that are in the World.

Kinematic bodies are put to sleep when they have not moved during a world step().

Any operation that causes a RigidBody or Constraint to be woken, or forced to sleep may modify this array, you should be carefully not to perform any such mutation of any such objects in the world during iteration.

Note

Read Only

18.12.3.7. liveConstraints

Array of all non-sleeping Constraints that are in the World.

Constraints are put to sleep when the island of dynamic bodies they are connected to is put to sleep.

Any operation that causes a RigidBody or Constraint to be woken, or forced to sleep may modify this array, you should be carefully not to perform any such mutation of any such objects in the world during iteration.

Note

Read Only

18.12.3.8. broadphase

The Broadphase object assigned to this World.

You should not modify the broadphase object, though you are free to query it.

Note

Read Only

18.12.3.9. velocityIterations

The number of iterations used in the physics step when solving errors in velocity constraints.

This value must be positive.

18.12.3.10. positionIterations

The number of iterations used in the physics step when solving errors in position constraints.

This value must be positive.

18.12.3.11. dynamicArbiters

Set of all non-sleeping Arbiter objects between pairs of dynamic rigid bodies.

If iterating over this array, you should be careful to ignore any Arbiter object whose active field is false. Such objects correspond either to an interaction which has recently ended and exists purely to cache values that may shortly be re-used, or that is waiting to be destroyed.

var arbiters = world.dynamicArbiters;
var numArbiters = arbiters.length;
var i;
for (i = 0; i < numArbiters; i += 1)
{
    var arb = arbiters[i];
    if (!arb.active)
    {
        continue;
    }
    ...
}

This array may be modified by removing a RigidBody object from the World and should be avoided during iteration.

Note

Read Only

18.12.3.12. staticArbiters

Set of all non-sleeping Arbiter objects between a dynamic, and non-dynamic rigid body.

If iterating over this array, you should be careful to ignore any Arbiter object whose active field is false. Such objects correspond either to an interaction which has recently ended and exists purely to cache values that may shortly be re-used, or that is waiting to be destroyed.

var arbiters = world.staticArbiters;
var numArbiters = arbiters.length;
var i;
for (i = 0; i < numArbiters; i += 1)
{
    var arb = arbiters[i];
    if (!arb.active)
    {
        continue;
    }
    ...
}

This array may be modified by removing a RigidBody object from the World and should be avoided during iteration.

Note

Read Only