physics.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // Compound
  54. var part0 = BABYLON.Mesh.CreateBox("part0", 3, scene);
  55. part0.position = new BABYLON.Vector3(3, 30, 0);
  56. part0.material = materialWood;
  57. var part1 = BABYLON.Mesh.CreateBox("part1", 3, scene);
  58. part1.parent = part0; // We need a hierarchy for compound objects
  59. part1.position = new BABYLON.Vector3(0, 3, 0);
  60. part1.material = materialWood;
  61. shadowGenerator.getShadowMap().renderList.push(part0);
  62. shadowGenerator.getShadowMap().renderList.push(part1);
  63. // Playground
  64. var ground = BABYLON.Mesh.CreateBox("Ground", 1, scene);
  65. ground.scaling = new BABYLON.Vector3(100, 1, 100);
  66. ground.position.y = -5.0;
  67. ground.checkCollisions = true;
  68. var border0 = BABYLON.Mesh.CreateBox("border0", 1, scene);
  69. border0.scaling = new BABYLON.Vector3(1, 100, 100);
  70. border0.position.y = -5.0;
  71. border0.position.x = -50.0;
  72. border0.checkCollisions = true;
  73. var border1 = BABYLON.Mesh.CreateBox("border1", 1, scene);
  74. border1.scaling = new BABYLON.Vector3(1, 100, 100);
  75. border1.position.y = -5.0;
  76. border1.position.x = 50.0;
  77. border1.checkCollisions = true;
  78. var border2 = BABYLON.Mesh.CreateBox("border2", 1, scene);
  79. border2.scaling = new BABYLON.Vector3(100, 100, 1);
  80. border2.position.y = -5.0;
  81. border2.position.z = 50.0;
  82. border2.checkCollisions = true;
  83. var border3 = BABYLON.Mesh.CreateBox("border3", 1, scene);
  84. border3.scaling = new BABYLON.Vector3(100, 100, 1);
  85. border3.position.y = -5.0;
  86. border3.position.z = -50.0;
  87. border3.checkCollisions = true;
  88. camera.setTarget(new BABYLON.Vector3(0, 0, 0));
  89. var groundMat = new BABYLON.StandardMaterial("groundMat", scene);
  90. groundMat.diffuseColor = new BABYLON.Color3(0.5, 0.5, 0.5);
  91. groundMat.emissiveColor = new BABYLON.Color3(0.2, 0.2, 0.2);
  92. ground.material = groundMat;
  93. border0.material = groundMat;
  94. border1.material = groundMat;
  95. border2.material = groundMat;
  96. border3.material = groundMat;
  97. ground.receiveShadows = true;
  98. // Physics
  99. box0.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 2, friction: 0.4, restitution: 0.3 });
  100. ground.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0, friction: 0.5, restitution: 0.7 });
  101. border0.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  102. border1.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  103. border2.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  104. border3.setPhysicsState({ impostor: BABYLON.PhysicsEngine.BoxImpostor, mass: 0 });
  105. scene.createCompoundImpostor({
  106. mass: 2, friction: 0.4, restitution: 0.3, parts: [
  107. { mesh: part0, impostor: BABYLON.PhysicsEngine.BoxImpostor },
  108. { mesh: part1, impostor: BABYLON.PhysicsEngine.BoxImpostor }]
  109. });
  110. return scene;
  111. };