Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

223 rindas
9.7 KiB

  1. /*
  2. * Copyright (c) 2009-2012 jMonkeyEngine
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package roadtrip;
  33. import com.jme3.app.Application;
  34. import com.jme3.asset.AssetManager;
  35. import com.jme3.asset.TextureKey;
  36. import com.jme3.bullet.PhysicsSpace;
  37. import com.jme3.bullet.collision.shapes.CollisionShape;
  38. import com.jme3.bullet.collision.shapes.MeshCollisionShape;
  39. import com.jme3.bullet.control.RigidBodyControl;
  40. import com.jme3.input.MouseInput;
  41. import com.jme3.input.controls.ActionListener;
  42. import com.jme3.input.controls.MouseButtonTrigger;
  43. import com.jme3.light.AmbientLight;
  44. import com.jme3.material.Material;
  45. import com.jme3.math.ColorRGBA;
  46. import com.jme3.renderer.queue.RenderQueue.ShadowMode;
  47. import com.jme3.scene.Geometry;
  48. import com.jme3.scene.Node;
  49. import com.jme3.scene.shape.Box;
  50. import com.jme3.scene.shape.Sphere;
  51. import com.jme3.scene.shape.Sphere.TextureMode;
  52. import com.jme3.texture.Texture;
  53. /**
  54. *
  55. * @author normenhansen
  56. */
  57. public class PhysicsTestHelper {
  58. /**
  59. * creates a simple physics test world with a floor, an obstacle and some test boxes
  60. * @param rootNode
  61. * @param assetManager
  62. * @param space
  63. */
  64. public static void createPhysicsTestWorld(Node rootNode, AssetManager assetManager, PhysicsSpace space) {
  65. AmbientLight light = new AmbientLight();
  66. light.setColor(ColorRGBA.LightGray);
  67. rootNode.addLight(light);
  68. Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  69. material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
  70. //movable boxes
  71. for (int i = 0; i < 20; i++) {
  72. float s = i % 3 == 0 ? 1f : 0.4f;
  73. Box box = new Box(s, s, s);
  74. Geometry boxGeometry = new Geometry("Box", box);
  75. boxGeometry.setMaterial(material);
  76. boxGeometry.setLocalTranslation(i * 1.2f, 35, -3);
  77. //RigidBodyControl automatically uses box collision shapes when attached to single geometry with box mesh
  78. boxGeometry.addControl(new RigidBodyControl(2));
  79. rootNode.attachChild(boxGeometry);
  80. space.add(boxGeometry);
  81. }
  82. }
  83. public static void createPhysicsTestWorldSoccer(Node rootNode, AssetManager assetManager, PhysicsSpace space) {
  84. AmbientLight light = new AmbientLight();
  85. light.setColor(ColorRGBA.LightGray);
  86. rootNode.addLight(light);
  87. Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  88. material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
  89. Box floorBox = new Box(20, 0.25f, 20);
  90. Geometry floorGeometry = new Geometry("Floor", floorBox);
  91. floorGeometry.setMaterial(material);
  92. floorGeometry.setLocalTranslation(0, -0.25f, 0);
  93. // Plane plane = new Plane();
  94. // plane.setOriginNormal(new Vector3f(0, 0.25f, 0), Vector3f.UNIT_Y);
  95. // floorGeometry.addControl(new RigidBodyControl(new PlaneCollisionShape(plane), 0));
  96. floorGeometry.addControl(new RigidBodyControl(0));
  97. rootNode.attachChild(floorGeometry);
  98. space.add(floorGeometry);
  99. //movable spheres
  100. for (int i = 0; i < 5; i++) {
  101. Sphere sphere = new Sphere(16, 16, .5f);
  102. Geometry ballGeometry = new Geometry("Soccer ball", sphere);
  103. ballGeometry.setMaterial(material);
  104. ballGeometry.setLocalTranslation(i, 2, -3);
  105. //RigidBodyControl automatically uses Sphere collision shapes when attached to single geometry with sphere mesh
  106. ballGeometry.addControl(new RigidBodyControl(.001f));
  107. ballGeometry.getControl(RigidBodyControl.class).setRestitution(1);
  108. rootNode.attachChild(ballGeometry);
  109. space.add(ballGeometry);
  110. }
  111. {
  112. //immovable Box with mesh collision shape
  113. Box box = new Box(1, 1, 1);
  114. Geometry boxGeometry = new Geometry("Box", box);
  115. boxGeometry.setMaterial(material);
  116. boxGeometry.setLocalTranslation(4, 1, 2);
  117. boxGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(box), 0));
  118. rootNode.attachChild(boxGeometry);
  119. space.add(boxGeometry);
  120. }
  121. {
  122. //immovable Box with mesh collision shape
  123. Box box = new Box(1, 1, 1);
  124. Geometry boxGeometry = new Geometry("Box", box);
  125. boxGeometry.setMaterial(material);
  126. boxGeometry.setLocalTranslation(4, 3, 4);
  127. boxGeometry.addControl(new RigidBodyControl(new MeshCollisionShape(box), 0));
  128. rootNode.attachChild(boxGeometry);
  129. space.add(boxGeometry);
  130. }
  131. }
  132. /**
  133. * creates a box geometry with a RigidBodyControl
  134. * @param assetManager
  135. * @return
  136. */
  137. public static Geometry createPhysicsTestBox(AssetManager assetManager) {
  138. Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  139. material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
  140. Box box = new Box(0.25f, 0.25f, 0.25f);
  141. Geometry boxGeometry = new Geometry("Box", box);
  142. boxGeometry.setMaterial(material);
  143. //RigidBodyControl automatically uses box collision shapes when attached to single geometry with box mesh
  144. boxGeometry.addControl(new RigidBodyControl(2));
  145. return boxGeometry;
  146. }
  147. /**
  148. * creates a sphere geometry with a RigidBodyControl
  149. * @param assetManager
  150. * @return
  151. */
  152. public static Geometry createPhysicsTestSphere(AssetManager assetManager) {
  153. Material material = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  154. material.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
  155. Sphere sphere = new Sphere(8, 8, 0.25f);
  156. Geometry boxGeometry = new Geometry("Sphere", sphere);
  157. boxGeometry.setMaterial(material);
  158. //RigidBodyControl automatically uses sphere collision shapes when attached to single geometry with sphere mesh
  159. boxGeometry.addControl(new RigidBodyControl(2));
  160. return boxGeometry;
  161. }
  162. /**
  163. * creates an empty node with a RigidBodyControl
  164. * @param manager
  165. * @param shape
  166. * @param mass
  167. * @return
  168. */
  169. public static Node createPhysicsTestNode(AssetManager manager, CollisionShape shape, float mass) {
  170. Node node = new Node("PhysicsNode");
  171. RigidBodyControl control = new RigidBodyControl(shape, mass);
  172. node.addControl(control);
  173. return node;
  174. }
  175. /**
  176. * creates the necessary inputlistener and action to shoot balls from teh camera
  177. * @param app
  178. * @param rootNode
  179. * @param space
  180. */
  181. public static void createBallShooter(final Application app, final Node rootNode, final PhysicsSpace space) {
  182. ActionListener actionListener = new ActionListener() {
  183. public void onAction(String name, boolean keyPressed, float tpf) {
  184. Sphere bullet = new Sphere(32, 32, 0.4f, true, false);
  185. bullet.setTextureMode(TextureMode.Projected);
  186. Material mat2 = new Material(app.getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
  187. TextureKey key2 = new TextureKey("Textures/Terrain/Rock/Rock.PNG");
  188. key2.setGenerateMips(true);
  189. Texture tex2 = app.getAssetManager().loadTexture(key2);
  190. mat2.setTexture("ColorMap", tex2);
  191. if (name.equals("shoot") && !keyPressed) {
  192. Geometry bulletg = new Geometry("bullet", bullet);
  193. bulletg.setMaterial(mat2);
  194. bulletg.setShadowMode(ShadowMode.CastAndReceive);
  195. bulletg.setLocalTranslation(app.getCamera().getLocation());
  196. RigidBodyControl bulletControl = new RigidBodyControl(10);
  197. bulletg.addControl(bulletControl);
  198. bulletControl.setLinearVelocity(app.getCamera().getDirection().mult(25));
  199. bulletg.addControl(bulletControl);
  200. rootNode.attachChild(bulletg);
  201. space.add(bulletControl);
  202. }
  203. }
  204. };
  205. app.getInputManager().addMapping("shoot", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
  206. app.getInputManager().addListener(actionListener, "shoot");
  207. }
  208. }