<commit_before>= jMonkeyEngine 3 Tutorial (5) - Hello Input System - Variation over time key is pressed
:author: 
:revnumber: 
:revdate: 2016/03/17 20:48
:relfileprefix: ../../../
:imagesdir: ../../..
ifdef::env-github,env-browser[:outfilesuffix: .adoc]


Parent: <<jme3/beginner/hello_input_system#,jMonkeyEngine 3 Tutorial (5) - Hello Input System>>

Here is a sample of a program where the camera accelerates if the key is kept pressed.

[source,java]
----
import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.light.PointLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
import com.jme3.terrain.heightmap.AbstractHeightMap;
import com.jme3.terrain.heightmap.ImageBasedHeightMap;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;

public class Accelerate extends SimpleApplication implements ActionListener {

    private TerrainQuad terrain;
    Material matTerrain;
    Material matWire;
    boolean wireframe = false;
    boolean triPlanar = false;
    boolean wardiso = false;
    boolean minnaert = false;
    protected BitmapText hintText;
    PointLight pl;
    Geometry lightMdl;
    private float dirtScale = 16;

    private enum Actions {

        LEFT, RIGHT, UP, DOWN;
    }
    private final int CAMERA_HEIGHT = 250;
    private final int STARTING_CAM_SPEED = 1;
    private final Vector3f CAM_DIRECTION = new Vector3f(0, -0.3f, 1f);
    private final Vector3f CAM_LOCATION = new Vector3f(-1, 0, -CAMERA_HEIGHT);
    private final Vector3f walkDirection = new Vector3f();
    private boolean left = false;
    private boolean right = false;
    private boolean up = false;
    private boolean down = false;
    private float camSpeed = STARTING_CAM_SPEED;

    private void initCamera() {
        cam.setLocation(CAM_LOCATION);
        cam.lookAtDirection(CAM_DIRECTION.normalizeLocal(), Vector3f.UNIT_Y.clone().addLocal(0, 2, 0));
    }

    public static void main(String[] args) {
        Accelerate app = new Accelerate();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        setupKeys();
        initTerrain();
        initCamera();
    }

    @Override
    public void simpleUpdate(float tpf) {
        updateMovement(tpf);
    }

    private void updateMovement(float tpf) {
        if (left || right || up || down) {
            // here we increment the speed using the tpf
            camSpeed += tpf * 2;
            System.out.print("Camera speed " + camSpeed + ";  Location:" + cam.getLocation().toString() 
                    + ";  direction:"+cam.getDirection()+"walkDirection:" + walkDirection.toString());
        }
        Vector3f camDir = cam.getDirection().clone().multLocal(camSpeed);
        Vector3f camLeft = cam.getLeft().clone().multLocal(camSpeed);
        // adjust position of rigid body for collisions
        walkDirection.set(cam.getLocation().clone());
        if (left) {
            walkDirection.addLocal(camLeft);
        }
        if (right) {
            walkDirection.addLocal(camLeft.negate());
        }
        if (up) {
            walkDirection.addLocal(camDir);
        }
        if (down) {
            walkDirection.addLocal(camDir.negate());
        }
//        player.setWalkDirection(walkDirection);
        cam.setLocation(walkDirection);
        if (left || right || up || down) {
            System.out.println("; new walkDirection:" + walkDirection);
        }
    }

    private void initTerrain() {
        // First, we load up our textures and the heightmap texture for the terrain<commit_msg>fix xref<commit_after>= jMonkeyEngine 3 Tutorial (5) - Hello Input System - Variation over time key is pressed
:author:
:revnumber:
:revdate: 2016/03/17 20:48
:relfileprefix: ../../../
:imagesdir: ../../..
ifdef::env-github,env-browser[:outfilesuffix: .adoc]


Parent: xref:beginner/hello_input_system.adoc[jMonkeyEngine 3 Tutorial (5) - Hello Input System]

Here is a sample of a program where the camera accelerates if the key is kept pressed.

[source,java]
----
import com.jme3.app.SimpleApplication;
import com.jme3.bounding.BoundingBox;
import com.jme3.font.BitmapText;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.light.DirectionalLight;
import com.jme3.light.PointLight;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
import com.jme3.terrain.heightmap.AbstractHeightMap;
import com.jme3.terrain.heightmap.ImageBasedHeightMap;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;

public class Accelerate extends SimpleApplication implements ActionListener {

    private TerrainQuad terrain;
    Material matTerrain;
    Material matWire;
    boolean wireframe = false;
    boolean triPlanar = false;
    boolean wardiso = false;
    boolean minnaert = false;
    protected BitmapText hintText;
    PointLight pl;
    Geometry lightMdl;
    private float dirtScale = 16;

    private enum Actions {

        LEFT, RIGHT, UP, DOWN;
    }
    private final int CAMERA_HEIGHT = 250;
    private final int STARTING_CAM_SPEED = 1;
    private final Vector3f CAM_DIRECTION = new Vector3f(0, -0.3f, 1f);
    private final Vector3f CAM_LOCATION = new Vector3f(-1, 0, -CAMERA_HEIGHT);
    private final Vector3f walkDirection = new Vector3f();
    private boolean left = false;
    private boolean right = false;
    private boolean up = false;
    private boolean down = false;
    private float camSpeed = STARTING_CAM_SPEED;

    private void initCamera() {
        cam.setLocation(CAM_LOCATION);
        cam.lookAtDirection(CAM_DIRECTION.normalizeLocal(), Vector3f.UNIT_Y.clone().addLocal(0, 2, 0));
    }

    public static void main(String[] args) {
        Accelerate app = new Accelerate();
        app.start();
    }

    @Override
    public void simpleInitApp() {
        setupKeys();
        initTerrain();
        initCamera();
    }

    @Override
    public void simpleUpdate(float tpf) {
        updateMovement(tpf);
    }

    private void updateMovement(float tpf) {
        if (left || right || up || down) {
            // here we increment the speed using the tpf
            camSpeed += tpf * 2;
            System.out.print("Camera speed " + camSpeed + ";  Location:" + cam.getLocation().toString()
                    + ";  direction:"+cam.getDirection()+"walkDirection:" + walkDirection.toString());
        }
        Vector3f camDir = cam.getDirection().clone().multLocal(camSpeed);
        Vector3f camLeft = cam.getLeft().clone().multLocal(camSpeed);
        // adjust position of rigid body for collisions
        walkDirection.set(cam.getLocation().clone());
        if (left) {
            walkDirection.addLocal(camLeft);
        }
        if (right) {
            walkDirection.addLocal(camLeft.negate());
        }
        if (up) {
            walkDirection.addLocal(camDir);
        }
        if (down) {
            walkDirection.addLocal(camDir.negate());
        }
//        player.setWalkDirection(walkDirection);
        cam.setLocation(walkDirection);
        if (left || right || up || down) {
            System.out.println("; new walkDirection:" + walkDirection);
        }
    }

    private void initTerrain() {
        // First, we load up our textures and the heightmap texture for the terrain
{'fraction_non_alphanumeric': 0.10842170929507174, 'fraction_numerical': 0.015346225826575171, 'mean_word_length': 2.978163771712159, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '141977', 'n_tokens_mistral': 2544, 'n_tokens_neox': 2416, 'n_words': 588}