jMonkeyEngine 3 Tutorial (5) - Hello Input System - Variation over time key is pressed¶
Parent: jMonkeyEngine 3 Tutorial (5) - Hello Input System
This sample accelerates the camera while movement keys are held. Add jme3-terrain and the separate jme3-testdata module to your project for the terrain and example textures. Movement is scaled by tpf so speed is expressed in world units per second.
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() {
flyCam.setEnabled(false); // custom bindings below control camera movement
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 * tpf);
Vector3f camLeft = cam.getLeft().clone().multLocal(camSpeed * tpf);
// Calculate the new camera position; this example does not use physics.
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
// TERRAIN TEXTURE material
matTerrain = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
matTerrain.setBoolean("useTriPlanarMapping", false);
matTerrain.setFloat("Shininess", 0.0f);
// ALPHA map (for splat textures)
matTerrain.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alpha1.png"));
matTerrain.setTexture("AlphaMap_1", assetManager.loadTexture("Textures/Terrain/splat/alpha2.png"));
// HEIGHTMAP image (for the terrain heightmap)
Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
// GRASS texture
Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
grass.setWrap(WrapMode.Repeat);
// DIRT texture
Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
dirt.setWrap(WrapMode.Repeat);
matTerrain.setTexture("DiffuseMap", dirt);
matTerrain.setFloat("DiffuseMap_0_scale", dirtScale);
// ROCK texture
Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
rock.setWrap(WrapMode.Repeat);
// BRICK texture
Texture brick = assetManager.loadTexture("Textures/Terrain/BrickWall/BrickWall.jpg");
brick.setWrap(WrapMode.Repeat);
// RIVER ROCK texture
Texture riverRock = assetManager.loadTexture("Textures/Terrain/Pond/Pond.jpg");
riverRock.setWrap(WrapMode.Repeat);
// WIREFRAME material
matWire = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
matWire.getAdditionalRenderState().setWireframe(true);
matWire.setColor("Color", ColorRGBA.Green);
// CREATE HEIGHTMAP
AbstractHeightMap heightmap = null;
try {
heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.5f);
heightmap.load();
heightmap.smooth(0.9f, 1);
} catch (Exception e) {
e.printStackTrace();
}
/*
* Here we create the actual terrain. The tiles will be 65x65, and the total size of the
* terrain will be 513x513. It uses the heightmap we created to generate the height values.
*/
/**
* Optimal terrain patch size is 65 (64x64).
* The total size is up to you. At 1025 it ran fine for me (200+FPS), however at
* size=2049, it got really slow. But that is a jump from 2 million to 8 million triangles...
*/
// The bundled image is 512x512; extend its edge for a 513x513 terrain.
float[] source = heightmap.getHeightMap();
float[] heights = new float[513 * 513];
for (int z = 0; z < 513; z++) {
for (int x = 0; x < 513; x++) {
heights[z * 513 + x] = source[Math.min(z, 511) * 512 + Math.min(x, 511)];
}
}
terrain = new TerrainQuad("terrain", 65, 513, heights);
TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
control.setLodCalculator(new DistanceLodCalculator(65, 2.7f)); // patch size, and a multiplier
terrain.addControl(control);
terrain.setMaterial(matTerrain);
terrain.setModelBound(new BoundingBox());
terrain.updateModelBound();
terrain.setLocalTranslation(0, -100, 0);
terrain.setLocalScale(1f, 1f, 1f);
rootNode.attachChild(terrain);
DirectionalLight light = new DirectionalLight();
light.setDirection((new Vector3f(-0.5f, -0.5f, -0.5f)).normalize());
rootNode.addLight(light);
}
private void setupKeys() {
inputManager.addMapping(Actions.LEFT.name(), new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping(Actions.RIGHT.name(), new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping(Actions.UP.name(), new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping(Actions.DOWN.name(), new KeyTrigger(KeyInput.KEY_S));
inputManager.addListener(this, Actions.LEFT.name());
inputManager.addListener(this, Actions.RIGHT.name());
inputManager.addListener(this, Actions.UP.name());
inputManager.addListener(this, Actions.DOWN.name());
}
/**
* These are our custom actions triggered by key presses. We do not walk yet, we just keep track of the direction
* the user pressed.
*/
@Override
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals(Actions.LEFT.name())) {
left = keyPressed;
} else if (name.equals(Actions.RIGHT.name())) {
right = keyPressed;
} else if (name.equals(Actions.UP.name())) {
up = keyPressed;
} else if (name.equals(Actions.DOWN.name())) {
down = keyPressed;
}
if (!left && !right && !up && !down) {
camSpeed = STARTING_CAM_SPEED;
}
}
}