Advertisement
yerzhik

index.js

Jun 18th, 2014
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var loadingBack;
  2. var canvas;
  3. var engine;
  4. var scene;
  5. var main;
  6. var catMesh = null;
  7. var vectorX = 0, vectorY = 0, vectorZ = 0;
  8. var mainObjectBox;
  9. var doPoll = function() {
  10.  
  11.     $.ajax({
  12.         url : "/orientation/getdata",
  13.         type: "POST",
  14.         dataType: 'json',
  15.         success: function(data, textStatus, jqXHR)
  16.         {
  17.             vectorX = data.vectorX;
  18.             vectorY = data.vectorY;
  19.             vectorZ = data.vectorZ;
  20.         },
  21.         error: function (jqXHR, textStatus, errorThrown)
  22.         {
  23.             console.log(textStatus);
  24.         }
  25.     });
  26.     setTimeout(doPoll, 10);
  27. }
  28.  
  29. window.onload = function () {
  30.     console.log("onload is called");
  31.     loadingBack = document.getElementById("loadingBack");
  32.     canvas = document.getElementById("renderCanvas");
  33.     engine = new BABYLON.Engine(canvas, true);
  34.     doPoll();
  35.     main();
  36. };
  37.  
  38. var main = function () {
  39.     var loadScene = function (name, incremental, then, scene) {
  40.         sceneChecked = false;
  41.  
  42.         var restoreUI = function () {
  43.  
  44.         };
  45.  
  46.         // Loading
  47.         var importScene = function () {
  48.             loadingBack.removeEventListener("transitionend", importScene);
  49.             loadingBack.removeEventListener("webkitTransitionend", importScene);
  50.  
  51.             engine.resize();
  52.             scene = CreatePhysicsScene(engine, scene, catMesh);
  53.  
  54.             scene.collisionsEnabled = true;
  55.             scene.executeWhenReady(function () {
  56.                 if (scene.activeCamera) {
  57.                     scene.activeCamera.attachControl(canvas);
  58.                     if (then) {
  59.                         then();
  60.                     }
  61.                 }
  62.  
  63.                 // UI
  64.                 restoreUI();
  65.             });
  66.  
  67.             return;
  68.         };
  69.         importScene();
  70.     };
  71.  
  72.  
  73.     var doIt = false;
  74.  
  75.     // Render loop
  76.     var renderFunction = function () {
  77.         // Render scene
  78.         if (scene) {
  79.  
  80.             if (doIt) {
  81.                 mainObjectBox.applyImpulse(new BABYLON.Vector3(-vectorY, vectorZ, vectorX), new BABYLON.Vector3(0, 0, 0));
  82.                 vectorX = 0;
  83.                 vectorY = 0;
  84.                 vectorZ = 0;
  85.                 doIt = false;
  86.             }
  87.  
  88.             scene.render();
  89.  
  90.  
  91.         }
  92.     };
  93.  
  94.     var scene = new BABYLON.Scene(engine);
  95.     scene.registerBeforeRender(function() {
  96.         if (vectorX != 0 || vectorY != 0 || vectorZ!=0) {
  97.             console.log("impulse X:" + vectorX);
  98.             console.log("impulse Y:" + vectorY);
  99.             console.log("impulse Z:" + vectorZ);
  100.             console.log(mainObjectBox);
  101.             mainObjectBox.position.x = 0;
  102.             mainObjectBox.position.y = 0;
  103.             mainObjectBox.position.z = 0;
  104.  
  105.             /*mainObjectBox.position.y = -0;
  106.             mainObjectBox.position.z = -25;*/
  107.             doIt = true;
  108.         } else {
  109.             //console.log(mainObjectBox.position.x);
  110.             mainObjectBox.position.x = mainObjectBox.position.x + 100;
  111.         }
  112.     });
  113.  
  114.    
  115. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement