A World object can be constructed with Physics2DDevice.createWorld.
Summary
Step forwards in simulation time.
Syntax
world.step(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);
}
Summary
Retrieve gravity set on world.
Syntax
var gravity = world.getGravity();
// or
world.getGravity(gravity);
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.
Summary
Set gravity on world.
Syntax
world.setGravity(gravity);
Summary
Add a RigidBody to the simulation world.
Syntax
var success = world.addRigidBody(body);
This method will fail if the body is already in a World.
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);
This method will fail if the body is not in this World.
Summary
Add a Constraint to the simulation world.
Syntax
var success = world.addConstraint(constraint);
This method will fail if the constraint is already in a World.
Summary
Remove a Constraint from the simulation world.
Syntax
var success = world.removeConstraint(constraint);
This method will fail if the constraint is not in this World.
Summary
Clear the simulation world of all rigid bodies and constraints.
Syntax
world.clear();
Summary
Sample world to find all Shape‘s intersecting the given axis aligned rectangle.
Syntax
var store = [];
var count = world.shapeRectangleQuery(rectangle, store);
The return value count is the number of shapes which were intersected.
Summary
Sample world to find all RigidBody‘s intersecting the given axis aligned rectangle.
Syntax
var store = [];
var count = world.bodyRectangleQuery(rectangle, store);
The return value count is the number of bodies which were intersected.
Summary
Sample world to find all Shape‘s intersecting the given circle.
Syntax
var store = [];
var count = world.shapeCircleQuery(center, radius, store);
The return value count is the number of shapes which were intersected.
Summary
Sample world to find all RigidBody‘s intersecting the given point.
Syntax
var store = [];
var count = world.bodyCircleQuery(center, radius, store);
The return value count is the number of bodies which were intersected.
Summary
Sample world to find all Shape‘s intersecting the given point.
Syntax
var store = [];
var count = world.shapePointQuery(point, store);
The return value count is the number of shapes which were intersected.
Summary
Sample world to find all RigidBody‘s intersecting the given point.
Syntax
var store = [];
var count = world.bodyPointQuery(point, store);
The return value count is the number of bodies which were intersected.
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)));
}
If true, then intersections with the inner surfaces of Shapes will be ignored.
Default value is false.
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.
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
}
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);
}
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.
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.
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.
The amount of time in seconds that has been simulated since world creation.
Note
Read Only
The current time stamp for this World: equal to the number of times step() has been executed.
Note
Read Only
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
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
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
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
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
The Broadphase object assigned to this World.
You should not modify the broadphase object, though you are free to query it.
Note
Read Only
The number of iterations used in the physics step when solving errors in velocity constraints.
This value must be positive.
The number of iterations used in the physics step when solving errors in position constraints.
This value must be positive.
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
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