Inspired by three.js and ammo.js, and driven by the fact that the web lacks a physics engine, here comes cannon.js.
- Lightweight - less than 50Kb compressed. For comparison: ammo.js uses 1.12Mb when compressed.
- 100% open source JavaScript, written from scratch
- Uses an iterative Gauss-Seidel solver to solve generic constraints
- Uses SPOOK for time stepping
// 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 normal = new CANNON.Vec3(0,0,1);
var groundShape = new CANNON.Plane(normal);
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);
Here is a live version.
- Collision/contacts between convexhulls and sphere
- Contact reduction
- Better collision detection - spatial hashing, octrees or similar (Continous?)
- Rename the current Solver class to GSSolver, and make the Solver class to a base class
- ParallelSolver that uses Web Workers - splits the system into islands and then adds to subsolvers (may be any other solver) - see http://www.html5rocks.com/en/tutorials/workers/basics/
- Caching of bounding sphere radius
- Better class structure for Constraints, Jacobian entries etc
- Shapes (based on ConvexHull is enough to begin with): Cone, cylinder
- Ray casting
- Constraints: PointToPoint, etc etc
- First-contact impulses
- Search for "@todo" if you want to find more things to do
Download Node.js and NPM for your platform and make sure they work. When you've cloned cannon.js, run npm install -d
in the main cannon.js directory and the tools you need will be installed (jshint, uglify-js, nodeunit etc).
When you've changed something in the cannon.js source, you must build to see the changes in the demos. See below.
When a new version of the software has been made, make a new build. Run make
in the main directory to do this. Remember to update VERSION. The software versioning should follow the Semantic Version Specification: http://semver.org/