timekeypressed.adoc 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. = jMonkeyEngine 3 Tutorial (5) - Hello Input System - Variation over time key is pressed
  2. :revnumber: 2.0
  3. :revdate: 2020/07/27
  4. Parent: xref:beginner/hello_input_system.adoc[jMonkeyEngine 3 Tutorial (5) - Hello Input System]
  5. Here is a sample of a program where the camera accelerates if the key is kept pressed.
  6. [source,java]
  7. ----
  8. import com.jme3.app.SimpleApplication;
  9. import com.jme3.bounding.BoundingBox;
  10. import com.jme3.font.BitmapText;
  11. import com.jme3.input.KeyInput;
  12. import com.jme3.input.controls.ActionListener;
  13. import com.jme3.input.controls.KeyTrigger;
  14. import com.jme3.light.DirectionalLight;
  15. import com.jme3.light.PointLight;
  16. import com.jme3.material.Material;
  17. import com.jme3.math.ColorRGBA;
  18. import com.jme3.math.Vector3f;
  19. import com.jme3.scene.Geometry;
  20. import com.jme3.terrain.geomipmap.TerrainLodControl;
  21. import com.jme3.terrain.geomipmap.TerrainQuad;
  22. import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
  23. import com.jme3.terrain.heightmap.AbstractHeightMap;
  24. import com.jme3.terrain.heightmap.ImageBasedHeightMap;
  25. import com.jme3.texture.Texture;
  26. import com.jme3.texture.Texture.WrapMode;
  27. public class Accelerate extends SimpleApplication implements ActionListener {
  28. private TerrainQuad terrain;
  29. Material matTerrain;
  30. Material matWire;
  31. boolean wireframe = false;
  32. boolean triPlanar = false;
  33. boolean wardiso = false;
  34. boolean minnaert = false;
  35. protected BitmapText hintText;
  36. PointLight pl;
  37. Geometry lightMdl;
  38. private float dirtScale = 16;
  39. private enum Actions {
  40. LEFT, RIGHT, UP, DOWN;
  41. }
  42. private final int CAMERA_HEIGHT = 250;
  43. private final int STARTING_CAM_SPEED = 1;
  44. private final Vector3f CAM_DIRECTION = new Vector3f(0, -0.3f, 1f);
  45. private final Vector3f CAM_LOCATION = new Vector3f(-1, 0, -CAMERA_HEIGHT);
  46. private final Vector3f walkDirection = new Vector3f();
  47. private boolean left = false;
  48. private boolean right = false;
  49. private boolean up = false;
  50. private boolean down = false;
  51. private float camSpeed = STARTING_CAM_SPEED;
  52. private void initCamera() {
  53. cam.setLocation(CAM_LOCATION);
  54. cam.lookAtDirection(CAM_DIRECTION.normalizeLocal(), Vector3f.UNIT_Y.clone().addLocal(0, 2, 0));
  55. }
  56. public static void main(String[] args) {
  57. Accelerate app = new Accelerate();
  58. app.start();
  59. }
  60. @Override
  61. public void simpleInitApp() {
  62. setupKeys();
  63. initTerrain();
  64. initCamera();
  65. }
  66. @Override
  67. public void simpleUpdate(float tpf) {
  68. updateMovement(tpf);
  69. }
  70. private void updateMovement(float tpf) {
  71. if (left || right || up || down) {
  72. // here we increment the speed using the tpf
  73. camSpeed += tpf * 2;
  74. System.out.print("Camera speed " + camSpeed + "; Location:" + cam.getLocation().toString()
  75. + "; direction:"+cam.getDirection()+"walkDirection:" + walkDirection.toString());
  76. }
  77. Vector3f camDir = cam.getDirection().clone().multLocal(camSpeed);
  78. Vector3f camLeft = cam.getLeft().clone().multLocal(camSpeed);
  79. // adjust position of rigid body for collisions
  80. walkDirection.set(cam.getLocation().clone());
  81. if (left) {
  82. walkDirection.addLocal(camLeft);
  83. }
  84. if (right) {
  85. walkDirection.addLocal(camLeft.negate());
  86. }
  87. if (up) {
  88. walkDirection.addLocal(camDir);
  89. }
  90. if (down) {
  91. walkDirection.addLocal(camDir.negate());
  92. }
  93. // player.setWalkDirection(walkDirection);
  94. cam.setLocation(walkDirection);
  95. if (left || right || up || down) {
  96. System.out.println("; new walkDirection:" + walkDirection);
  97. }
  98. }
  99. private void initTerrain() {
  100. // First, we load up our textures and the heightmap texture for the terrain
  101. // TERRAIN TEXTURE material
  102. matTerrain = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
  103. matTerrain.setBoolean("useTriPlanarMapping", false);
  104. matTerrain.setFloat("Shininess", 0.0f);
  105. // ALPHA map (for splat textures)
  106. matTerrain.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alpha1.png"));
  107. matTerrain.setTexture("AlphaMap_1", assetManager.loadTexture("Textures/Terrain/splat/alpha2.png"));
  108. // HEIGHTMAP image (for the terrain heightmap)
  109. Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
  110. // GRASS texture
  111. Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
  112. grass.setWrap(WrapMode.Repeat);
  113. // DIRT texture
  114. Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
  115. dirt.setWrap(WrapMode.Repeat);
  116. matTerrain.setTexture("DiffuseMap", dirt);
  117. matTerrain.setFloat("DiffuseMap_0_scale", dirtScale);
  118. // ROCK texture
  119. Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
  120. rock.setWrap(WrapMode.Repeat);
  121. // BRICK texture
  122. Texture brick = assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg");
  123. brick.setWrap(WrapMode.Repeat);
  124. // RIVER ROCK texture
  125. Texture riverRock = assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg");
  126. riverRock.setWrap(WrapMode.Repeat);
  127. // WIREFRAME material
  128. matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  129. matWire.getAdditionalRenderState().setWireframe(true);
  130. matWire.setColor("Color", ColorRGBA.Green);
  131. // CREATE HEIGHTMAP
  132. AbstractHeightMap heightmap = null;
  133. try {
  134. heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.5f);
  135. heightmap.load();
  136. heightmap.smooth(0.9f, 1);
  137. } catch (Exception e) {
  138. e.printStackTrace();
  139. }
  140. /*
  141. * Here we create the actual terrain. The tiles will be 65x65, and the total size of the
  142. * terrain will be 513x513. It uses the heightmap we created to generate the height values.
  143. */
  144. /**
  145. * Optimal terrain patch size is 65 (64x64).
  146. * The total size is up to you. At 1025 it ran fine for me (200+FPS), however at
  147. * size=2049, it got really slow. But that is a jump from 2 million to 8 million triangles...
  148. */
  149. terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());//, new LodPerspectiveCalculatorFactory(getCamera(), 4)); // add this in to see it use entropy for LOD calculations
  150. TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
  151. control.setLodCalculator(new DistanceLodCalculator(65, 2.7f)); // patch size, and a multiplier
  152. terrain.addControl(control);
  153. terrain.setMaterial(matTerrain);
  154. terrain.setModelBound(new BoundingBox());
  155. terrain.updateModelBound();
  156. terrain.setLocalTranslation(0, -100, 0);
  157. terrain.setLocalScale(1f, 1f, 1f);
  158. rootNode.attachChild(terrain);
  159. DirectionalLight light = new DirectionalLight();
  160. light.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalize());
  161. rootNode.addLight(light);
  162. }
  163. private void setupKeys() {
  164. inputManager.addMapping(Actions.LEFT.name(), new KeyTrigger(KeyInput.KEY_A));
  165. inputManager.addMapping(Actions.RIGHT.name(), new KeyTrigger(KeyInput.KEY_D));
  166. inputManager.addMapping(Actions.UP.name(), new KeyTrigger(KeyInput.KEY_W));
  167. inputManager.addMapping(Actions.DOWN.name(), new KeyTrigger(KeyInput.KEY_S));
  168. inputManager.addListener(this, Actions.LEFT.name());
  169. inputManager.addListener(this, Actions.RIGHT.name());
  170. inputManager.addListener(this, Actions.UP.name());
  171. inputManager.addListener(this, Actions.DOWN.name());
  172. }
  173. /**
  174. * These are our custom actions triggered by key presses. We do not walk yet, we just keep track of the direction
  175. * the user pressed.
  176. */
  177. @Override
  178. public void onAction(String name, boolean keyPressed, float tpf) {
  179. System.out.println("name:" + name + "; keyPressed:" + keyPressed);
  180. if (name.equals(Actions.LEFT.name())) {
  181. left = keyPressed;
  182. if (!keyPressed) { // if the key isn't pressed anymore reset the speed to the initial value
  183. camSpeed = STARTING_CAM_SPEED;
  184. }
  185. } else if (name.equals(Actions.RIGHT.name())) {
  186. right = keyPressed;
  187. if (!keyPressed) {
  188. camSpeed = STARTING_CAM_SPEED;
  189. }
  190. } else if (name.equals(Actions.UP.name())) {
  191. up = keyPressed;
  192. if (!keyPressed) {
  193. camSpeed = STARTING_CAM_SPEED;
  194. }
  195. } else if (name.equals(Actions.DOWN.name())) {
  196. down = keyPressed;
  197. if (!keyPressed) {
  198. camSpeed = STARTING_CAM_SPEED;
  199. }
  200. }
  201. }
  202. }
  203. ----