topical media & game development

talk show tell print

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



  <DOCTYPE html>
  <html>
    <head>
      <title>cannon.js - sleep 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>
  
        
When a body sleeps, it does not move until it gets touched by another body. Why is this handy? Well, it can help you get a more stable simulation, and increase performance. No collision detection is made between sleeping bodies (and static ones).

  
  
        var demo = new CANNON.Demo({
          stepFrequency:180,
          k:4000
        });
        var size = 1;
  
        // Sleep demo
        demo.addScene("Sleep",function(){
        
          // Create world and collision detection
          var world = demo.getWorld();
          world.gravity.set(0,0,-10);
          world.broadphase = new CANNON.NaiveBroadphase();
  
          // Create ground plane
          var groundShape = new CANNON.Plane();
          var groundBody = new CANNON.RigidBody(0,groundShape);
  
          // Create sphere
          var sphere = new CANNON.Sphere(size);
          var sphereBody = new CANNON.RigidBody(1,sphere);
          var pos = new CANNON.Vec3(0,0,size);
          sphereBody.position.set(0,0,size*6);
        
          // Allow sleeping
          world.allowSleep = true;
          sphereBody.allowSleep = true;
  
          // Sleep parameters
          sphereBody.sleepSpeedLimit = 0.1; // Body will feel sleepy if speed<1 (speed == norm of velocity)
          sphereBody.sleepTimeLimit = 1000; // Body falls asleep after 1000ms of sleepiness
  
          sphereBody.addEventListener("sleepy",function(event){
            console.log("The sphere is feeling sleepy...");
          });
  
          sphereBody.addEventListener("sleep",function(event){
            console.log("The sphere fell asleep!");
          });
        
          // Add bodies to the world
          world.add(sphereBody);
          world.add(groundBody);
  
          // Add visuals
          demo.addVisual(sphereBody);
          demo.addVisual(groundBody);
  
        });
  
        // Wake up demo
        demo.addScene("Wake up",function(){
          // Create world and collision detection
          var world = demo.getWorld();
          world.gravity.set(0,0,-10);
          world.broadphase = new CANNON.NaiveBroadphase();
  
          // Create ground plane
          var groundShape = new CANNON.Plane();
          var groundBody = new CANNON.RigidBody(0,groundShape);
  
          // Create sphere
          var size = 2;
          var sphere = new CANNON.Sphere(size);
          var sphereBody = new CANNON.RigidBody(1,sphere);
          sphereBody.position.set(0,0,size);
          sphereBody.sleep();
  
          // Create sphere that will wake up the first one
          var sphereBody2 = new CANNON.RigidBody(1,sphere);
          sphereBody2.position.set(size*10,0,size);
          sphereBody2.velocity.set(-10,0,0);
          sphereBody2.angularDamping = 0.0;
          sphereBody2.linearDamping = 0.0;
        
          world.allowSleep = true;
          sphereBody.allowSleep = true;
  
          sphereBody.sleepSpeedLimit = 0.5;
          sphereBody.sleepTimeLimit = 1000;
  
          // The body wakes up when it gets a new contact
          sphereBody.addEventListener("wakeup",function(event){
            console.log("The sphere woke up!");
          });
        
          // Add bodies to the world
          world.add(sphereBody);
          world.add(sphereBody2);
          world.add(groundBody);
  
          // Add visuals
          demo.addVisual(sphereBody);
          demo.addVisual(sphereBody2);
          demo.addVisual(groundBody);
  
        });
  
        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.