• Stars
    star
    4,584
  • Rank 8,835 (Top 0.2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 12 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A lightweight 3D physics engine written in JavaScript.

cannon.js

Lightweight 3D physics for the web

Inspired by three.js and ammo.js, and driven by the fact that the web lacks a physics engine, here comes cannon.js. The rigid body physics engine includes simple collision detection, various body shapes, contacts, friction and constraints.

Demos - Documentation - Rendering hints - NPM package - CDN

Browser install

Just include cannon.js or cannon.min.js in your html and you're done:

<script src="cannon.min.js"></script>

Node.js install

Install the cannon package via NPM:

npm install --save cannon

Alternatively, point to the Github repo directly to get the very latest version:

npm install --save schteppe/cannon.js

Example

The sample code below creates a sphere on a plane, steps the simulation, and prints the sphere simulation to the console. Note that Cannon.js uses SI units (metre, kilogram, second, etc.).

// Setup our world
var world = new CANNON.World();
world.gravity.set(0, 0, -9.82); // m/sΒ²

// Create a sphere
var radius = 1; // m
var sphereBody = new CANNON.Body({
   mass: 5, // kg
   position: new CANNON.Vec3(0, 0, 10), // m
   shape: new CANNON.Sphere(radius)
});
world.addBody(sphereBody);

// Create a plane
var groundBody = new CANNON.Body({
    mass: 0 // mass == 0 makes the body static
});
var groundShape = new CANNON.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;
})();

If you want to know how to use cannon.js with a rendering engine, for example Three.js, see the Examples.

Features

  • Rigid body dynamics
  • Discrete collision detection
  • Contacts, friction and restitution
  • Constraints
    • PointToPoint (a.k.a. ball/socket joint)
    • Distance
    • Hinge (with optional motor)
    • Lock
    • ConeTwist
  • Gauss-Seidel constraint solver and an island split algorithm
  • Collision filters
  • Body sleeping
  • Experimental SPH / fluid support
  • Various shapes and collision algorithms (see table below)
Sphere Plane Box Convex Particle Heightfield Trimesh
Sphere Yes Yes Yes Yes Yes Yes Yes
Plane - - Yes Yes Yes - Yes
Box - - Yes Yes Yes Yes (todo)
Cylinder - - Yes Yes Yes Yes (todo)
Convex - - - Yes Yes Yes (todo)
Particle - - - - - (todo) (todo)
Heightfield - - - - - - (todo)
Trimesh - - - - - - -

Todo

The simpler todos are marked with @todo in the code. Github Issues can and should also be used for todos.

Help

Create an issue if you need help.

More Repositories

1

p2.js

JavaScript 2D physics library
JavaScript
2,607
star
2

poly-decomp.js

Decompose 2D polygons into convex pieces.
JavaScript
416
star
3

gpu-physics.js

GPGPU physics for Three.js
JavaScript
280
star
4

ammo.js-demos

Demo application base class and 3D physics demos based on ammo.js. Support for several scenegraphs including Three.js and SceneJS.
JavaScript
114
star
5

imgui-wasm

imgui/wasm boilerplate
C++
42
star
6

cartridge.js

HTML5 retro game engine
JavaScript
38
star
7

codegif

Use Canvas API to make a gif animation
JavaScript
36
star
8

motionblur

WebGL shader effect
JavaScript
34
star
9

remote-physics

A node.js app that runs a physics engine and lets clients download physics data in real time.
JavaScript
24
star
10

occlusion-culling.js

Occlusion culling tests using three.js
JavaScript
20
star
11

vehicle-editor

A goofy vehicle editor made using Cannon.js physics and Goo Create.
JavaScript
19
star
12

floatcompress.js

Simple library for doing lossy compress of floats. Can be used to pack floats into a typed array when high precision is not needed.
JavaScript
16
star
13

goo-cannon-softbody

Experimenting with soft body simulation using Cannon.js in Goo Create
JavaScript
16
star
14

physicstoy

2D physics editor
JavaScript
12
star
15

voronoi-cube

Exploding cube in WebGL/GooEngine using 3D Voronoi tessellation data. (Goofy Day project)
JavaScript
12
star
16

sweep-and-prune

2D collision detection for AABBs
HTML
11
star
17

doc.js

Web based on-the-fly documentation generator targeted for JavaScript.
JavaScript
11
star
18

gpu-springs

GPU accelerated spring simulation
JavaScript
9
star
19

smart-signal-routing

Orthogonal connector routing for use in interactive diagram editors
JavaScript
8
star
20

m.js

JavaScript matrix math library that uses typed arrays.
JavaScript
7
star
21

velvet-drop

GPU cloth simulation (a.k.a Velvet Drop) made in Goo Create
JavaScript
6
star
22

tinygoon

8-bit style WebGL multiplayer game using the GamePad API
JavaScript
5
star
23

CreateForge

Visual shader editor for Goo Engine
HTML
5
star
24

dansa

Dance game for the web
JavaScript
5
star
25

taptruck

Mobile 2D WebGL game. Drive the truck to the goal without tipping over.
JavaScript
5
star
26

jox

Source code documentation generator for JavaScript
JavaScript
5
star
27

hyper-cube

Spinning Cube made with Goo Create
JavaScript
4
star
28

goo-p2-platformer

Simple physics based platformer scene built using Goo Create and engine.
JavaScript
4
star
29

refract

2D WebGL puzzle game. Guide the laser beam to the goal.
JavaScript
4
star
30

goo-bunnymark

Tribute to Pixi.js bunnymark. This time in 3D, made with Goo Engine.
HTML
3
star
31

multibody-xml

An XML markup language for descibing a physical multibody system.
3
star
32

bomb-sorting-game

Sort the boxes into their containers!
JavaScript
3
star
33

fuffrboxing

3D WebGL mobile game made for Fuffr
JavaScript
2
star
34

schteppe.github.com

2
star
35

node-mysql-querycache

Query caching to relax your MySQL database. Layer on top of node-mysql.
JavaScript
1
star
36

wasm_test

WebAssembly
1
star
37

ragdoll-goon

Almost-working goofy project. Need to be fixed!
JavaScript
1
star
38

goon-yoga

Yoga for the Goon. Built with Goo Engine.
JavaScript
1
star
39

fb-node-issues

Public Issue tracker
1
star