topical media & game development

talk show tell print

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



  <DOCTYPE html>
  <html>
    <head>
      <title>cannon.js - compound 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>
  
        var demo = new CANNON.Demo({
          stepFrequency:180,
          
        });
        
        demo.addScene("Boxes",function(){
            var world = setupWorld(demo);
        
            // Compound shape
            var compoundShape = new CANNON.Compound();
            var s = 1.5;
            var shape = new CANNON.Box(new CANNON.Vec3(0.5*s,0.5*s,0.5*s));
            compoundShape.addChild(shape,new CANNON.Vec3( s, 0,-s));
            compoundShape.addChild(shape,new CANNON.Vec3( s, 0, s));
            compoundShape.addChild(shape,new CANNON.Vec3(-s, 0,-s));
            compoundShape.addChild(shape,new CANNON.Vec3(-s, 0, s));
        
            compoundShape.addChild(shape,new CANNON.Vec3(-s, 0, 0));
            compoundShape.addChild(shape,new CANNON.Vec3( 0, 0,-s));
            compoundShape.addChild(shape,new CANNON.Vec3( 0, 0, s));
        
            var mass = 10;
            var body = new CANNON.RigidBody(mass,compoundShape);
            body.position.set(0,0,6);
            body.quaternion.set(0,1,0,0.1);
            world.add(body);
            demo.addVisual(body);
  
          });
        
        demo.addScene("Spheres",function(){
            var world = setupWorld(demo);
        
            // Compound shape
            var compoundShape = new CANNON.Compound();
            var sphereShape = new CANNON.Sphere(1);
            compoundShape.addChild(sphereShape,new CANNON.Vec3( 1, 0,-1));
            compoundShape.addChild(sphereShape,new CANNON.Vec3( 1, 0, 1));
            compoundShape.addChild(sphereShape,new CANNON.Vec3(-1, 0,-1));
            compoundShape.addChild(sphereShape,new CANNON.Vec3(-1, 0, 1));
        
            var mass = 10;
            var body = new CANNON.RigidBody(mass,compoundShape);
            body.position.set(0,0,6);
            body.quaternion.set(0,1,0,0.1);
            world.add(body);
            demo.addVisual(body);
        
          });
        
        function setupWorld(demo){
          var world = demo.getWorld();
          world.gravity.set(0,0,-30);
          world.broadphase = new CANNON.NaiveBroadphase();
          world.solver.iterations = 50;
          world.solver.k = 10000;
          world.solver.d = 10;
        
          // ground plane
          var groundShape = new CANNON.Plane();
          var groundBody = new CANNON.RigidBody(0,groundShape);
          world.add(groundBody);
          demo.addVisual(groundBody);
        
          return world;
        };
        
        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.