Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

cannon

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cannon

A lightweight 3D physics engine written in JavaScript.

  • 0.6.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.2K
increased by4.9%
Maintainers
1
Weekly downloads
 
Created
Source

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

Usage

Optionally, start by building the library using Grunt.

Include build/cannon.js in your html:

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

Then you can start experimenting.

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²
world.broadphase = new CANNON.NaiveBroadphase();

// Create a sphere
var radius = 1; // m
var sphereBody = new CANNON.Body({
   mass: 5 // kg
});
var sphereShape = new CANNON.Sphere(radius);
sphereBody.addShape(sphereShape);
sphereBody.position.set(0,0,10); // m
world.add(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.add(groundBody);

// Step the simulation
setInterval(function(){
  var timeStep = 1.0/60.0; // seconds
  world.step(timeStep);
  console.log("Sphere z position: " + sphereBody.position.z);
}, 1000.0/60.0);

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

Features

  • Rigid body physics
  • Collision detection (no CCD)
  • Contacts with friction and restitution
  • Constraints
    • PointToPoint (also called balljoint)
    • Distance
    • Hinge (with optional motor)
  • Gauss-Seidel constraint solver and an island split algorithm
  • Collision filters
  • Body motion states (dynamic, kinematic, static)
  • Body sleeping
  • Experimental SPH / fluid support
  • Various shapes and collisions (see table below)
SpherePlaneBoxConvexParticleHeightfield
SphereYesYesYesYesYesYes
Plane--YesYesYes-
Box--YesYesYesYes
Cylinder--YesYesYesYes
Convex---YesYesYes
Particle-----(todo)
Heightfield------

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 on here if you need help.

Keywords

FAQs

Package last updated on 31 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc