Inspired by three.js and ammo.js, and driven by the fact that the web lacks a physics engine, here comes cannon.js.
Demos - Documentation - Rendering hints - NPM package
Download the library and include it in your html. Alternatively, build the library yourself (see Makefile).
<script src="cannon.js"></script>The code below creates a sphere on a plane, steps the simulation, and prints the sphere simulation to the console.
// Setup our world
var world = new CANNON.World();
world.gravity.set(0,0,-9.82);
world.broadphase = new CANNON.NaiveBroadphase();
// Create a sphere
var mass = 5, radius = 1;
var sphereShape = new CANNON.Sphere(radius);
var sphereBody = new CANNON.RigidBody(mass,sphereShape);
sphereBody.position.set(0,0,10);
world.add(sphereBody);
// Create a plane
var groundShape = new CANNON.Plane();
var groundBody = new CANNON.RigidBody(0,groundShape);
world.add(groundBody);
// Step the simulation
setInterval(function(){
world.step(1.0/60.0);
console.log("Sphere z position: " + sphereBody.position.z);
}, 1000.0/60.0);If you want to know how to use cannon.js with a rendering engine, for example Three.js, see the Examples.
| Sphere | Plane | Box | Compound | Convex¹ | Particle | |
|---|---|---|---|---|---|---|
| Sphere | Yes | Yes | Yes | Yes | Yes | Yes |
| Plane | - | - | Yes | Yes | Yes | Yes |
| Box | - | - | Yes | Yes | Yes | Yes |
| Compound | - | - | - | Yes | Yes | Yes |
| Convex¹ | - | - | - | - | Yes | Yes |
| Particle | - | - | - | - | - | - |
¹ including Cylinder
Current
- Changed API for adding forces and impulses to a body. See
RigidBody.addImpulseandRigidBody.addForce. - Removed
World.collision_matrixand instead addedWorld.collisionMatrixandWorld.collisionMatrixPrevious. They both now work with body indices instead of body IDs. - Added
SPHSystem. - Renamed
RigidBody.calculateAABBto.computeAABB. AddedRigidBody.aabbNeedsUpdate. - Added
Broadphase.useBoundingBoxes,.doBoundingBoxBroadphaseand.intersectionTest. ConvexPolyhedronconstructor now calculates normals instead of taking them as a parameter.- Added
Body.collisionFilterGroupandBody.collisionFilterMask. - Added
GridBroadphase, though it only supportsPlaneandSpherefor now. - Removed World.temp.
- Reuse of various event objects to minimize object creation in the step loop.
- Removed unused class
ContactPoint. - Changed the signature of
Broadphase.collisionPairstoBroadphase.collisionPairs(world,pairs1,pairs2), removing the need of an array return value.
0.5.0
- Changed unit of sleep properties in
Particleto seconds instead of milliseconds, madeParticle.sleepStatepublic. (schteppe,airbaggins). - Changed property
Shape.boundingSphereRadiusto being a number, added methodShape.computeBoundingSphereRadiusand.boundingSphereRadiusNeedsUpdate - Removed
Box.getCorners - Added properties to
ContactMaterial:.contactEquationStiffness,.contactEquationRegularizationTime,.frictionEquationStiffness,.frictionEquationRegularizationTimeto be able to control settings for the on-the-fly created contact constraints - Renamed the solver parameter "damping" to "regularizationTime", since it is a more correct name.
- Solver parameters (stiffness, damping etc) were moved from
SolvertoEquation. Now you control the solver parameters per constraint instead of globally. - Added
HingeConstraintand itsConstraintbase class - Added contact support for all possible
Shape.types(see table above). - Fixed convex contact bugs.
- Added method
ConvexPolyhedron.getAveragePointLocal. - Added method
ConvexPolyhedron.transformAllPoints. - Added
SplitSolver. - Removed use of typed arrays, since they are slower than ordinary ones.
- Corrected applying of linear and angular damping, should now be physically correct and independent of timestep size.
- Renamed
SolvertoGSSolver, madeSolvera base class instead. - Added method
Mat3.setTrace ContactGeneratornow producesContactEquationinstead ofContactPoint- Added property
Solver.tolerance - Changed default
Solverparameter values - Improved
Solveralgorithm, the parameters.a,.b,.k,.d,.epsdo not have the same effect anymore. - Rewrote
Solver,EquationandConstrainttotally, broke backward compatibility. - Added property
World.enableImpulses- still an experimental feature - Added
PointToPointConstraint - Added
Cylinder. - Added method
RigidBody.applyImpulse - Added "iterator" method
Box.forEachWorldCorner - Added "abstract method"
Shape.calculateWorldAABBand implemented it in subclasses. - Removed
Plane.normalin favor ofRigidBody.quaternion. One way to rotate a plane is enough.
0.4.3
Worldnow dispatches "preStep" and "postStep" events.- Introduced
BodyandParticle. New inheritance:Body->Particle->RigidBody. - Added
Quaternion.toAxisAngle() - Added
Ray. Basic hit testing forConvexPolyhedra. RigidBodynow dispatches the following events:"collide","sleep","sleepy","wakeup"- Added
Solver.setSpookParams(k,d)and removed SPOOK param things fromWorld. - Sleep functionality for
RigidBody
0.4.2 2012-08-06
- Code seem stable enough to start a change log.
The simpler todos are marked with @todo in the code. Github Issues can and should also be used for todos.
Create an issue on here if you need help.