physics.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. var CreatePhysicsScene = function(engine) {
  2. var scene = new BABYLON.Scene(engine);
  3. var camera = new BABYLON.FreeCamera("Camera", new BABYLON.Vector3(0, 0, -20), scene);
  4. camera.checkCollisions = true;
  5. camera.applyGravity = true;
  6. var light = new BABYLON.DirectionalLight("dir02", new BABYLON.Vector3(0.2, -1, 0), scene);
  7. light.position = new BABYLON.Vector3(0, 80, 0);
  8. // Material
  9. var materialAmiga = new BABYLON.StandardMaterial("amiga", scene);
  10. materialAmiga.diffuseTexture = new BABYLON.Texture("assets/amiga.jpg", scene);
  11. materialAmiga.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  12. materialAmiga.diffuseTexture.uScale = 5;
  13. materialAmiga.diffuseTexture.vScale = 5;
  14. var materialAmiga2 = new BABYLON.StandardMaterial("amiga", scene);
  15. materialAmiga2.diffuseTexture = new BABYLON.Texture("assets/mosaic.jpg", scene);
  16. materialAmiga2.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  17. // Shadows
  18. var shadowGenerator = new BABYLON.ShadowGenerator(2048, light);
  19. shadowGenerator.getShadowMap().renderList.push(box0);
  20. // Physics
  21. scene.enablePhysics();
  22. // Spheres
  23. var y = 0;
  24. for (var index = 0; index < 32; index++) {
  25. var sphere = BABYLON.Mesh.CreateSphere("Sphere0", 16, 3, scene);
  26. sphere.material = materialAmiga;
  27. sphere.position = new BABYLON.Vector3(Math.random() * 20 - 10, y, Math.random() * 10 - 5);
  28. shadowGenerator.getShadowMap().renderList.push(sphere);
  29. sphere.setPhysicsState({ impostor: BABYLON.PhysicsEngine.SphereImpostor, mass: 1 });
  30. y += 2;
  31. }
  32. // Link
  33. var spheres = [];
  34. for (var index = 0; index < 10; index++) {
  35. var sphere = BABYLON.Mesh.CreateSphere("Sphere0", 16, 1, scene);
  36. spheres.push(sphere);
  37. sphere.material = materialAmiga2;
  38. sphere.position = new BABYLON.Vector3(Math.random() * 20 - 10, y, Math.random() * 10 - 5);
  39. shadowGenerator.getShadowMap().renderList.push(sphere);
  40. sphere.setPhysicsState({ impostor: BABYLON.PhysicsEngine.SphereImpostor, mass: 1 });
  41. }
  42. for (var index = 0; index < 10; index++) {
  43. spheres[index].setPhysicsLinkWith(spheres[index + 1], new BABYLON.Vector3(0, 0.5, 0), new BABYLON.Vector3(0, -0.5, 0));
  44. }
  45. // Box
  46. var box0 = BABYLON.Mesh.CreateBox("Box0", 3, scene);
  47. box0.position = new BABYLON.Vector3(3, 30, 0);
  48. var materialWood = new BABYLON.StandardMaterial("wood", scene);
  49. materialWood.diffuseTexture = new BABYLON.Texture("assets/wood.jpg", scene);
  50. materialWood.emissiveColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  51. box0.material = materialWood;
  52. shadowGenerator.getShadowMap().renderList.push(box0);
  53. // Playground
  54. var ground = BABYLON.Mesh.CreateBox("Ground", 1, scene);
  55. ground.scaling = new BABYLON.Vector3(100, 1, 100);
  56. ground.position.y = -5.0;
  57. ground.checkCollisions = true;
  58. var border0 = BABYLON.Mesh.CreateBox("border0", 1, scene);
  59. border0.scaling = new BABYLON.Vector3(1, 100, 100);
  60. border0.position.y = -5.0;
  61. border0.position.x = -50.0;
  62. border0.checkCollisions = true;
  63. var border1 = BABYLON.Mesh.CreateBox("border1", 1, scene);
  64. border1.scaling = new BABYLON.Vector3(1, 100, 100);
  65. border1.position.y = -5.0;
  66. border1.position.x = 50.0;
  67. border1.checkCollisions = true;
  68. var border2 = BABYLON.Mesh.CreateBox("border2", 1, scene);
  69. border2.scaling = new BABYLON.Vector3(100, 100, 1);
  70. border2.position.y = -5.0;
  71. border2.position.z = 50.0;
  72. border2.checkCollisions = true;
  73. var border3 = BABYLON.Mesh.CreateBox("border3", 1, scene);
  74. border3.scaling = new BABYLON.Vector3(100, 100, 1);
  75. border3.position.y = -5.0;
  76. border3.position.z = -50.0;
  77. border3.checkCollisions = true;
  78. camera.setTarget(new BABYLON.Vector3(0, 0, 0));
  79. var groundMat = new BABYLON.StandardMaterial("groundMat", scene);
  80. groundMat.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  81. groundMat.emissiveColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  82. ground.material = groundMat;
  83. border0.material = groundMat;
  84. border1.material = groundMat;
  85. border2.material = groundMat;
  86. border3.material = groundMat;
  87. ground.receiveShadows = true;
  88. // Physics
  89. box0.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 2, friction: 0.4, restitution: 0.3 });
  90. ground.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0, friction: 0.5, restitution: 0.7 });
  91. border0.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  92. border1.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  93. border2.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  94. border3.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  95. return scene;
  96. };