Advertisement
Guest User

Physijs double collision issue

a guest
Jul 29th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2.  
  3. <html>
  4.  
  5. <head>
  6.     <title>Rigid body - Physijs</title>
  7.    
  8.     <link rel="stylesheet" type="text/css" href="css/styles.css" />
  9.    
  10.     <script type="text/javascript" src="js/three.min.js"></script>
  11.     <script type="text/javascript" src="js/stats.js"></script>
  12.     <script type="text/javascript" src="../physi.js"></script>
  13.    
  14.     <script type="text/javascript">
  15.    
  16.     'use strict';
  17.    
  18.     Physijs.scripts.worker = '../physijs_worker.js';
  19.     Physijs.scripts.ammo = 'examples/js/ammo.js';
  20.    
  21.     var initScene, render, applyForce, setMousePosition, mouse_position,
  22.         ground_material, box_material,
  23.         projector, renderer, render_stats, physics_stats, scene, ground, light, camera, box, sphere, boxes = [];
  24.    
  25.     initScene = function() {
  26.         projector = new THREE.Projector;
  27.        
  28.         renderer = new THREE.WebGLRenderer({ antialias: true });
  29.         renderer.setSize( window.innerWidth, window.innerHeight );
  30.         renderer.shadowMapEnabled = true;
  31.         renderer.shadowMapSoft = true;
  32.         document.getElementById( 'viewport' ).appendChild( renderer.domElement );
  33.        
  34.         render_stats = new Stats();
  35.         render_stats.domElement.style.position = 'absolute';
  36.         render_stats.domElement.style.top = '1px';
  37.         render_stats.domElement.style.zIndex = 100;
  38.         document.getElementById( 'viewport' ).appendChild( render_stats.domElement );
  39.  
  40.         physics_stats = new Stats();
  41.         physics_stats.domElement.style.position = 'absolute';
  42.         physics_stats.domElement.style.top = '50px';
  43.         physics_stats.domElement.style.zIndex = 100;
  44.         document.getElementById( 'viewport' ).appendChild( physics_stats.domElement );
  45.        
  46.         scene = new Physijs.Scene;
  47.         scene.setGravity(new THREE.Vector3( 0, -30, 0 ));
  48.         scene.addEventListener(
  49.             'update',
  50.             function() {
  51.                 //applyForce();
  52.                 scene.simulate( undefined, 1 );
  53.                 physics_stats.update();
  54.             }
  55.         );
  56.        
  57.         camera = new THREE.PerspectiveCamera(
  58.             35,
  59.             window.innerWidth / window.innerHeight,
  60.             1,
  61.             1000
  62.         );
  63.         camera.position.set( 60, 50, 60 );
  64.         camera.lookAt( scene.position );
  65.         scene.add( camera );
  66.        
  67.         // Light
  68.         light = new THREE.DirectionalLight( 0xFFFFFF );
  69.         light.position.set( 20, 40, -15 );
  70.         light.target.position.copy( scene.position );
  71.         light.castShadow = true;
  72.         light.shadowCameraLeft = -60;
  73.         light.shadowCameraTop = -60;
  74.         light.shadowCameraRight = 60;
  75.         light.shadowCameraBottom = 60;
  76.         light.shadowCameraNear = 20;
  77.         light.shadowCameraFar = 200;
  78.         light.shadowBias = -.0001
  79.         light.shadowMapWidth = light.shadowMapHeight = 2048;
  80.         light.shadowDarkness = .7;
  81.         scene.add( light );
  82.        
  83.         // Materials
  84.         ground_material = Physijs.createMaterial(
  85.             new THREE.MeshLambertMaterial({ map: THREE.ImageUtils.loadTexture( 'images/rocks.jpg' ) }),
  86.             .8, // high friction
  87.             .4 // low restitution
  88.         );
  89.         ground_material.map.wrapS = ground_material.map.wrapT = THREE.RepeatWrapping;
  90.         ground_material.map.repeat.set( 3, 3 );
  91.        
  92.         box_material = Physijs.createMaterial(
  93.             new THREE.MeshLambertMaterial({ map: THREE.ImageUtils.loadTexture( 'images/plywood.jpg' ) }),
  94.             .4, // low friction
  95.             .6 // high restitution
  96.         );
  97.         box_material.map.wrapS = ground_material.map.wrapT = THREE.RepeatWrapping;
  98.         box_material.map.repeat.set( .25, .25 );
  99.        
  100.         // Ground
  101.         ground = new Physijs.BoxMesh(
  102.             new THREE.CubeGeometry(100, 1, 100),
  103.             ground_material,
  104.             0 // mass
  105.         );
  106.         ground.receiveShadow = true;
  107.         scene.add( ground );
  108.        
  109.        
  110.        
  111.        
  112.         box = new Physijs.BoxMesh(
  113.             new THREE.CubeGeometry( 4, 4, 4 ),
  114.             box_material
  115.         );
  116.         box.position.set(
  117.             0,
  118.             2,
  119.             0
  120.         );
  121.         box.castShadow = true;
  122.         scene.add( box );
  123.         boxes.push( box );
  124.        
  125.        
  126.         sphere = new Physijs.SphereMesh(
  127.             new THREE.SphereGeometry( 2, 8, 16 ),
  128.             box_material
  129.         );
  130.         sphere.position.set(
  131.             0,
  132.             8,
  133.             0
  134.         );
  135.         sphere.addEventListener( 'collision', function ( other_object, relative_velocity, relative_rotation, contact_normal ) {
  136.        
  137.             // `this` has collided with `other_object` with an impact speed of `relative_velocity` and a rotational force of `relative_rotation` and at normal `contact_normal`        
  138.             //console.log('collision', new Date().getTime(), this.name, other_object.name);
  139.            
  140.                         console.log('speed', relative_velocity.length(), new Date().getTime(), relative_velocity);
  141.            
  142.         } );
  143.         sphere.castShadow = true;
  144.         scene.add( sphere );
  145.        
  146.        
  147.        
  148.        
  149.         //renderer.domElement.addEventListener( 'mousemove', setMousePosition );
  150.  
  151.         requestAnimationFrame( render );
  152.         scene.simulate();
  153.     };
  154.    
  155.     render = function() {
  156.         requestAnimationFrame( render );
  157.         renderer.render( scene, camera );
  158.         render_stats.update();
  159.     };
  160.    
  161.     window.onload = initScene;
  162.    
  163.     </script>
  164. </head>
  165.  
  166. <body>
  167.     <div id="heading">
  168.         <h1>Rigid Body Double Collision</h1>
  169.     </div>
  170.     <div id="viewport"></div>
  171. </body>
  172.  
  173. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement