Skip to content
forked from schteppe/cannon.js

A lightweight 3D physics engine written in Typescript.

License

Notifications You must be signed in to change notification settings

TheRohans/dapao

 
 

Repository files navigation

大炮 (Dàpào)

Maintainability

Test Coverage

Lightweight 3D Physics for the Web and Beyond

Dapao is just a Typescript port of the cannon.js library with some added tests.

Project Goals

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.

Using the library

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"
  ...
},
...

Contributing

  1. Clone this repository
  2. Install node.js
  3. Run npm install to install all of the dependencies
  4. Run npm run build to build DaPao

Example

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;
})();

Features

  • 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

About

A lightweight 3D physics engine written in Typescript.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 49.9%
  • JavaScript 37.2%
  • Objective-J 12.9%