topical media & game development

talk show tell print

mobile-query-three-plugins-cannonjs-vendor-cannon.js-demos-motionstates.htm / htm



  <DOCTYPE html>
  <html>
    <head>
      <title>cannon.js - motion states demo</title>
      <meta charset="utf-8">
      <style>* {margin:0;padding:0}</style>
    </head>
    <body>
      <script src="../build/cannon.js"></script>
      <script src="../build/cannon.demo.js"></script>
      <script src="../libs/dat.gui.js"></script>
      <script src="../libs/Three.js"></script>
      <script src="../libs/Detector.js"></script>
      <script src="../libs/Stats.js"></script>
      <script>
  
        
Demos of the Body.motionstate types.

  
        var demo = new CANNON.Demo({ stepFrequency:180 });
        var size = 2;
        
        demo.addScene("Moving box",function(){
            var world = setupWorld(demo);
        
            var boxShape = new CANNON.Box(new CANNON.Vec3(size,size,size));
            var sphereShape = new CANNON.Sphere(size);
        
            var mass = 5, boxMass = 0;
        
            // Kinematic Box
            // Does only collide with dynamic bodies, but does not respond to any force. Can be controlled by setting its velocity.
            var b1 = new CANNON.RigidBody(boxMass,boxShape);
            b1.motionstate = CANNON.Body.KINEMATIC;
            b1.position.set(0,0,0.5*size);
            world.add(b1);
            demo.addVisual(b1);
        
            // To control the box movement we must set its velocity
            b1.velocity.set(0,0,5);
            setInterval(function(){
                if(b1.velocity.z<0)
                    b1.velocity.set(0,0,5);
                else
                    b1.velocity.set(0,0,-5);
              },1000);
        
            // Dynamic Sphere
            // Dynamic bodies can collide with bodies of all other motionstates.
            var b2 = new CANNON.RigidBody(mass,sphereShape);
            b2.position.set(0,0,3*size);
            world.add(b2);
            demo.addVisual(b2);
          });
        
        demo.start();
        
        function setupWorld(demo){
          var world = demo.getWorld();
          world.gravity.set(0,0,-40);
          world.broadphase = new CANNON.NaiveBroadphase();
          world.solver.iterations = 30;
          world.solver.setSpookParams(10000,20);
        
          // Static ground plane
          // Static bodies only interacts with dynamic bodies. Velocity is always zero.
          var groundShape = new CANNON.Plane();
          var mass = 0; // mass=0 will produce a static body automatically
          var groundBody = new CANNON.RigidBody(mass,groundShape);
          world.add(groundBody);
          demo.addVisual(groundBody);
        
          return world;
        }
              
      </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.