collisions.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. var createScene = function () {
  2. var scene = new BABYLON.Scene(engine);
  3. // Lights
  4. var light0 = new BABYLON.DirectionalLight("Omni", new BABYLON.Vector3(-2, -5, 2), scene);
  5. var light1 = new BABYLON.PointLight("Omni", new BABYLON.Vector3(2, -5, -2), scene);
  6. // Need a free camera for collisions
  7. var camera = new BABYLON.FreeCamera("FreeCamera", new BABYLON.Vector3(0, -8, -20), scene);
  8. camera.attachControl(canvas, true);
  9. //Ground
  10. var ground = BABYLON.Mesh.CreatePlane("ground", 20.0, scene);
  11. ground.material = new BABYLON.StandardMaterial("groundMat", scene);
  12. ground.material.diffuseColor = new BABYLON.Color3(1, 1, 1);
  13. ground.material.backFaceCulling = false;
  14. ground.position = new BABYLON.Vector3(5, -10, -15);
  15. ground.rotation = new BABYLON.Vector3(Math.PI / 2, 0, 0);
  16. //Simple crate
  17. var box = new BABYLON.Mesh.CreateBox("crate", 2, scene);
  18. box.material = new BABYLON.StandardMaterial("Mat", scene);
  19. box.material.diffuseTexture = new BABYLON.Texture("textures/crate.png", scene);
  20. box.material.diffuseTexture.hasAlpha = true;
  21. box.position = new BABYLON.Vector3(5, -9, -10);
  22. //Set gravity for the scene (G force like, on Y-axis)
  23. scene.gravity = new BABYLON.Vector3(0, -0.9, 0);
  24. // Enable Collisions
  25. scene.collisionsEnabled = true;
  26. //Then apply collisions and gravity to the active camera
  27. camera.checkCollisions = true;
  28. camera.applyGravity = true;
  29. //Set the ellipsoid around the camera (e.g. your player's size)
  30. camera.ellipsoid = new BABYLON.Vector3(1, 1, 1);
  31. //finally, say which mesh will be collisionable
  32. ground.checkCollisions = true;
  33. box.checkCollisions = true;
  34. return scene;
  35. }