GithubHelp home page GithubHelp logo

therohans / dapao Goto Github PK

View Code? Open in Web Editor NEW

This project forked from schteppe/cannon.js

16.0 9.0 0.0 7.16 MB

A lightweight 3D physics engine written in Typescript.

License: MIT License

JavaScript 37.18% Makefile 0.02% Shell 0.01% TypeScript 49.87% Objective-J 12.91%
physics-engine typescript

dapao's Introduction

大炮 (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

dapao's People

Contributors

brianpeiris avatar chrisforbes avatar elisee avatar fjavierv avatar groe avatar jgottero avatar mrdoob avatar onionhammer avatar oxyflour avatar rcpowers avatar recoshet avatar robrohan avatar schteppe avatar swift502 avatar tbo avatar timrwood avatar tong avatar wivlaro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.