topical media & game development

talk show tell print

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



  <DOCTYPE html>
  <html>
    <head>
      <title>cannon.js - events 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 });
        var size = 1;
        
        demo.addScene("'collide' event",function(){
        
            // Create world
            var world = demo.getWorld();
            world.solver.setSpookParams(5000,10);
            world.gravity.set(0,0,-20);
        
            // ground plane
            var groundShape = new CANNON.Plane();
            var groundBody = new CANNON.RigidBody(0,groundShape);
            world.add(groundBody);
            demo.addVisual(groundBody);
        
            // Sphere
            var sphere = new CANNON.Sphere(size);
            var sphereBody = new CANNON.RigidBody(30,sphere);
            var pos = new CANNON.Vec3(0,0,size);
            sphereBody.position.set(0,0,size*6);
            world.add(sphereBody);
            demo.addVisual(sphereBody);
            
            // Add collide event
            sphereBody.addEventListener("collide",function(e){
                alert("The sphere just collided with the ground!");
                sphereBody.velocity.set(0,0,0);
            });
            
          });
        
        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.