PhysicsTestHelper.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package jme3test.bullet;
  6. import com.jme3.app.Application;
  7. import com.jme3.asset.AssetManager;
  8. import com.jme3.asset.TextureKey;
  9. import com.jme3.bullet.PhysicsSpace;
  10. import com.jme3.bullet.collision.shapes.CollisionShape;
  11. import com.jme3.bullet.collision.shapes.MeshCollisionShape;
  12. import com.jme3.bullet.control.RigidBodyControl;
  13. import com.jme3.input.MouseInput;
  14. import com.jme3.input.controls.ActionListener;
  15. import com.jme3.input.controls.MouseButtonTrigger;
  16. import com.jme3.light.AmbientLight;
  17. import com.jme3.material.Material;
  18. import com.jme3.math.ColorRGBA;
  19. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  20. import com.jme3.scene.Geometry;
  21. import com.jme3.scene.Node;
  22. import com.jme3.scene.shape.Box;
  23. import com.jme3.scene.shape.Sphere;
  24. import com.jme3.scene.shape.Sphere.TextureMode;
  25. import com.jme3.texture.Texture;
  26. /**
  27. *
  28. * @author normenhansen
  29. */
  30. public class PhysicsTestHelper {
  31. /**
  32. * creates a simple physics test world with a floor, an obstacle and some test boxes
  33. * @param rootNode
  34. * @param assetManager
  35. * @param space
  36. */
  37. public static void createPhysicsTestWorld(Node rootNode, AssetManager assetManager, PhysicsSpace space) {
  38. AmbientLight light = new AmbientLight();
  39. light.setColor(ColorRGBA.LightGray);
  40. rootNode.addLight(light);
  41. Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  42. material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
  43. Box floorBox = new Box(140, 0.25f, 140);
  44. Geometry floorGeometry = new Geometry("Floor", floorBox);
  45. floorGeometry.setMaterial(material);
  46. floorGeometry.setLocalTranslation(0, -5, 0);
  47. // Plane plane = new Plane();
  48. // plane.setOriginNormal(new Vector3f(0, 0.25f, 0), Vector3f.UNIT_Y);
  49. // floorGeometry.addControl(new RigidBodyControl(new PlaneCollisionShape(plane), 0));
  50. floorGeometry.addControl(new RigidBodyControl(0));
  51. rootNode.attachChild(floorGeometry);
  52. space.add(floorGeometry);
  53. //movable boxes
  54. for (int i = 0; i < 12; i++) {
  55. Box box = new Box(0.25f, 0.25f, 0.25f);
  56. Geometry boxGeometry = new Geometry("Box", box);
  57. boxGeometry.setMaterial(material);
  58. boxGeometry.setLocalTranslation(i, 5, -3);
  59. //RigidBodyControl automatically uses box collision shapes when attached to single geometry with box mesh
  60. boxGeometry.addControl(new RigidBodyControl(2));
  61. rootNode.attachChild(boxGeometry);
  62. space.add(boxGeometry);
  63. }
  64. //immovable sphere with mesh collision shape
  65. Sphere sphere = new Sphere(8, 8, 1);
  66. Geometry sphereGeometry = new Geometry("Sphere", sphere);
  67. sphereGeometry.setMaterial(material);
  68. sphereGeometry.setLocalTranslation(4, -4, 2);
  69. sphereGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(sphere), 0));
  70. rootNode.attachChild(sphereGeometry);
  71. space.add(sphereGeometry);
  72. }
  73. /**
  74. * creates a box geometry with a RigidBodyControl
  75. * @param assetManager
  76. * @return
  77. */
  78. public static Geometry createPhysicsTestBox(AssetManager assetManager) {
  79. Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  80. material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
  81. Box box = new Box(0.25f, 0.25f, 0.25f);
  82. Geometry boxGeometry = new Geometry("Box", box);
  83. boxGeometry.setMaterial(material);
  84. //RigidBodyControl automatically uses box collision shapes when attached to single geometry with box mesh
  85. boxGeometry.addControl(new RigidBodyControl(2));
  86. return boxGeometry;
  87. }
  88. /**
  89. * creates a sphere geometry with a RigidBodyControl
  90. * @param assetManager
  91. * @return
  92. */
  93. public static Geometry createPhysicsTestSphere(AssetManager assetManager) {
  94. Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  95. material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
  96. Sphere sphere = new Sphere(8, 8, 0.25f);
  97. Geometry boxGeometry = new Geometry("Sphere", sphere);
  98. boxGeometry.setMaterial(material);
  99. //RigidBodyControl automatically uses sphere collision shapes when attached to single geometry with sphere mesh
  100. boxGeometry.addControl(new RigidBodyControl(2));
  101. return boxGeometry;
  102. }
  103. /**
  104. * creates an empty node with a RigidBodyControl
  105. * @param manager
  106. * @param shape
  107. * @param mass
  108. * @return
  109. */
  110. public static Node createPhysicsTestNode(AssetManager manager, CollisionShape shape, float mass) {
  111. Node node = new Node("PhysicsNode");
  112. RigidBodyControl control = new RigidBodyControl(shape, mass);
  113. node.addControl(control);
  114. return node;
  115. }
  116. /**
  117. * creates the necessary inputlistener and action to shoot balls from teh camera
  118. * @param app
  119. * @param rootNode
  120. * @param space
  121. */
  122. public static void createBallShooter(final Application app, final Node rootNode, final PhysicsSpace space) {
  123. ActionListener actionListener = new ActionListener() {
  124. public void onAction(String name, boolean keyPressed, float tpf) {
  125. Sphere bullet = new Sphere(32, 32, 0.4f, true, false);
  126. bullet.setTextureMode(TextureMode.Projected);
  127. Material mat2 = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
  128. TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
  129. key2.setGenerateMips(true);
  130. Texture tex2 = app.getAssetManager().loadTexture(key2);
  131. mat2.setTexture("ColorMap", tex2);
  132. if (name.equals("shoot") && !keyPressed) {
  133. Geometry bulletg = new Geometry("bullet", bullet);
  134. bulletg.setMaterial(mat2);
  135. bulletg.setShadowMode(ShadowMode.CastAndReceive);
  136. bulletg.setLocalTranslation(app.getCamera().getLocation());
  137. RigidBodyControl bulletControl = new RigidBodyControl(1);
  138. bulletg.addControl(bulletControl);
  139. bulletControl.setLinearVelocity(app.getCamera().getDirection().mult(25));
  140. bulletg.addControl(bulletControl);
  141. rootNode.attachChild(bulletg);
  142. space.add(bulletControl);
  143. }
  144. }
  145. };
  146. app.getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  147. app.getInputManager().addListener(actionListener, "shoot");
  148. }
  149. }