Coder Social home page Coder Social logo

gero3 / cannon.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from schteppe/cannon.js

1.0 1.0 0.0 1.27 MB

A lightweight 3D physics engine written in JavaScript. Perfect to use with three.js, for example.

Home Page: http://schteppe.github.com/cannon.js

License: MIT License

JavaScript 97.90% Python 2.10%

cannon.js's Introduction

cannon.js - a lightweight 3D physics engine for the web

Container Boxes

All examples >>

Inspired by three.js and ammo.js, and driven by the fact that the web lacks a physics engine, here comes cannon.js.

Features

  • Lightweight - less than 50Kb compressed. For comparison: ammo.js uses 1.12Mb when compressed.
  • Supports solid spheres and static planes at the moment - soon also boxes
  • 100% open source JavaScript, written from scratch
  • Uses typed arrays for fast number crunching
  • Uses an iterative Gauss-Seidel solver to solve generic constraints
  • Uses SPOOK for time stepping

Example

// Setup our world
var world = new CANNON.World();
world.gravity(new CANNON.Vec3(0,0,-9.82));
var bp = new CANNON.BroadPhase();
world.broadphase(bp);
    
// Create a sphere
var mass = 5, radius = 1;
var sphereShape = new CANNON.Sphere(radius);
var sphereBody = new CANNON.RigidBody(mass,sphereShape);
sphereBody.setPosition(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);
  var pos = sphereBody.getPosition();
  console.log("Sphere z position: "+pos.x);
}, 1000.0/60.0);

Developer instructions

Todo

  • Box/box collision
  • Better collision detection - spatial hashing, octrees or similar
  • 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
  • Shapes: Cone, cylinder, compound
  • Search for "@todo" if you want to find more things to do

Build

When a new version of the software has been made, a new build needs to be made. Run cd cannon.js/utils/; ./build.py; to do this. The version number will be read from cannon.js/VERSION and put into the built files, so update VERSION first. The software versioning should follow the Semantic Version Specification: http://semver.org/

Documentation

The documentation is made using Doxygen-style blocks before each class, method and property.

Examples

To be able to view the examples on the web using Github Pages, the code need to be copied to that public branch. The master branch is therefore being merged into the gh-pages branch now and then, eg git checkout gh-pages; git merge master;. This way we can use file references in the examples transparently.

Profiling / optimizing the code

Use the FireBug profiling function console.profile(); before the world.step(); and console.profileEnd(); afterwards. Open FireBug and you'll see execution times for each function. All entries will be on anonymous functions, but you can always look up the corresponding function in the code. Hopefully, profiling will be integrated in the future debug renderer so it can be used by pressing a button.

cannon.js's People

Contributors

mrdoob avatar schteppe avatar

Stargazers

 avatar

Watchers

 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.