Dapao is just a Typescript port of the cannon.js library with some added tests.
I am porting this to Typescript so I can use it within my own game engine (which is in Typescript). While the cannon.js version works perfectly well, I want to have better integration with my engine (tree shaking, etc), and I eventually want to be able to compile this to webassembly or maybe even native - things Javascript can't do at the moment.
If you want to use the library, you can either reference this repo directly from within your webpack config:
...
"dependencies": {
"dapao": "https://github.com/TheRohans/dapao.git#master",
"document-register-element": "^1.7.0"
...
},
...
Or clone the repo and reference it from the local file system:
...
"dependencies": {
"dapao": "file:../dapao",
"document-register-element": "^1.7.0"
...
},
...
- Clone this repository
- Install node.js
- Run
npm install
to install all of the dependencies - Run
npm run build
to build DaPao
For deeper examples, I recommend you checkout the cannon.js project (cannon.js), but here is a very basic example:
The sample code below creates a sphere on a plane, steps the simulation, and prints the sphere simulation to the console. Note that DaPao (and Cannon.js) uses SI units (metre, kilogram, second, etc.).
import * as DaPao from 'dapao';
// Setup our world
var world = new DaPao.World();
world.gravity.set(0, 0, -9.82); // m/s²
// Create a sphere
var radius = 1; // m
var sphereBody = new DaPao.Body({
mass: 5, // kg
position: new DaPao.Vec3(0, 0, 10), // m
shape: new DaPao.Sphere(radius)
});
world.addBody(sphereBody);
// Create a plane
var groundBody = new DaPao.Body({
mass: 0 // mass == 0 makes the body static
});
var groundShape = new DaPao.Plane();
groundBody.addShape(groundShape);
world.addBody(groundBody);
var fixedTimeStep = 1.0 / 60.0; // seconds
var maxSubSteps = 3;
// Start the simulation loop
var lastTime;
(function simloop(time){
requestAnimationFrame(simloop);
if(lastTime !== undefined){
var dt = (time - lastTime) / 1000;
world.step(fixedTimeStep, dt, maxSubSteps);
}
console.log("Sphere z position: " + sphereBody.position.z);
lastTime = time;
})();
- Rigid body dynamics
- Discrete collision detection
- Contacts, friction and restitution
- Collision filters
- Body sleeping
- Various shapes and collision algorithms (see table below)
Sphere | Plane | Box | Convex | Particle | Heightfield | Trimesh | |
---|---|---|---|---|---|---|---|
Sphere | Yes | Yes | Yes | Yes | NT | TD | TD |
Plane | - | - | Yes | Yes | NT | - | TD |
Box | - | - | Yes | BU | NT | TD | TD |
Cylinder | - | - | NT | NT | NT | TD | TD |
Convex | - | - | - | BU | NT | TD | TD |
Particle | - | - | - | - | - | TD | TD |
Heightfield | - | - | - | - | - | - | TD |
Trimesh | - | - | - | - | - | - | - |
- NT = Not tested, but code exists. Feedback welcome.
- BU = Buggy
- TD = Todo