Constructor for a p2-es World
options for creating the world
Accumulator for the world
Enable to automatically apply body damping each step.
true
Enable to automatically apply gravity each step.
true
Enable to automatically apply spring forces each step.
true
All bodies in the world. To add a body to the world, use addBody.
The broadphase algorithm to use.
User-added constraints.
The ContactMaterials added to the World.
The default contact material to use, if no contact material was set for the colliding materials.
Dummy default material in the world, used in .defaultContactMaterial
Disabled body collision pairs. See disableBodyCollision.
Set to true if you want to the world to emit the "impact" event. Turning this off could improve performance.
true
Impact event will be removed. Use beginContact instead.
Gravity to use when approximating the friction max force (mumassgravity).
Gravity in the world. This is applied on all bodies in the beginning of each step().
True if any bodies are not sleeping, false if every body is sleeping.
Whether to enable island splitting. Island splitting can be an advantage for both precision and performance.
false
For keeping track of what time step size we used last step
The narrowphase to use to generate contacts.
Overlap keeper for the world
How to deactivate bodies during simulation. Possible modes are: ,NO_SLEEPING, BODY_SLEEPING and ISLAND_SLEEPING. If sleeping is enabled, you might need to wakeUp the bodies if they fall asleep when they shouldn't. If you want to enable sleeping in the world, but want to disable it for a particular body, see allowSleep.
World.NO_SLEEPING
Enable/disable constraint solving in each step.
true
The solver used to satisfy constraints and contacts. Default is GSSolver.
All springs in the world. To add a spring to the world, use addSpring.
Is true during step().
World time.
If the length of .gravity is zero, and .useWorldGravityAsFrictionGravity=true, then switch to using .frictionGravity for friction instead. This fallback is useful for gravityless games.
true
Set to true if you want .frictionGravity to be automatically set to the length of .gravity.
true
Static
BODY_Deactivate individual bodies if they are sleepy.
Static
ISLAND_Deactivates bodies that are in contact, if all of them are sleepy. Note that you must enable islandSplit for this to work.
Static
NO_Never deactivate bodies.
Add a body to the simulation. Note that you can't add a body during step: you have to wait until after the step (see the postStep event). Also note that bodies can only be added to one World at a time.
var world = new World(),
body = new Body();
world.addBody(body);
Add a constraint to the simulation. Note that both bodies connected to the constraint must be added to the world first. Also note that you can't run this method during step.
var constraint = new LockConstraint(bodyA, bodyB);
world.addConstraint(constraint);
Add a ContactMaterial to the simulation.
Add a spring to the simulation. Note that this operation can't be done during step.
Emit an event.
The self object, for chainability.
emitter.emit({
type: 'myEvent',
customData: 123
});
Get a contact material given two materials
Use faster hash map to lookup from material id's
Check if an event listener is added
Optional
listener: ((e) => void)Test if a world point overlaps bodies
Array of bodies that overlap the point
Should use an api similar to the raycast function
Should probably implement a .containsPoint method for all shapes. Would be more efficient
Should use the broadphase
Returning the hit shape would be fine - it carries a reference to the body now
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!');
});
Ray cast against all bodies in the world.
true if any body was hit
var ray = new Ray({
mode: Ray.ALL,
from: [0, 0],
to: [10, 0],
callback: function(result){
// Print some info about the hit
console.log('Hit body and shape: ', result.body, result.shape);
// Get the hit point
var hitPoint = vec2.create();
result.getHitPoint(hitPoint, ray);
console.log('Hit point: ', hitPoint[0], hitPoint[1], ' at distance ' + result.getHitDistance(ray));
// If you are happy with the hits you got this far, you can stop the traversal here:
result.stop();
}
});
var result = new RaycastResult();
world.raycast(result, ray);
Remove a body from the simulation. Note that bodies cannot be removed during step (for example, inside the beginContact event). In that case you need to wait until the step is done (see the postStep event).
Also note that any constraints connected to the body must be removed before the body.
var removeBody;
world.on("beginContact",function(event){
// We cannot remove the body here since the world is still stepping.
// Instead, schedule the body to be removed after the step is done.
removeBody = body;
});
world.on("postStep",function(event){
if(removeBody){
// Safely remove the body from the world.
world.removeBody(removeBody);
removeBody = null;
}
});
Removes a constraint. Note that you can't run this method during step.
Removes a contact material
Remove a spring. Note that this operation can't be done during step.
Step the physics world forward in time.
There are two modes. The simple mode is fixed timestepping without interpolation. In this case you only use the first argument. The second case uses interpolation. In that you also provide the time since the function was last used, as well as the maximum fixed timesteps to take.
The fixed time step size to use.
Optional
timeSinceLastCalled: numberThe time elapsed since the function was last called.
Maximum number of fixed steps to take per function call.
// Simple fixed timestepping without interpolation
var fixedTimeStep = 1 / 60;
var world = new World();
var body = new Body({ mass: 1 });
world.addBody(body);
function animate(){
requestAnimationFrame(animate);
world.step(fixedTimeStep);
renderBody(body.position, body.angle);
}
// Start animation loop
requestAnimationFrame(animate);
// Fixed timestepping with interpolation
var maxSubSteps = 10;
var lastTimeSeconds;
function animate(time){
requestAnimationFrame(animate);
var timeSeconds = time / 1000;
if(lastTimeSeconds){
var deltaTime = timeSeconds - lastTimeSeconds;
world.step(fixedTimeStep, deltaTime, maxSubSteps);
}
lastTimeSeconds = timeSeconds;
renderBody(body.interpolatedPosition, body.interpolatedAngle);
}
// Start animation loop
requestAnimationFrame(animate);
http://bulletphysics.org/mediawiki-1.5.8/index.php/Stepping_The_World
The dynamics world, where all bodies and constraints live.
Example