Advertisement
Guest User

Untitled

a guest
Nov 4th, 2016
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var map;
  2. var maparray = [];
  3. var meshwithphysicsarray = [];
  4. var physicsobjects = [];
  5.  
  6. var loader = new THREE.ObjectLoader();
  7.  
  8. function loadmap( url ) {
  9.  
  10.  
  11.     loader.load( url, function( obj ) {
  12.  
  13.         console.log( url + ' LOADED' );
  14.         map = obj;
  15.  
  16.         maparray = map.children;
  17.         world.scene.add( map );
  18.  
  19.     } );
  20.  
  21.  
  22.  
  23.  
  24. }
  25.  
  26. var applyphysicsloop = setInterval( function() {
  27.  
  28.     if ( map ) {
  29.  
  30.         maparray.forEach( applyphysics );
  31.  
  32.     }
  33.  
  34.  
  35. }, 1 );
  36.  
  37.  
  38. function applyphysics( mesh ) {
  39.  
  40.     ///////////////////////////////////CUBE////////////////////////////////////////////////
  41.     if ( mesh.name.indexOf( '[PhyBox]' ) != - 1 ) {
  42.  
  43.         if ( meshwithphysicsarray.indexOf( mesh ) != - 1 ) {
  44.  
  45.             physicsobjects.forEach( function( physicsobject ) {
  46.  
  47.                 if ( physicsobject.native.name == mesh.name ) {
  48.  
  49.  
  50.                     mesh.position.x = physicsobject.native.position.x;
  51.                     mesh.position.y = physicsobject.native.position.y;
  52.                     mesh.position.z = physicsobject.native.position.z;
  53.  
  54.                     mesh.quaternion._w = physicsobject.native.quaternion._w;
  55.                     mesh.quaternion._x = physicsobject.native.quaternion._x;
  56.                     mesh.quaternion._y = physicsobject.native.quaternion._y;
  57.                     mesh.quaternion._z = physicsobject.native.quaternion._z;
  58.  
  59.                 }
  60.  
  61.             } );
  62.  
  63.         } else {
  64.  
  65.             var box = new WHS.Box( {
  66.                 geometry: {
  67.                     width: mesh.geometry.parameters.width,
  68.                     height: mesh.geometry.parameters.height,
  69.                     depth: mesh.geometry.parameters.depth
  70.                 },
  71.  
  72.                 mass: Number( mesh.name.replace( /\D/g, '' ) ),
  73.  
  74.                 material: {
  75.                     kind: 'basic',
  76.                     color: 0xffffff
  77.                 },
  78.  
  79.                 position: {
  80.                     x: mesh.position.x,
  81.                     y: mesh.position.y,
  82.                     z: mesh.position.z
  83.                 }
  84.  
  85.             } );
  86.             box.native.name = mesh.name;
  87.             box.native.visible = false;
  88.  
  89.             box.addTo( world );
  90.  
  91.             physicsobjects.push( box );
  92.  
  93.             meshwithphysicsarray.push( mesh );
  94.  
  95.         }
  96.  
  97.  
  98.  
  99.     }
  100.     /////////////////////////////////////////SPHERE////////////////////////////////////////////
  101.     if ( mesh.name.indexOf( '[PhyBall]' ) != - 1 ) {
  102.  
  103.         if ( meshwithphysicsarray.indexOf( mesh ) != - 1 ) {
  104.  
  105.             physicsobjects.forEach( function( physicsobject ) {
  106.  
  107.                 if ( physicsobject.native.name == mesh.name ) {
  108.  
  109.  
  110.                     mesh.position.x = physicsobject.native.position.x;
  111.                     mesh.position.y = physicsobject.native.position.y;
  112.                     mesh.position.z = physicsobject.native.position.z;
  113.  
  114.                     mesh.quaternion._w = physicsobject.native.quaternion._w;
  115.                     mesh.quaternion._x = physicsobject.native.quaternion._x;
  116.                     mesh.quaternion._y = physicsobject.native.quaternion._y;
  117.                     mesh.quaternion._z = physicsobject.native.quaternion._z;
  118.  
  119.                 }
  120.  
  121.             } );
  122.  
  123.         } else {
  124.  
  125.  
  126.  
  127.  
  128.             var sphere = new WHS.Sphere( {
  129.                 geometry: {
  130.                     radius: mesh.geometry.parameters.radius
  131.                 },
  132.  
  133.                 mass: Number( mesh.name.replace( /\D/g, '' ) ),
  134.  
  135.                 material: {
  136.                     color: 0xffffff,
  137.                     kind: 'lambert'
  138.                 },
  139.  
  140.                 position: {
  141.                     x: mesh.position.x,
  142.                     y: mesh.position.y,
  143.                     z: mesh.position.z
  144.                 }
  145.             } );
  146.  
  147.  
  148.  
  149.             sphere.native.name = mesh.name;
  150.             sphere.native.visible = false;
  151.  
  152.             sphere.addTo( world );
  153.  
  154.  
  155.             physicsobjects.push( sphere );
  156.  
  157.             meshwithphysicsarray.push( mesh );
  158.  
  159.         }
  160.  
  161.  
  162.  
  163.     }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement