timekeypressed.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>jMonkeyEngine 3 Tutorial (5) - Hello Input System - Variation over time key is pressed :: jMonkeyEngine Docs</title>
  7. <link rel="canonical" href="https://wiki.jmonkeyengine.org/docs/jme3/beginner/hello_input_system/timekeypressed.html">
  8. <meta name="generator" content="Antora 2.3.3">
  9. <link rel="stylesheet" href="../../../../_/css/site.css">
  10. <meta property="og:image" content="https://wiki.jmonkeyengine.org/_/img/iconx128.png">
  11. <meta property="og:description" content="jMonkeyEngine 3 Tutorial (5) - Hello Input System - Variation over time key is pressed">
  12. <meta property="og:title" content="jMonkeyEngine Docs">
  13. <link rel="stylesheet" href="../../../../_/css/site-extra.css">
  14. <link rel="stylesheet" href="../../../../_/css/vendor/docsearch.min.css">
  15. <!-- fetched from https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.css -->
  16. <link rel="icon" href="../../../../_/img/favicon.ico" type="image/x-icon">
  17. </head>
  18. <body class="article">
  19. <header class="header">
  20. <nav class="navbar">
  21. <div class="navbar-brand">
  22. <a class="navbar-item" href="https://wiki.jmonkeyengine.org">
  23. <img alt="" src="../../../../_/img/jMonkeyDocLogo.png" height="32" type="image/x-icon">
  24. </a>
  25. <div class="navbar-item hide-for-print">
  26. <input type="text" placeholder="Search docs..." id="search-input"/>
  27. </div>
  28. <button class="navbar-burger" data-target="topbar-nav">
  29. <span></span>
  30. <span></span>
  31. <span></span>
  32. </button>
  33. </div>
  34. <div id="topbar-nav" class="navbar-menu">
  35. <div class="navbar-end">
  36. <div class="navbar-item theme-switch-wrapper">
  37. <label class="theme-switch" for="checkbox">
  38. <input type="checkbox" id="checkbox" />
  39. <div class="slider round"></div>
  40. </label>
  41. </div>
  42. <a class="navbar-item" href="https://github.com/jmonkeyengine/wiki">Github</a>
  43. </div>
  44. </div>
  45. </nav>
  46. </header>
  47. <div class="body">
  48. <div class="nav-container" data-component="docs" data-version="master">
  49. <aside class="nav">
  50. <div class="panels">
  51. <div class="nav-panel-menu is-active" data-panel="menu">
  52. <nav class="nav-menu">
  53. <h3 class="title"><a href="../../../documentation.html">Docs</a></h3>
  54. <ul class="nav-list">
  55. <li class="nav-item" data-depth="0">
  56. <ul class="nav-list">
  57. <li class="nav-item" data-depth="1">
  58. <a class="nav-link" href="../../../documentation.html">Getting Started</a>
  59. </li>
  60. <li class="nav-item" data-depth="1">
  61. <a class="nav-link" href="https://javadoc.jmonkeyengine.org/v3.3.2-stable">JavaDoc</a>
  62. </li>
  63. <li class="nav-item" data-depth="1">
  64. <button class="nav-item-toggle"></button>
  65. <a class="nav-link" href="../../../jme3.html">jMonkeyEngine 3</a>
  66. <ul class="nav-list">
  67. <li class="nav-item" data-depth="2">
  68. <button class="nav-item-toggle"></button>
  69. <span class="nav-text">Beginner Tutorials</span>
  70. <ul class="nav-list">
  71. <li class="nav-item" data-depth="3">
  72. <a class="nav-link" href="../hello_simpleapplication.html">Hello SimpleApplication</a>
  73. </li>
  74. <li class="nav-item" data-depth="3">
  75. <a class="nav-link" href="../hello_node.html">Hello Node</a>
  76. </li>
  77. <li class="nav-item" data-depth="3">
  78. <a class="nav-link" href="../hello_asset.html">Hello Asset</a>
  79. </li>
  80. <li class="nav-item" data-depth="3">
  81. <a class="nav-link" href="../hello_main_event_loop.html">Hello Main Event Loop</a>
  82. </li>
  83. <li class="nav-item" data-depth="3">
  84. <a class="nav-link" href="../hello_input_system.html">Hello Input System</a>
  85. </li>
  86. <li class="nav-item" data-depth="3">
  87. <a class="nav-link" href="../hello_material.html">Hello Material</a>
  88. </li>
  89. <li class="nav-item" data-depth="3">
  90. <a class="nav-link" href="../hello_animation.html">Hello Animation</a>
  91. </li>
  92. <li class="nav-item" data-depth="3">
  93. <a class="nav-link" href="../hello_picking.html">Hello Picking</a>
  94. </li>
  95. <li class="nav-item" data-depth="3">
  96. <a class="nav-link" href="../hello_collision.html">Hello Collision</a>
  97. </li>
  98. <li class="nav-item" data-depth="3">
  99. <a class="nav-link" href="../hello_terrain.html">Hello Terrain</a>
  100. </li>
  101. <li class="nav-item" data-depth="3">
  102. <a class="nav-link" href="../hello_audio.html">Hello Audio</a>
  103. </li>
  104. <li class="nav-item" data-depth="3">
  105. <a class="nav-link" href="../hello_effects.html">Hello Effects</a>
  106. </li>
  107. <li class="nav-item" data-depth="3">
  108. <a class="nav-link" href="../hello_physics.html">Hello Physics</a>
  109. </li>
  110. </ul>
  111. </li>
  112. <li class="nav-item" data-depth="2">
  113. <button class="nav-item-toggle"></button>
  114. <span class="nav-text">Intermediate Tutorials</span>
  115. <ul class="nav-list">
  116. <li class="nav-item" data-depth="3">
  117. <button class="nav-item-toggle"></button>
  118. <span class="nav-text">Concepts</span>
  119. <ul class="nav-list">
  120. <li class="nav-item" data-depth="4">
  121. <a class="nav-link" href="../../intermediate/best_practices.html">Best Practices</a>
  122. </li>
  123. <li class="nav-item" data-depth="4">
  124. <a class="nav-link" href="../../intermediate/simpleapplication.html">Simple Application</a>
  125. </li>
  126. <li class="nav-item" data-depth="4">
  127. <a class="nav-link" href="../../features.html">Features</a>
  128. </li>
  129. <li class="nav-item" data-depth="4">
  130. <a class="nav-link" href="../../intermediate/optimization.html">Optimization</a>
  131. </li>
  132. <li class="nav-item" data-depth="4">
  133. <a class="nav-link" href="../../faq.html">FAQ</a>
  134. </li>
  135. </ul>
  136. </li>
  137. <li class="nav-item" data-depth="3">
  138. <button class="nav-item-toggle"></button>
  139. <span class="nav-text">Math Concepts</span>
  140. <ul class="nav-list">
  141. <li class="nav-item" data-depth="4">
  142. <a class="nav-link" href="../../math_for_dummies.html">Math For Dummies</a>
  143. </li>
  144. <li class="nav-item" data-depth="4">
  145. <a class="nav-link" href="../../intermediate/math.html">Math</a>
  146. </li>
  147. <li class="nav-item" data-depth="4">
  148. <a class="nav-link" href="../../math.html">More Math</a>
  149. </li>
  150. <li class="nav-item" data-depth="4">
  151. <a class="nav-link" href="../../rotate.html">Rotate</a>
  152. </li>
  153. <li class="nav-item" data-depth="4">
  154. <a class="nav-link" href="../../math_video_tutorials.html">Math Video Tutorials</a>
  155. </li>
  156. </ul>
  157. </li>
  158. <li class="nav-item" data-depth="3">
  159. <button class="nav-item-toggle"></button>
  160. <span class="nav-text">3D Graphics Concepts</span>
  161. <ul class="nav-list">
  162. <li class="nav-item" data-depth="4">
  163. <a class="nav-link" href="../../intermediate/multi-media_asset_pipeline.html">Multi-Media Asset Pipeline</a>
  164. </li>
  165. <li class="nav-item" data-depth="4">
  166. <a class="nav-link" href="../../scenegraph_for_dummies.html">Scenegraph for Dummies</a>
  167. </li>
  168. <li class="nav-item" data-depth="4">
  169. <a class="nav-link" href="../hellovector.html">Hello Vector</a>
  170. </li>
  171. <li class="nav-item" data-depth="4">
  172. <a class="nav-link" href="../../terminology.html">Terminology</a>
  173. </li>
  174. <li class="nav-item" data-depth="4">
  175. <a class="nav-link" href="../../intermediate/how_to_use_materials.html">How to Use Materials</a>
  176. </li>
  177. <li class="nav-item" data-depth="4">
  178. <a class="nav-link" href="../../intermediate/transparency_sorting.html">Transparency and Sorting</a>
  179. </li>
  180. <li class="nav-item" data-depth="4">
  181. <a class="nav-link" href="../../external/blender.html">Importing from Blender</a>
  182. </li>
  183. <li class="nav-item" data-depth="4">
  184. <a class="nav-link" href="../../external/3dsmax.html">Importing from 3DS Max</a>
  185. </li>
  186. </ul>
  187. </li>
  188. </ul>
  189. </li>
  190. </ul>
  191. </li>
  192. <li class="nav-item" data-depth="1">
  193. <a class="nav-link" href="../../../logo.html">Logo Usage</a>
  194. </li>
  195. <li class="nav-item" data-depth="1">
  196. <a class="nav-link" href="../../../bsd_license.html">License</a>
  197. </li>
  198. <li class="nav-item" data-depth="1">
  199. <a class="nav-link" href="../../../github_tips.html">Github Tips</a>
  200. </li>
  201. </ul>
  202. </li>
  203. <li class="nav-item" data-depth="0">
  204. <button class="nav-item-toggle"></button>
  205. <span class="nav-text">SDK</span>
  206. <ul class="nav-list">
  207. <li class="nav-item" data-depth="1">
  208. <a class="nav-link" href="../../../sdk.html">jMonkeyEngine SDK</a>
  209. </li>
  210. </ul>
  211. </li>
  212. </ul>
  213. </nav>
  214. </div>
  215. <div class="nav-panel-explore" data-panel="explore">
  216. <div class="context">
  217. <span class="title">Docs</span>
  218. <span class="version">master</span>
  219. </div>
  220. <ul class="components">
  221. <li class="component is-current">
  222. <span class="title">Docs</span>
  223. <ul class="versions">
  224. <li class="version is-current is-latest">
  225. <a href="../../../documentation.html">master</a>
  226. </li>
  227. </ul>
  228. </li>
  229. <li class="component">
  230. <span class="title">Wiki UI</span>
  231. <ul class="versions">
  232. <li class="version is-latest">
  233. <a href="../../../../wiki-ui/index.html">master</a>
  234. </li>
  235. </ul>
  236. </li>
  237. </ul>
  238. </div>
  239. </div>
  240. </aside>
  241. </div>
  242. <main class="article">
  243. <div class="toolbar" role="navigation">
  244. <button class="nav-toggle"></button>
  245. <nav class="breadcrumbs" aria-label="breadcrumbs">
  246. <ul>
  247. <li><a href="../../../documentation.html">Docs</a></li>
  248. <li><a href="timekeypressed.html">jMonkeyEngine 3 Tutorial (5) - Hello Input System - Variation over time key is pressed</a></li>
  249. </ul>
  250. </nav>
  251. <div class="edit-this-page"><a href="https://github.com/jMonkeyEngine/wiki/edit/master/docs/modules/ROOT/pages/jme3/beginner/hello_input_system/timekeypressed.adoc">Edit this Page</a></div>
  252. </div>
  253. <div class="content">
  254. <article class="doc">
  255. <h1 class="page">jMonkeyEngine 3 Tutorial (5) - Hello Input System - Variation over time key is pressed</h1>
  256. <div class="paragraph">
  257. <p>Parent: <a href="../hello_input_system.html" class="page">jMonkeyEngine 3 Tutorial (5) - Hello Input System</a></p>
  258. </div>
  259. <div class="paragraph">
  260. <p>Here is a sample of a program where the camera accelerates if the key is kept pressed.</p>
  261. </div>
  262. <div class="listingblock">
  263. <div class="content">
  264. <pre class="highlightjs highlight"><code class="language-java hljs" data-lang="java">import com.jme3.app.SimpleApplication;
  265. import com.jme3.bounding.BoundingBox;
  266. import com.jme3.font.BitmapText;
  267. import com.jme3.input.KeyInput;
  268. import com.jme3.input.controls.ActionListener;
  269. import com.jme3.input.controls.KeyTrigger;
  270. import com.jme3.light.DirectionalLight;
  271. import com.jme3.light.PointLight;
  272. import com.jme3.material.Material;
  273. import com.jme3.math.ColorRGBA;
  274. import com.jme3.math.Vector3f;
  275. import com.jme3.scene.Geometry;
  276. import com.jme3.terrain.geomipmap.TerrainLodControl;
  277. import com.jme3.terrain.geomipmap.TerrainQuad;
  278. import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
  279. import com.jme3.terrain.heightmap.AbstractHeightMap;
  280. import com.jme3.terrain.heightmap.ImageBasedHeightMap;
  281. import com.jme3.texture.Texture;
  282. import com.jme3.texture.Texture.WrapMode;
  283. public class Accelerate extends SimpleApplication implements ActionListener {
  284. private TerrainQuad terrain;
  285. Material matTerrain;
  286. Material matWire;
  287. boolean wireframe = false;
  288. boolean triPlanar = false;
  289. boolean wardiso = false;
  290. boolean minnaert = false;
  291. protected BitmapText hintText;
  292. PointLight pl;
  293. Geometry lightMdl;
  294. private float dirtScale = 16;
  295. private enum Actions {
  296. LEFT, RIGHT, UP, DOWN;
  297. }
  298. private final int CAMERA_HEIGHT = 250;
  299. private final int STARTING_CAM_SPEED = 1;
  300. private final Vector3f CAM_DIRECTION = new Vector3f(0, -0.3f, 1f);
  301. private final Vector3f CAM_LOCATION = new Vector3f(-1, 0, -CAMERA_HEIGHT);
  302. private final Vector3f walkDirection = new Vector3f();
  303. private boolean left = false;
  304. private boolean right = false;
  305. private boolean up = false;
  306. private boolean down = false;
  307. private float camSpeed = STARTING_CAM_SPEED;
  308. private void initCamera() {
  309. cam.setLocation(CAM_LOCATION);
  310. cam.lookAtDirection(CAM_DIRECTION.normalizeLocal(), Vector3f.UNIT_Y.clone().addLocal(0, 2, 0));
  311. }
  312. public static void main(String[] args) {
  313. Accelerate app = new Accelerate();
  314. app.start();
  315. }
  316. @Override
  317. public void simpleInitApp() {
  318. setupKeys();
  319. initTerrain();
  320. initCamera();
  321. }
  322. @Override
  323. public void simpleUpdate(float tpf) {
  324. updateMovement(tpf);
  325. }
  326. private void updateMovement(float tpf) {
  327. if (left || right || up || down) {
  328. // here we increment the speed using the tpf
  329. camSpeed += tpf * 2;
  330. System.out.print("Camera speed " + camSpeed + "; Location:" + cam.getLocation().toString()
  331. + "; direction:"+cam.getDirection()+"walkDirection:" + walkDirection.toString());
  332. }
  333. Vector3f camDir = cam.getDirection().clone().multLocal(camSpeed);
  334. Vector3f camLeft = cam.getLeft().clone().multLocal(camSpeed);
  335. // adjust position of rigid body for collisions
  336. walkDirection.set(cam.getLocation().clone());
  337. if (left) {
  338. walkDirection.addLocal(camLeft);
  339. }
  340. if (right) {
  341. walkDirection.addLocal(camLeft.negate());
  342. }
  343. if (up) {
  344. walkDirection.addLocal(camDir);
  345. }
  346. if (down) {
  347. walkDirection.addLocal(camDir.negate());
  348. }
  349. // player.setWalkDirection(walkDirection);
  350. cam.setLocation(walkDirection);
  351. if (left || right || up || down) {
  352. System.out.println("; new walkDirection:" + walkDirection);
  353. }
  354. }
  355. private void initTerrain() {
  356. // First, we load up our textures and the heightmap texture for the terrain
  357. // TERRAIN TEXTURE material
  358. matTerrain = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
  359. matTerrain.setBoolean("useTriPlanarMapping", false);
  360. matTerrain.setFloat("Shininess", 0.0f);
  361. // ALPHA map (for splat textures)
  362. matTerrain.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alpha1.png"));
  363. matTerrain.setTexture("AlphaMap_1", assetManager.loadTexture("Textures/Terrain/splat/alpha2.png"));
  364. // HEIGHTMAP image (for the terrain heightmap)
  365. Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
  366. // GRASS texture
  367. Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
  368. grass.setWrap(WrapMode.Repeat);
  369. // DIRT texture
  370. Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
  371. dirt.setWrap(WrapMode.Repeat);
  372. matTerrain.setTexture("DiffuseMap", dirt);
  373. matTerrain.setFloat("DiffuseMap_0_scale", dirtScale);
  374. // ROCK texture
  375. Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
  376. rock.setWrap(WrapMode.Repeat);
  377. // BRICK texture
  378. Texture brick = assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg");
  379. brick.setWrap(WrapMode.Repeat);
  380. // RIVER ROCK texture
  381. Texture riverRock = assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg");
  382. riverRock.setWrap(WrapMode.Repeat);
  383. // WIREFRAME material
  384. matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
  385. matWire.getAdditionalRenderState().setWireframe(true);
  386. matWire.setColor("Color", ColorRGBA.Green);
  387. // CREATE HEIGHTMAP
  388. AbstractHeightMap heightmap = null;
  389. try {
  390. heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.5f);
  391. heightmap.load();
  392. heightmap.smooth(0.9f, 1);
  393. } catch (Exception e) {
  394. e.printStackTrace();
  395. }
  396. /*
  397. * Here we create the actual terrain. The tiles will be 65x65, and the total size of the
  398. * terrain will be 513x513. It uses the heightmap we created to generate the height values.
  399. */
  400. /**
  401. * Optimal terrain patch size is 65 (64x64).
  402. * The total size is up to you. At 1025 it ran fine for me (200+FPS), however at
  403. * size=2049, it got really slow. But that is a jump from 2 million to 8 million triangles...
  404. */
  405. terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());//, new LodPerspectiveCalculatorFactory(getCamera(), 4)); // add this in to see it use entropy for LOD calculations
  406. TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
  407. control.setLodCalculator(new DistanceLodCalculator(65, 2.7f)); // patch size, and a multiplier
  408. terrain.addControl(control);
  409. terrain.setMaterial(matTerrain);
  410. terrain.setModelBound(new BoundingBox());
  411. terrain.updateModelBound();
  412. terrain.setLocalTranslation(0, -100, 0);
  413. terrain.setLocalScale(1f, 1f, 1f);
  414. rootNode.attachChild(terrain);
  415. DirectionalLight light = new DirectionalLight();
  416. light.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalize());
  417. rootNode.addLight(light);
  418. }
  419. private void setupKeys() {
  420. inputManager.addMapping(Actions.LEFT.name(), new KeyTrigger(KeyInput.KEY_A));
  421. inputManager.addMapping(Actions.RIGHT.name(), new KeyTrigger(KeyInput.KEY_D));
  422. inputManager.addMapping(Actions.UP.name(), new KeyTrigger(KeyInput.KEY_W));
  423. inputManager.addMapping(Actions.DOWN.name(), new KeyTrigger(KeyInput.KEY_S));
  424. inputManager.addListener(this, Actions.LEFT.name());
  425. inputManager.addListener(this, Actions.RIGHT.name());
  426. inputManager.addListener(this, Actions.UP.name());
  427. inputManager.addListener(this, Actions.DOWN.name());
  428. }
  429. /**
  430. * These are our custom actions triggered by key presses. We do not walk yet, we just keep track of the direction
  431. * the user pressed.
  432. */
  433. @Override
  434. public void onAction(String name, boolean keyPressed, float tpf) {
  435. System.out.println("name:" + name + "; keyPressed:" + keyPressed);
  436. if (name.equals(Actions.LEFT.name())) {
  437. left = keyPressed;
  438. if (!keyPressed) { // if the key isn't pressed anymore reset the speed to the initial value
  439. camSpeed = STARTING_CAM_SPEED;
  440. }
  441. } else if (name.equals(Actions.RIGHT.name())) {
  442. right = keyPressed;
  443. if (!keyPressed) {
  444. camSpeed = STARTING_CAM_SPEED;
  445. }
  446. } else if (name.equals(Actions.UP.name())) {
  447. up = keyPressed;
  448. if (!keyPressed) {
  449. camSpeed = STARTING_CAM_SPEED;
  450. }
  451. } else if (name.equals(Actions.DOWN.name())) {
  452. down = keyPressed;
  453. if (!keyPressed) {
  454. camSpeed = STARTING_CAM_SPEED;
  455. }
  456. }
  457. }
  458. }</code></pre>
  459. </div>
  460. </div>
  461. </article>
  462. <aside class="toc sidebar" data-title="Contents" data-levels="2">
  463. <div class="toc-menu"></div>
  464. </aside>
  465. </div>
  466. </main>
  467. </div>
  468. <footer class="footer">
  469. <p>Copyright 2020 jMonkeyEngine Wiki Contributors. Licensed BSD-3.</p>
  470. </footer>
  471. <script src="../../../../_/js/vendor/docsearch.min.js"></script>
  472. <!-- fetched from https://cdn.jsdelivr.net/npm/docsearch.js@2/dist/cdn/docsearch.min.js -->
  473. <script>
  474. var search = docsearch({
  475. apiKey: 'a736b6d93de805e26ec2f49b55013fbd',
  476. indexName: 'jmonkeyengine',
  477. inputSelector: '#search-input',
  478. autocompleteOptions: { hint: false, keyboardShortcuts: ['s'] },
  479. algoliaOptions: { hitsPerPage: 10 }
  480. }).autocomplete
  481. search.on('autocomplete:closed', function () { search.autocomplete.setVal() })
  482. function focusSearchInput () { document.querySelector('#search-input').focus() }
  483. if (document.querySelector('.home-link.is-current')) window.addEventListener('load', focusSearchInput)
  484. </script>
  485. <script src="../../../../_/js/site.js"></script>
  486. <script async src="../../../../_/js/vendor/highlight.js"></script>
  487. </body>
  488. </html>