Cannon.js explained

Cannon.js
Cannon.js
Programming Language:JavaScript
Operating System:OS independent
Genre:3D physics engine
License:MIT License
Developer:Stefan Hedman
Latest Release Version:0.6.2
Latest Release Date:[1]

Cannon.js is an open source JavaScript 3D physics engine created by Stefan "schteppe" Hedman.[2] Unlike physics engine libraries ported from C++ to JavaScript, cannon.js is written in JavaScript from the start and can take advantage of its features.[3] In a 2013 comparison with Ammo.js, cannon.js was found to be "more compact, more comprehensible, more powerful with regard to its performance and also easier to understand", but did not have as many features.[4]

Features

Cannon.js supports the following shapes: sphere, plane, box, cylinder, convex polyhedron, particle, and heightfield. This collection of shapes matches the collection used by rendering engines such as Three.js and Babylon, but is not complete. For example, it is not sufficient for X3DOM, an application of X3D which allows 3D graphics to be included in web pages without the need for a plug-in.[5]

The physics engine implements rigid-body dynamics, discrete collision detection, and a Gauss-Seidel constraint solver.[6] It can perform cloth simulation[7]

Cannon.js can be used with Three.js and Babylon.js[8] [9] WebGL renderers to generate physics-based 3D scenes. It can also be used to provide networked-physics synchronization for multiplayer online games using Lance.gg[10]

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.).[11] // Setup our worldvar world = new CANNON.World;world.gravity.set(0, 0, -9.82); // m/s²

// Create a spherevar radius = 1; // mvar sphereBody = new CANNON.Body;world.addBody(sphereBody);

// Create a planevar groundBody = new CANNON.Body;var groundShape = new CANNON.Plane;groundBody.addShape(groundShape);world.addBody(groundBody);

var fixedTimeStep = 1.0 / 60.0; // secondsvar maxSubSteps = 3;

// Start the simulation loopvar lastTime;(function simloop(time));

External links

Notes and References

  1. Web site: Releases · schteppe/cannon.js. GitHub.com. 11 May 2021.
  2. Web site: Stefan Hedman . GitHub . 27 April 2017.
  3. Web site: Prall . Chandler . JavaScript Physics Engines Comparison . Build New Games . 10 April 2012 . 27 April 2017.
  4. Book: Huber, Linda . Initial Steps for the Coupling of JavaScript Physics Engines with X3DOM . Jan Bender . Jeremie Dequidt . Christian Duriez . Gabriel Zachmann . Workshop on Virtual Reality Interaction and Physical Simulation (VRIPHYS 2013) . Workshop on Virtual Reality Interaction and Physical Simulation . The Eurographics Association . 2013 . 81–90 . 978-3-905674-57-6 . 10.2312/PE.vriphys.vriphys13.081-090.
  5. Web site: Background: What is X3DOM, and what can it do for me? . official x3dom documentation . x3dom.org . 27 April 2017.
  6. Web site: Branch: master. cannon.js/README.markdown . GitHub . 22 April 2015 . 27 April 2017.
  7. Web site: Cloth physics simulation.
  8. Web site: Create wonderful interactive games for the web: Using webgl and a physics engine (babylon.js & cannon.js). 21 February 2017.
  9. Web site: MSDN Game Development with Babylon.js. 21 February 2017.
  10. Web site: Lance source repository. GitHub.
  11. Web site: Cannon.js - JavaScripting. 2020-12-22. www.javascripting.com.