Advertisement
metalx1000

Basic Babylonjs Physics Scenes with Controls

Jun 7th, 2014
568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <title>BABYLON.JS Basic Physics Scene</title>
  5.  
  6.     <script src="babylon.js"></script>
  7.     <script src="cannon.js"></script>
  8.  
  9.     <style type="text/css">
  10.         html,body,#canvas {
  11.             width:100%;
  12.             height:100%;
  13.             padding:0;
  14.             margin:0;
  15.             overflow: hidden;
  16.         }
  17.         #buttons{
  18.             position: absolute;
  19.             top: 0px;
  20.             left: 0px;
  21.         }
  22.     </style>
  23. </head>
  24. <body>
  25.     <canvas id="canvas"></canvas>
  26.     <div id="buttons">
  27.       <div id="ball_controls">
  28.         <button id="button_up" onclick="Ball_Move(0,20,0)">UP</button>
  29.         <button id="button_left" onclick="Ball_Move(-1,0,0)">LEFT</button>
  30.         <button id="button_right" onclick="Ball_Move(1,0,0)">RIGHT</button>
  31.         <button id="button_forward" onclick="Ball_Move(0,0,1)">Forward</button>
  32.         <button id="button_back" onclick="Ball_Move(0,0,-1)">Back</button>
  33.       <div>
  34.  
  35.       <div id="box_controls">
  36.         <button id="button_up" onclick="Box_Move(0,1,0)">UP</button>
  37.         <button id="button_left" onclick="Box_Move(-.5,0,0)">LEFT</button>
  38.         <button id="button_right" onclick="Box_Move(.5,0,0)">RIGHT</button>
  39.         <button id="button_forward" onclick="Box_Move(0,0,.5)">Forward</button>
  40.         <button id="button_back" onclick="Box_Move(0,0,-.5)">Back</button>
  41.       <div>
  42.  
  43.     </div>
  44. <script>
  45.  
  46. var scene, camera, light0, ball, plane;
  47. window.onload = function(){
  48.     var canvas = document.getElementById("canvas");
  49.  
  50.     // Check support
  51.     if (!BABYLON.Engine.isSupported()) {
  52.         window.alert('Browser not supported');
  53.     } else {
  54.         // Babylon
  55.         var engine = new BABYLON.Engine(canvas, true);
  56.  
  57.         scene = new BABYLON.Scene(engine);
  58.         scene.enablePhysics();
  59.         scene.setGravity(new BABYLON.Vector3(0, -10, 0));
  60.         // Creating a camera looking to the zero point (0,0,0), a light, and a sphere of size 1
  61.         //camera = new BABYLON.FreeCamera("Camera", 1, 0.8, 10, scene);
  62.         camera = new BABYLON.FreeCamera("FreeCamera", new BABYLON.Vector3(-10, -10, -15), scene);
  63.  
  64.         light0 = new BABYLON.PointLight("Omni", new BABYLON.Vector3(0, 0, -20), scene);
  65.  
  66.         ball = BABYLON.Mesh.CreateSphere("Ball", 10, 5.0, scene);
  67.         ball.position.y = 5;
  68.         ball.setPhysicsState({ impostor: BABYLON.PhysicsEngine.SphereImpostor, mass: 1 });
  69.         camera.lockedTarget = ball;
  70.  
  71.         box = BABYLON.Mesh.CreateBox("Box", 3, scene);
  72.         box.position.x = 5;
  73.         box.position.z = 5;
  74.         box.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 1, friction: 0.5, restitution: 0.7 });
  75.  
  76.         plane = BABYLON.Mesh.CreatePlane("Plane", 200.0, scene);
  77.         plane.position.y = -30;
  78.         plane.rotate(BABYLON.Axis.X, Math.PI/2, BABYLON.Space.LOCAL);
  79.         plane.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0, friction: 0.5, restitution: 0.7 });
  80.  
  81.         // Attach the camera to the scene
  82.         scene.activeCamera.attachControl(canvas);
  83.  
  84.         // Once the scene is loaded, just register a render loop to render it
  85.         engine.runRenderLoop(function () {
  86.             scene.render();
  87.         });
  88.  
  89.         // Resize
  90.         window.addEventListener("resize", function () {
  91.             engine.resize();
  92.         });
  93.     }
  94. };
  95.  
  96. function Ball_Move(x,y,z){
  97.     camera.lockedTarget = ball;
  98.     ball.applyImpulse(new BABYLON.Vector3(x, y, z),0);
  99. }
  100.  
  101. function Box_Move(x,y,z){
  102.     camera.lockedTarget = box;
  103.     box.applyImpulse(new BABYLON.Vector3(x, y, z),0);
  104. }
  105.  
  106. </script>
  107. </body>
  108. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement