Advertisement
yerzhik

physics.js

Jun 18th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var collisionDelta = 0.01;
  2. var mainObjectBox = 0;
  3. var intersectsPoint = function (point, boundingInfo, delta) {
  4.    
  5.     if (boundingInfo.maximumWorld.x - point.x < delta || delta > point.x - boundingInfo.minimumWorld.x)
  6.         return false;
  7.  
  8.     if (boundingInfo.maximumWorld.y - point.y < delta || delta > point.y - boundingInfo.minimumWorld.y)
  9.         return false;
  10.  
  11.     if (boundingInfo.maximumWorld.z - point.z < delta || delta > point.z - boundingInfo.minimumWorld.z)
  12.         return false;
  13.  
  14.     return true;
  15. };
  16.  
  17.  
  18. var CreatePhysicsScene = function (engine, scene, catMesh) {
  19.     var camera = new BABYLON.FreeCamera("Camera", new BABYLON.Vector3(0, 25, -50), scene);
  20.     camera.checkCollisions = true;
  21.     camera.applyGravity = true;
  22.  
  23.     var light = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(0.2, -1, 0), scene);
  24.     light.position = new BABYLON.Vector3(0, 80, 0);
  25.  
  26.     // Material
  27.     var materialAmiga = new BABYLON.StandardMaterial("amiga", scene);
  28.     materialAmiga.diffuseTexture = new BABYLON.Texture("assets/amiga.jpg", scene);
  29.     materialAmiga.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  30.     materialAmiga.diffuseTexture.uScale = 5;
  31.     materialAmiga.diffuseTexture.vScale = 5;
  32.  
  33.     var materialAmiga2 = new BABYLON.StandardMaterial("amiga", scene);
  34.     materialAmiga2.diffuseTexture = new BABYLON.Texture("assets/mosaic.jpg", scene);
  35.     materialAmiga2.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  36.  
  37.     // Shadows
  38.     var shadowGenerator = new BABYLON.ShadowGenerator(2048, light);
  39.   //  shadowGenerator.getShadowMap().renderList.push(box0);
  40.  
  41.     // Physics
  42.     scene.enablePhysics();
  43.  
  44.     var mainBoxLine = [];
  45.     var mainBoxLineIntersection = [];
  46.     var MAIN_BOX_LINE_AMOUNT = 10;
  47.    
  48.     for (var i = 0; i < MAIN_BOX_LINE_AMOUNT; ++i) {
  49.         var box0 = BABYLON.Mesh.CreateBox("Box" + i, 3, scene);
  50.         box0.position = new BABYLON.Vector3(-10 + i * 3, 0, 0);
  51.         var materialWood = new BABYLON.StandardMaterial("wood", scene);
  52.         materialWood.diffuseTexture = new BABYLON.Texture("assets/wood.jpg", scene);
  53.         materialWood.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  54.         box0.material = materialWood;
  55.  
  56.         shadowGenerator.getShadowMap().renderList.push(box0);
  57.         mainBoxLine.push(box0);
  58.     }
  59.    
  60.     for (var i = 0; i < MAIN_BOX_LINE_AMOUNT + 1; ++i) {
  61.         mainBoxLineIntersection[i] = new Array(MAIN_BOX_LINE_AMOUNT + 1);
  62.     }
  63.    
  64.     for (var i = 0; i < MAIN_BOX_LINE_AMOUNT + 1; ++i) {
  65.         for (var j = 0; j < MAIN_BOX_LINE_AMOUNT + 1; ++j) {
  66.             mainBoxLineIntersection[i][j] = false;
  67.         }
  68.     }
  69.    
  70.     var mainBox = null;
  71.    
  72.     var mainBox = BABYLON.Mesh.CreateBox("Box", 3, scene);
  73.     //var mainBox = catMesh;
  74.     mainBox.position = new BABYLON.Vector3(0, 0, -25);
  75.     var materialWood = new BABYLON.StandardMaterial("wood", scene);
  76.     materialWood.diffuseTexture = new BABYLON.Texture("assets/wood.jpg", scene);
  77.     materialWood.emissiveColor = new BABYLON.Color3(0.5, 0.0, 0.5);
  78.     mainBox.material = materialWood;
  79.  
  80.     shadowGenerator.getShadowMap().renderList.push(mainBox);
  81.    
  82.     // Playground
  83.     var ground = BABYLON.Mesh.CreateBox("Ground", 1, scene);
  84.     ground.scaling = new BABYLON.Vector3(100, 1, 100);
  85.     ground.position.y = -5.0;
  86.     ground.checkCollisions = true;
  87.  
  88.     var border0 = BABYLON.Mesh.CreateBox("border0", 1, scene);
  89.     border0.scaling = new BABYLON.Vector3(1, 100, 100);
  90.     border0.position.y = -5.0;
  91.     border0.position.x = -50.0;
  92.     border0.checkCollisions = true;
  93.  
  94.     var border1 = BABYLON.Mesh.CreateBox("border1", 1, scene);
  95.     border1.scaling = new BABYLON.Vector3(1, 100, 100);
  96.     border1.position.y = -5.0;
  97.     border1.position.x = 50.0;
  98.     border1.checkCollisions = true;
  99.  
  100.     var border2 = BABYLON.Mesh.CreateBox("border2", 1, scene);
  101.     border2.scaling = new BABYLON.Vector3(100, 100, 1);
  102.     border2.position.y = -5.0;
  103.     border2.position.z = 50.0;
  104.     border2.checkCollisions = true;
  105.  
  106.     var border3 = BABYLON.Mesh.CreateBox("border3", 1, scene);
  107.     border3.scaling = new BABYLON.Vector3(100, 100, 1);
  108.     border3.position.y = -5.0;
  109.     border3.position.z = -50.0;
  110.     border3.checkCollisions = true;
  111.  
  112.     camera.setTarget(new BABYLON.Vector3(0, 0, 0));
  113.  
  114.     var groundMat = new BABYLON.StandardMaterial("groundMat", scene);
  115.     groundMat.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  116.     groundMat.emissiveColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  117.     ground.material = groundMat;
  118.     border0.material = groundMat;
  119.     border1.material = groundMat;
  120.     border2.material = groundMat;
  121.     border3.material = groundMat;
  122.     ground.receiveShadows = true;
  123.  
  124.     // Physics
  125.     for (var i = 0; i < MAIN_BOX_LINE_AMOUNT; ++i) {
  126.         mainBoxLine[i].setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 2, friction: 0.4, restitution: 0.3 });
  127.     }
  128.    
  129.     mainBoxLine.push(mainBox);
  130.  
  131.  
  132.     var amplitude = 100;
  133.     var impX = 0.1;
  134.     var impY = 1;
  135.     var impZ = 0;
  136.     mainBox.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 5, friction: 0.4, restitution: 0.3 });
  137.  
  138.     mainObjectBox = mainBox;
  139.  
  140.     console.log("main box");
  141.     console.log(mainBox);
  142.     mainBox.checkCollisions = true;
  143.    
  144.     ground.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0, friction: 0.5, restitution: 0.7 });
  145.     border0.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  146.     border1.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  147.     border2.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  148.     border3.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  149.  
  150.     var boxSounds = [];
  151.     var floorSounds = [];
  152.    
  153.     for (m = 0; m < 100; ++m) {
  154.         boxSounds[m] = new Audio("audio/boxSound.wav");
  155.         floorSounds[m] = new Audio("audio/floorSound.wav");
  156.     }
  157.  
  158.     var playSound = function(soundType) {
  159.        
  160.         if (!soundType) {
  161.             for (k = 0; k < boxSounds.length; ++k) {
  162.                     if (boxSounds[k].ended || boxSounds[k].currentTime == 0) {
  163.                         boxSounds[k].play();
  164.                         break;
  165.                     }
  166.             }
  167.         } else {
  168.             for (k = 0; k < floorSounds.length; ++k) {
  169.                     if (floorSounds[k].ended || floorSounds[k].currentTime == 0) {
  170.                         floorSounds[k].play();
  171.                         break;
  172.                     }
  173.             }
  174.         }
  175.     }
  176.  
  177.     var flag = false;
  178.     var interCounter = 0;
  179.    
  180.     mainBox.playGroundIntersectionMatrix = [[],[],[],[],[]];
  181.    
  182.     for (var i = 0; i < MAIN_BOX_LINE_AMOUNT; ++i) {
  183.         mainBoxLine[i].playGroundIntersectionMatrix = [[],[],[],[],[]];
  184.     }
  185.    
  186.     var contains = function(arr, point) {
  187.         //console.log(arr);
  188.         for (var i = 0; i < arr.length; ++i) {
  189.             var arrayPoint = arr[i];
  190.             var arrX = arrayPoint.x;
  191.             var arrY = arrayPoint.y;
  192.             var arrZ = arrayPoint.z;
  193.            
  194.             var px = point.x;
  195.             var py = point.y;
  196.             var pz = point.z;
  197.            
  198.             var dX = Math.abs(arrX - px);
  199.             var dY = Math.abs(arrY - py);
  200.             var dZ = Math.abs(arrZ - pz);
  201.             var delta = Math.sqrt(dX * dX + dY * dY + dZ * dZ);
  202.             var eps = 0.01;
  203.            
  204.             if (delta < eps) {
  205.                 arr[i] = point;
  206.                 return true;
  207.             } else {
  208.                 /*console.log(point);
  209.                 console.log(arrayPoint);
  210.                 */
  211.                 //console.log("huy");
  212.             }
  213.         }
  214.         //console.log("returning false");
  215.         return false;
  216.     };
  217.    
  218.     var intersectsWith = function(border, object, index) {
  219.         var result = false;
  220.  
  221.         var localArray = [];
  222.         for (var i = 0; i < 8; ++i) {
  223.             var point = object._boundingInfo.boundingBox.vectorsWorld[i];
  224.             //console.log(border._boundingInfo.boundingBox);
  225.             if (intersectsPoint(point, border._boundingInfo.boundingBox, collisionDelta)) {
  226.                 point = object._boundingInfo.boundingBox.vectorsWorld[i]
  227.                 if (contains(object.playGroundIntersectionMatrix[index], point)) {
  228.                     //console.log("true");
  229.                 } else {
  230.                     //console.log("contains returns false");
  231.                     object.playGroundIntersectionMatrix[index].push(point);
  232.                     result = true;
  233.                 }
  234.                 localArray.push(point);
  235.             }
  236.         }
  237.        
  238.         object.playGroundIntersectionMatrix[index] = localArray;
  239.         return result;
  240.     };
  241.    
  242.     var checkCollisionWithPlayGround = function(obj) {
  243.         var result = false;
  244.  
  245.         if (intersectsWith(ground, obj, 0)) {
  246.             result = true;
  247.         }
  248.        
  249.         if (intersectsWith(border0, obj, 1)) {
  250.             result = true;
  251.         }
  252.        
  253.         if (intersectsWith(border1, obj, 2)) {
  254.             result = true;
  255.         }
  256.        
  257.         if (intersectsWith(border2, obj, 3)) {
  258.             result = true;
  259.         }
  260.        
  261.         if (intersectsWith(border3, obj, 4)) {
  262.             result = true;
  263.         }
  264.        
  265.         return result;
  266.     };
  267.    
  268.     scene.registerBeforeRender(function () {
  269.         var makeSound = false;
  270.         var makeSound2 = false;
  271.         for (var i = 0; i < MAIN_BOX_LINE_AMOUNT; ++i) {
  272.             for (var j = i + 1; j < MAIN_BOX_LINE_AMOUNT + 1; ++j) {
  273.                 var intersect1 = null;
  274.                 var intersect2 = null;
  275.  
  276.                 if (i != j) {
  277.                     if (i == MAIN_BOX_LINE_AMOUNT) {
  278.                         intersect1 = mainBox;
  279.                     } else {
  280.                         intersect1 = mainBoxLine[i];
  281.                     }
  282.  
  283.                     if (j == MAIN_BOX_LINE_AMOUNT) {
  284.                         intersect2 = mainBox;
  285.                     } else {
  286.                         intersect2 = mainBoxLine[j];
  287.                     }
  288.  
  289.                     if (intersect1.intersectsMesh(intersect2, false)) {
  290.                         if (!mainBoxLineIntersection[i][j]) {
  291.                             mainBoxLineIntersection[i][j] = true;
  292.                             makeSound = true;
  293.                         }
  294.                     } else {
  295.                         if (mainBoxLineIntersection[i][j]) {
  296.                             mainBoxLineIntersection[i][j] = false;
  297.                         }
  298.                     }
  299.                 }
  300.             }
  301.         }
  302.        
  303.         for (var i = 0; i < MAIN_BOX_LINE_AMOUNT; ++i) {
  304.             if (checkCollisionWithPlayGround(mainBoxLine[i])) {
  305.                 makeSound2 = true;
  306.             }
  307.         }
  308.  
  309.         if (checkCollisionWithPlayGround(mainBox)) {
  310.             makeSound2 = true;
  311.         }
  312.        
  313.         if (makeSound) {
  314.             playSound(false);
  315.         }
  316.        
  317.         if (makeSound2) {
  318.             playSound(true);
  319.         }
  320.        
  321.     });
  322.  
  323.     return scene;
  324. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement