timekeypressed.adoc 8.9 KB

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