Advertisement
yskang

threejs-minecraft-38

Apr 26th, 2020
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const dt = 1.0 / 60.0; // seconds
  2.  
  3. class PhysicsEngine {
  4.   constructor() {
  5.     this.world = null;
  6.     this.physicsMaterial  = null;
  7.     this.groundBody  = null;
  8.     this.sphereShape = null;
  9.     this.sphereBody = null;
  10.  
  11.     this.mockBoxes = [];
  12.     this.mockBoxMeshes = [];
  13.   }
  14.  
  15.   initialize() {
  16.     // 初始化 cannon.js、重力、碰撞偵測
  17.     const world = new CANNON.World();
  18.     world.gravity.set( 0, -20, 0 );
  19.     world.broadphase = new CANNON.NaiveBroadphase();
  20.     this.world = world;
  21.  
  22.     // 解算器設定
  23.     const solver = new CANNON.GSSolver();
  24.     solver.iterations = 7;
  25.     solver.tolerance = 0.1;
  26.     const split = true;
  27.     if( split )
  28.       world.solver = new CANNON.SplitSolver( solver );
  29.     else
  30.       world.solver = solver;
  31.  
  32.     // 接觸材質相關設定(摩擦力、恢復係數)
  33.     world.defaultContactMaterial.contactEquationStiffness = 1e9;
  34.     world.defaultContactMaterial.contactEquationRelaxation = 4;
  35.     const physicsMaterial = new CANNON.Material( 'slipperyMaterial' );
  36.     this.physicsMaterial = physicsMaterial;
  37.     const physicsContactMaterial = new CANNON.ContactMaterial(
  38.       physicsMaterial,
  39.       physicsMaterial,
  40.       0.0, // 摩擦力
  41.       0.3 // 恢復係數
  42.     );
  43.     world.addContactMaterial( physicsContactMaterial );
  44.  
  45.     // 鼠標控制器剛體
  46.     const sphereShape = new CANNON.Sphere( 1.5 );
  47.     const sphereBody = new CANNON.Body( { mass: 5 } );
  48.     sphereBody.addShape( sphereShape );
  49.     sphereBody.position.set( 0, 5, 0 );
  50.     sphereBody.linearDamping = 0.9;
  51.     world.addBody( sphereBody );
  52.  
  53.     this.sphereShape = sphereShape;
  54.     this.sphereBody = sphereBody;
  55.   }
  56.  
  57.   uninitialize() {
  58.   }
  59.  
  60.   mock( scene, count ) {
  61.     // Add boxes
  62.     const halfExtents = new CANNON.Vec3( 1, 1, 1 );
  63.     const boxShape = new CANNON.Box( halfExtents );
  64.     const boxGeometry = new THREE.BoxGeometry(
  65.       halfExtents.x * 2,
  66.       halfExtents.y * 2,
  67.       halfExtents.z * 2
  68.     );
  69.  
  70.     for( let i = 0; i < count; i++ ) {
  71.       const x = ( Math.random() - 0.5 ) * 30;
  72.       const y = 10 + ( Math.random() - 0.5 ) * 1;
  73.       const z = ( Math.random() - 0.5 ) * 30;
  74.       const boxBody = new CANNON.Body( { mass: 5 } );
  75.       boxBody.addShape( boxShape );
  76.       const boxMaterial = new THREE.MeshLambertMaterial({
  77.         color: Math.random() * 0xffffff
  78.       });
  79.       const boxMesh = new THREE.Mesh( boxGeometry, boxMaterial );
  80.       this.world.addBody( boxBody );
  81.       scene.add( boxMesh );
  82.       boxBody.position.set( x, y, z );
  83.       boxMesh.position.set( x, y, z );
  84.       boxMesh.castShadow = true;
  85.       boxMesh.receiveShadow = true;
  86.       this.mockBoxes.push( boxBody );
  87.       this.mockBoxMeshes.push( boxMesh );
  88.     }
  89.   }
  90.  
  91.   update( navigation ) {
  92.     if( !navigation.enabled ) return;
  93.    
  94.     this.world.step( dt );
  95.     // Update box mesh positions
  96.     const boxes = this.mockBoxes;
  97.     const boxMeshes = this.mockBoxMeshes;
  98.     for( let i = 0; i < boxes.length; i++ ) {
  99.       boxMeshes[i].position.copy( boxes[i].position );
  100.       boxMeshes[i].quaternion.copy( boxes[i].quaternion );
  101.     }
  102.   }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement