topical media & game development

talk show tell print

mobile-query-three-plugins-cannonjs-examples-pile.htm / htm



  <DOCTYPE html>
      <html>
      <head>
          <title>cannon.js - pile demo</title>
          <meta charset="utf-8">
          <style>* {margin:0;padding:0}</style>
      </head>
      <body>
          <script src="../vendor/cannon.js/build/cannon.js"></script>
          <script src="../vendor/cannon.js/build/cannon.demo.js"></script>
          <script src="../vendor/cannon.js/libs/dat.gui.js"></script>
          <script src="../vendor/cannon.js/libs/Three.js"></script>
          <script src="../vendor/cannon.js/libs/Detector.js"></script>
          <script src="../vendor/cannon.js/libs/Stats.js"></script>
          <script>
  
          var demo = new CANNON.Demo({ stepFrequency:180 });
          var size = 1;
          var interval;
  
          // Spheres
          demo.addScene("Pile",function(){
              if(interval) clearInterval(interval);
  
              var world = demo.getWorld();
  
              world.gravity.set(0,0,-50);
              world.broadphase = new CANNON.NaiveBroadphase();
              world.solver.iterations = 10;
              world.solver.setSpookParams(5000,10);
  
              // ground plane
              var groundShape = new CANNON.Plane(new CANNON.Vec3(0,0,1));
              var groundBody = new CANNON.RigidBody(0,groundShape);
              groundBody.position.set(0,0,0);
              world.add(groundBody);
              demo.addVisual(groundBody);
  
              // plane -x
              var planeShapeXmin = new CANNON.Plane();
              var planeXmin = new CANNON.RigidBody(0, planeShapeXmin);
              planeXmin.quaternion.setFromAxisAngle(new CANNON.Vec3(0,1,0),Math.PI/2);
              planeXmin.position.set(-5,0,0);
              world.add(planeXmin);
  
              // Plane +x
              var planeShapeXmax = new CANNON.Plane();
              var planeXmax = new CANNON.RigidBody(0, planeShapeXmax);
              planeXmax.quaternion.setFromAxisAngle(new CANNON.Vec3(0,1,0),-Math.PI/2);
              planeXmax.position.set(5,0,0);
              world.add(planeXmax);
  
              // Plane -y
              var planeShapeYmin = new CANNON.Plane();
              var planeYmin = new CANNON.RigidBody(0, planeShapeYmin);
              planeYmin.quaternion.setFromAxisAngle(new CANNON.Vec3(1,0,0),-Math.PI/2);
              planeYmin.position.set(0,-5,0);
              world.add(planeYmin);
  
              // Plane +y
              var planeShapeYmax = new CANNON.Plane();
              var planeYmax = new CANNON.RigidBody(0, planeShapeYmax);
              planeYmax.quaternion.setFromAxisAngle(new CANNON.Vec3(1,0,0),Math.PI/2);
              planeYmax.position.set(0,5,0);
              world.add(planeYmax);
  
              var bodies = [];
              var i = 0;
              interval = setInterval(function(){
                  // Sphere
                  i++;
                  var sphereShape = new CANNON.Sphere(size);
                  var b1 = new CANNON.RigidBody(5,sphereShape);
                  b1.position.set(2*size*Math.sin(i),2*size*Math.cos(i),7*2*size);
                  world.add(b1);
                  demo.addVisual(b1);
                  bodies.push(b1);
  
                  if(bodies.length>80){
                      var b = bodies.shift();
                      demo.removeVisual(b);
                      world.remove(b);
                  }
              },100);
          });
  
          demo.start();
  
          </script>
      </body>
  </html>
  


(C) Æliens 04/09/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.