You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

273 rivejä
11 KiB

  1. package roadtrip;
  2. import com.jme3.app.SimpleApplication;
  3. import com.jme3.bullet.BulletAppState;
  4. import com.jme3.bullet.PhysicsSpace;
  5. import com.jme3.bullet.collision.shapes.BoxCollisionShape;
  6. import com.jme3.bullet.collision.shapes.CompoundCollisionShape;
  7. import com.jme3.bullet.control.VehicleControl;
  8. import com.jme3.input.KeyInput;
  9. import com.jme3.input.controls.ActionListener;
  10. import com.jme3.input.controls.KeyTrigger;
  11. import com.jme3.material.Material;
  12. import com.jme3.math.ColorRGBA;
  13. import com.jme3.math.FastMath;
  14. import com.jme3.math.Matrix3f;
  15. import com.jme3.math.Vector3f;
  16. import com.jme3.scene.Geometry;
  17. import com.jme3.scene.Node;
  18. import com.jme3.scene.Spatial;
  19. import com.jme3.scene.shape.Cylinder;
  20. /**
  21. *
  22. * @author dejvino
  23. */
  24. public class RoadTrip extends SimpleApplication implements ActionListener {
  25. final int WEAK = 1;
  26. final int TRUCK = 2;
  27. final int SPORT = 3;
  28. final int carType = SPORT;
  29. private BulletAppState bulletAppState;
  30. private VehicleControl vehicle;
  31. private float accelerationForce = 200.0f;
  32. private float brakeForce = 100.0f;
  33. private float steeringValue = 0;
  34. private float accelerationValue = 0;
  35. private Vector3f jumpForce = new Vector3f(0, 3000, 0);
  36. public static void main(String[] args) {
  37. RoadTrip app = new RoadTrip();
  38. app.start();
  39. }
  40. Spatial map;
  41. Spatial car;
  42. @Override
  43. public void simpleInitApp() {
  44. bulletAppState = new BulletAppState();
  45. stateManager.attach(bulletAppState);
  46. bulletAppState.getPhysicsSpace().enableDebug(assetManager);
  47. PhysicsTestHelper.createPhysicsTestWorld(rootNode, assetManager, bulletAppState.getPhysicsSpace());
  48. setupKeys();
  49. buildPlayer();
  50. map = assetManager.loadModel("Scenes/TestMap.j3o");
  51. rootNode.attachChild(map);
  52. getPhysicsSpace().addAll(map);
  53. vehicle.setPhysicsLocation(new Vector3f(5f, 30f, 5f));
  54. }
  55. private PhysicsSpace getPhysicsSpace(){
  56. return bulletAppState.getPhysicsSpace();
  57. }
  58. private void setupKeys() {
  59. inputManager.addMapping("Lefts", new KeyTrigger(KeyInput.KEY_H));
  60. inputManager.addMapping("Rights", new KeyTrigger(KeyInput.KEY_K));
  61. inputManager.addMapping("Ups", new KeyTrigger(KeyInput.KEY_U));
  62. inputManager.addMapping("Downs", new KeyTrigger(KeyInput.KEY_J));
  63. inputManager.addMapping("Space", new KeyTrigger(KeyInput.KEY_SPACE));
  64. inputManager.addMapping("Reset", new KeyTrigger(KeyInput.KEY_RETURN));
  65. inputManager.addListener(this, "Lefts");
  66. inputManager.addListener(this, "Rights");
  67. inputManager.addListener(this, "Ups");
  68. inputManager.addListener(this, "Downs");
  69. inputManager.addListener(this, "Space");
  70. inputManager.addListener(this, "Reset");
  71. }
  72. private void buildPlayer() {
  73. Material mat = new Material(getAssetManager(), "Common/MatDefs/Misc/Unshaded.j3md");
  74. mat.getAdditionalRenderState().setWireframe(true);
  75. mat.setColor("Color", ColorRGBA.Red);
  76. //create a compound shape and attach the BoxCollisionShape for the car body at 0,1,0
  77. //this shifts the effective center of mass of the BoxCollisionShape to 0,-1,0
  78. CompoundCollisionShape compoundShape = new CompoundCollisionShape();
  79. BoxCollisionShape box = new BoxCollisionShape(new Vector3f(1.4f, 0.5f, 3.6f));
  80. compoundShape.addChildShape(box, new Vector3f(0, 1, 0));
  81. if (carType == TRUCK) {
  82. BoxCollisionShape boxCabin = new BoxCollisionShape(new Vector3f(1.4f, 0.8f, 1.0f));
  83. compoundShape.addChildShape(boxCabin, new Vector3f(0, 2, 2f));
  84. } else if (carType == SPORT) {
  85. BoxCollisionShape boxCabin = new BoxCollisionShape(new Vector3f(1.2f, 0.6f, 2.0f));
  86. compoundShape.addChildShape(boxCabin, new Vector3f(0, 2, -1f));
  87. }
  88. //create vehicle node
  89. Node vehicleNode=new Node("vehicleNode");
  90. vehicle = new VehicleControl(compoundShape, 500);
  91. vehicleNode.addControl(vehicle);
  92. //setting suspension values for wheels, this can be a bit tricky
  93. //see also https://docs.google.com/Doc?docid=0AXVUZ5xw6XpKZGNuZG56a3FfMzU0Z2NyZnF4Zmo&hl=en
  94. float stiffness = 30.0f;//200=f1 car
  95. float compValue = .1f; //(should be lower than damp)
  96. float dampValue = .2f;
  97. vehicle.setSuspensionCompression(compValue * 2.0f * FastMath.sqrt(stiffness));
  98. vehicle.setSuspensionDamping(dampValue * 2.0f * FastMath.sqrt(stiffness));
  99. vehicle.setSuspensionStiffness(stiffness);
  100. vehicle.setMaxSuspensionForce(10000.0f);
  101. //Create four wheels and add them at their locations
  102. Vector3f wheelDirection = new Vector3f(0, -1, 0); // was 0, -1, 0
  103. Vector3f wheelAxle = new Vector3f(-1, 0, 0); // was -1, 0, 0
  104. float radius = 1.0f;
  105. float restLength = 0.3f;
  106. float yOff = 0.5f;
  107. float xOff = 1.6f;
  108. float zOff = 2f;
  109. Cylinder wheelMesh = new Cylinder(16, 16, radius, radius * 0.2f, true);
  110. Node node1 = new Node("wheel 1 node");
  111. Geometry wheels1 = new Geometry("wheel 1", wheelMesh);
  112. node1.attachChild(wheels1);
  113. wheels1.rotate(0, FastMath.HALF_PI, 0);
  114. wheels1.setMaterial(mat);
  115. vehicle.addWheel(node1, new Vector3f(-xOff, yOff, zOff),
  116. wheelDirection, wheelAxle, restLength, radius, true);
  117. Node node2 = new Node("wheel 2 node");
  118. Geometry wheels2 = new Geometry("wheel 2", wheelMesh);
  119. node2.attachChild(wheels2);
  120. wheels2.rotate(0, FastMath.HALF_PI, 0);
  121. wheels2.setMaterial(mat);
  122. vehicle.addWheel(node2, new Vector3f(xOff, yOff, zOff),
  123. wheelDirection, wheelAxle, restLength, radius, true);
  124. Node node3 = new Node("wheel 3 node");
  125. Geometry wheels3 = new Geometry("wheel 3", wheelMesh);
  126. node3.attachChild(wheels3);
  127. wheels3.rotate(0, FastMath.HALF_PI, 0);
  128. wheels3.setMaterial(mat);
  129. vehicle.addWheel(node3, new Vector3f(-xOff, yOff, -zOff),
  130. wheelDirection, wheelAxle, restLength, radius, false);
  131. Node node4 = new Node("wheel 4 node");
  132. Geometry wheels4 = new Geometry("wheel 4", wheelMesh);
  133. node4.attachChild(wheels4);
  134. wheels4.rotate(0, FastMath.HALF_PI, 0);
  135. wheels4.setMaterial(mat);
  136. vehicle.addWheel(node4, new Vector3f(xOff, yOff, -zOff),
  137. wheelDirection, wheelAxle, restLength, radius, false);
  138. vehicleNode.attachChild(node1);
  139. vehicleNode.attachChild(node2);
  140. vehicleNode.attachChild(node3);
  141. vehicleNode.attachChild(node4);
  142. if (carType == TRUCK) {
  143. Node node5 = new Node("wheel 5 node");
  144. Geometry wheels5 = new Geometry("wheel 5", wheelMesh);
  145. node5.attachChild(wheels5);
  146. wheels5.rotate(0, FastMath.HALF_PI, 0);
  147. wheels5.setMaterial(mat);
  148. vehicle.addWheel(node5, new Vector3f(-xOff, yOff, 2.1f* -zOff),
  149. wheelDirection, wheelAxle, restLength, radius, false);
  150. Node node6 = new Node("wheel 6 node");
  151. Geometry wheels6 = new Geometry("wheel 6", wheelMesh);
  152. node6.attachChild(wheels6);
  153. wheels6.rotate(0, FastMath.HALF_PI, 0);
  154. wheels6.setMaterial(mat);
  155. vehicle.addWheel(node6, new Vector3f(xOff, yOff, 2.1f* -zOff),
  156. wheelDirection, wheelAxle, restLength, radius, false);
  157. vehicleNode.attachChild(node5);
  158. vehicleNode.attachChild(node6);
  159. }
  160. rootNode.attachChild(vehicleNode);
  161. getPhysicsSpace().add(vehicle);
  162. vehicle.setPhysicsLocation(new Vector3f(5f, 30f, 5f));
  163. vehicle.getWheel(0).setFrictionSlip(0.8f);
  164. vehicle.getWheel(1).setFrictionSlip(0.8f);
  165. vehicle.getWheel(2).setFrictionSlip(0.6f);
  166. vehicle.getWheel(3).setFrictionSlip(0.6f);
  167. if (carType == TRUCK) {
  168. vehicle.getWheel(4).setFrictionSlip(0.6f);
  169. vehicle.getWheel(5).setFrictionSlip(0.6f);
  170. accelerationForce = 1400f;
  171. brakeForce = 200f;
  172. } else if (carType == SPORT) {
  173. accelerationForce = 20000f;
  174. brakeForce = 200f;
  175. }
  176. }
  177. @Override
  178. public void simpleUpdate(float tpf) {
  179. cam.setLocation(new Vector3f(-10f, 35f, -10f));
  180. cam.lookAt(vehicle.getPhysicsLocation(), Vector3f.UNIT_Y);
  181. }
  182. public void onAction(String binding, boolean value, float tpf) {
  183. float steerMax = 0.5f;
  184. if (carType == TRUCK) {
  185. steerMax = 0.7f;
  186. }
  187. if (binding.equals("Lefts")) {
  188. if (value) {
  189. steeringValue += steerMax;
  190. } else {
  191. steeringValue += -steerMax;
  192. }
  193. vehicle.steer(steeringValue);
  194. } else if (binding.equals("Rights")) {
  195. if (value) {
  196. steeringValue += -steerMax;
  197. } else {
  198. steeringValue += steerMax;
  199. }
  200. vehicle.steer(steeringValue);
  201. } else if (binding.equals("Ups")) {
  202. if (value) {
  203. accelerationValue += accelerationForce;
  204. } else {
  205. accelerationValue -= accelerationForce;
  206. }
  207. vehicle.accelerate(2, accelerationValue);
  208. vehicle.accelerate(3, accelerationValue);
  209. if (carType == TRUCK) {
  210. vehicle.accelerate(4, accelerationValue);
  211. vehicle.accelerate(5, accelerationValue);
  212. }
  213. } else if (binding.equals("Downs")) {
  214. float b;
  215. if (value) {
  216. b = brakeForce;
  217. } else {
  218. b = 0f;
  219. }
  220. vehicle.brake(0, b);
  221. vehicle.brake(1, b);
  222. } else if (binding.equals("Space")) {
  223. if (value) {
  224. vehicle.applyImpulse(jumpForce, Vector3f.ZERO);
  225. }
  226. } else if (binding.equals("Reset")) {
  227. if (value) {
  228. System.out.println("Reset");
  229. vehicle.setPhysicsLocation(Vector3f.ZERO);
  230. vehicle.setPhysicsRotation(new Matrix3f());
  231. vehicle.setLinearVelocity(Vector3f.ZERO);
  232. vehicle.setAngularVelocity(Vector3f.ZERO);
  233. vehicle.resetSuspension();
  234. } else {
  235. }
  236. }
  237. }
  238. }