Software:Cannon.js

From HandWiki
Short description: 3D JavaScript physics engine
Cannon.js
Developer(s)Stefan Hedman
Stable release
0.6.2 / March 28, 2015 (2015-03-28)[1]
Written inJavaScript
Operating systemOS independent
Type3D physics engine
LicenseMIT License
Websitewww.cannonjs.org

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,[4] 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 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;
})();

References

  1. "Releases · schteppe/cannon.js". https://github.com/schteppe/cannon.js/releases. 
  2. "Stefan Hedman". https://github.com/schteppe. Retrieved 27 April 2017. 
  3. Prall, Chandler (10 April 2012). "JavaScript Physics Engines Comparison". http://buildnewgames.com/physics-engines-comparison/. Retrieved 27 April 2017. 
  4. 4.0 4.1 Huber, Linda (2013). "Initial Steps for the Coupling of JavaScript Physics Engines with X3DOM". Workshop on Virtual Reality Interaction and Physical Simulation (VRIPHYS 2013). The Eurographics Association. 81–90. doi:10.2312/PE.vriphys.vriphys13.081-090. ISBN 978-3-905674-57-6. 
  5. "Background: What is X3DOM, and what can it do for me?". official x3dom documentation. x3dom.org. https://doc.x3dom.org/gettingStarted/background/index.html. Retrieved 27 April 2017. 
  6. "Branch: master. cannon.js/README.markdown". GitHub. 22 April 2015. https://github.com/schteppe/cannon.js/blob/master/README.markdown. Retrieved 27 April 2017. 
  7. "Cloth physics simulation". https://blog.raananweber.com/2016/04/03/cloth-physics-simulation-for-babylon-js/. 
  8. "Create wonderful interactive games for the web: Using webgl and a physics engine (babylon.js & cannon.js)". https://www.eternalcoding.com/?p=153. Retrieved 21 February 2017. 
  9. "MSDN Game Development with Babylon.js". https://msdn.microsoft.com/en-us/magazine/mt614269.aspx. Retrieved 21 February 2017. 
  10. "Lance source repository". https://github.com/lance-gg/lance. 
  11. "Cannon.js - JavaScripting". https://www.javascripting.com/view/cannon-js. 

External links