Security News
PyPI Introduces Digital Attestations to Strengthen Python Package Security
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
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
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.
Sphere | Plane | Box | Convex | Particle | Heightfield | |
---|---|---|---|---|---|---|
Sphere | Yes | Yes | Yes | Yes | Yes | Yes |
Plane | - | - | Yes | Yes | Yes | - |
Box | - | - | Yes | Yes | Yes | Yes |
Cylinder | - | - | Yes | Yes | Yes | Yes |
Convex | - | - | - | Yes | Yes | Yes |
Particle | - | - | - | - | - | (todo) |
Heightfield | - | - | - | - | - | - |
The simpler todos are marked with @todo
in the code. Github Issues can and should also be used for todos.
Create an issue on here if you need help.
FAQs
A lightweight 3D physics engine written in JavaScript.
The npm package cannon receives a total of 1,718 weekly downloads. As such, cannon popularity was classified as popular.
We found that cannon demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.