To create a physics simulation, you need to instantiate a p2.World, create p2.Body objects, add shapes (like p2.Circle or p2.Plane) to those bodies, and add the bodies to the world. To animate the simulation, call world.step() within an animation loop using a fixed time step.
// Create a physics world, where bodies and constraints live
var world = new p2.World({
gravity:[0, -9.82]
});
// Create an empty dynamic body
var circleBody = new p2.Body({
mass: 5,
position: [0, 10]
});
// Add a circle shape to the body
var circleShape = new p2.Circle({ radius: 1 });
circleBody.addShape(circleShape);
// ...and add the body to the world.
// If we don't add it to the world, it won't be simulated.
world.addBody(circleBody);
// Create an infinite ground plane body
var groundBody = new p2.Body({
mass: 0 // Setting mass to 0 makes it static
});
var groundShape = new p2.Plane();
groundBody.addShape(groundShape);
world.addBody(groundBody);
// To animate the bodies, we must step the world forward in time, using a fixed time step size.
// The World will run substeps and interpolate automatically for us, to get smooth animation.
var fixedTimeStep = 1 / 60; // seconds
var maxSubSteps = 10; // Max sub steps to catch up with the wall clock
var lastTime;
// Animation loop
function animate(time){
requestAnimationFrame(animate);
// Compute elapsed time since last render frame
var deltaTime = lastTime ? (time - lastTime) / 1000 : 0;
// Move bodies forward in time
world.step(fixedTimeStep, deltaTime, maxSubSteps);
// Render the circle at the current interpolated position
renderCircleAtPosition(circleBody.interpolatedPosition);
lastTime = time;
}
// Start the animation loop
requestAnimationFrame(animate);