What the title says. Is it possible?
As far as I know, there is no getKeyPressed
or anything of the sort. I want to use a switch case for organization (and practice) as well as speed (or so I was informed).
Basically, the switch case clause(?) is just a boolean return. But how can I check if a key is pressed based on a value passed into it without a pasta bowl of if / else statements?
Obviously this code doesn't work, but I'm looking for something like it.
public void moveCamera() {
switch (Keyboard.isKeyDown(!!!CASE CHECKING HERE!!!)) {
case Keyboard.KEY_W:
position.z -= MOVE_SPEED;
break;
case Keyboard.KEY_A:
position.x += MOVE_SPEED;
break;
case Keyboard.KEY_S:
position.z -= MOVE_SPEED;
break;
case Keyboard.KEY_D:
position.x += MOVE_SPEED;
break;
}
}
Here is a solution that I liked (but did not try) from @KysonTyner.
You can switch on
getEventKey()
. This will hit your current cases and then you can wrap the whole switch statement with if(getEventKeyState()) {switch/case}
. There is no need to use an event listener. – Kylon Tyner
As for what I used, I abandoned the idea of a switch entirely, as I realized that the movement speed would multiply if there were more than one direction of input.
I wanted to support multiple directions of motion for diagonal movement, so it wouldn't be "ghosting" as keys were pressed.
What I did is separate the functions, one to test keyboard input, and one to move the camera. There doesn't have to be any special logic for the key input, as it simply passes an LWJGL Vector3f
to the camera move function.
Then, in camera move, I normalized that vector and altered the positions accordingly.
This is a simplified version of my Camera class for demonstration. I removed everything except translation.
package spikespaz.engine.main;
import org.lwjgl.input.Keyboard;
import org.lwjgl.util.vector.Vector3f;
// Created by spike on 7/3/2017.
public final class Camera {
private Camera() {}
private float moveSpeed = 0.2f;
private Vector3f position = new Vector3f(0, 0, 0);
public Camera(Vector3f position, Vector3f rotation) {
this.position = position;
}
public void updateKeyInput() {
Vector3f direction = new Vector3f(0, 0, 0);
if (Keyboard.isKeyDown(Keyboard.KEY_W)) {
direction.x = 1;
} else if (Keyboard.isKeyDown(Keyboard.KEY_S)) {
direction.x = -1;
}
if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
direction.y = -1;
} else if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT)) {
direction.y = 1;
}
if (Keyboard.isKeyDown(Keyboard.KEY_A)) {
direction.z = 1;
} else if (Keyboard.isKeyDown(Keyboard.KEY_D)) {
direction.z = -1;
}
translate(direction);
}
private void translate(Vector3f direction) {
Vector3f normalized = direction.normalise(null);
Vector3f.add(position, (Vector3f) normalized.scale(moveSpeed), position);
}
public float getMoveSpeed() {
return moveSpeed;
}
public void setMoveSpeed(float moveSpeed) {
this.moveSpeed = moveSpeed;
}
}
Translate is relative. In the full class I have another function to set absolute position, without the move speed.
In the above example, the input vector for translate is simply a direction. The function should coordinate that based on the movement speed for each frame.