Main.hx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. class Main extends hxd.App {
  2. var world : bullet.World;
  3. var bodies : Array<{ b : bullet.Body, m : h3d.scene.Mesh }> = [];
  4. override function init() {
  5. world = new bullet.World();
  6. world.setGravity(0,0,-9.81);
  7. var floor = new bullet.Body(bullet.Shape.createBox(100,100,1),0);
  8. world.addRigidBody(floor);
  9. var floorGfx = new h3d.prim.Cube(100, 100, 1, true);
  10. floorGfx.addNormals();
  11. var floorMesh = new h3d.scene.Mesh(floorGfx, s3d);
  12. floorMesh.material.color.setColor(0x800000);
  13. bodies.push({ b : floor, m : floorMesh });
  14. new h3d.scene.DirLight(new h3d.Vector(1, 2, -4), s3d);
  15. var shapes = [bullet.Shape.createSphere(0.5), bullet.Shape.createBox(1,1,1)];
  16. var prims = [new h3d.prim.Sphere(0.5), new h3d.prim.Cube(1, 1, 1, true)];
  17. prims[1].unindex();
  18. var comp = bullet.Shape.createCompound([
  19. { shape : shapes[0], mass : 1, position : new h3d.col.Point(0, 0, 0), rotation : new h3d.Quat() },
  20. { shape : shapes[1], mass : 1, position : new h3d.col.Point(0, 0, 1), rotation : new h3d.Quat() }
  21. ]);
  22. shapes.push(comp.shape);
  23. var c = new h3d.prim.Cube(1, 1, 2, true);
  24. c.unindex();
  25. prims.push(c);
  26. for( p in prims )
  27. p.addNormals();
  28. for( i in 0...100 ) {
  29. var id = Std.random(shapes.length);
  30. var m = new h3d.scene.Mesh(prims[id], s3d);
  31. m.x = Math.random() * 10;
  32. m.y = Math.random() * 10;
  33. m.z = 2 + Math.random() * 10;
  34. var mt = new h3d.Matrix();
  35. mt.identity();
  36. mt.colorHue(Math.random() * Math.PI * 2);
  37. m.material.color.set(0.5, 0.3, 0);
  38. m.material.color.transform(mt);
  39. var b = new bullet.Body(shapes[id], 0.5);
  40. b.setTransform(new h3d.col.Point(m.x, m.y, m.z));
  41. world.addRigidBody(b);
  42. bodies.push({ b : b, m : m });
  43. }
  44. for( b in bodies ) {
  45. b.m.material.mainPass.enableLights = true;
  46. b.m.material.shadows = true;
  47. }
  48. new h3d.scene.CameraController(80, s3d);
  49. }
  50. override function update(dt:Float) {
  51. world.stepSimulation(dt / 60, 10);
  52. for( b in bodies ) {
  53. var pos = b.b.position;
  54. var q = b.b.rotation;
  55. b.m.x = pos.x;
  56. b.m.y = pos.y;
  57. b.m.z = pos.z;
  58. b.m.setRotationQuat(q);
  59. }
  60. // check correct memory gc
  61. for( i in 0...1000 )
  62. bullet.Shape.createSphere(0.5);
  63. }
  64. static function main() {
  65. new Main();
  66. }
  67. }