Advertisement
mttprvst13

LibGDX Camera Input Controller

Apr 14th, 2016
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. import com.badlogic.gdx.Gdx;
  2. import com.badlogic.gdx.Input;
  3. import com.badlogic.gdx.InputAdapter;
  4. import com.badlogic.gdx.graphics.Camera;
  5. import com.badlogic.gdx.math.Vector3;
  6. import com.badlogic.gdx.utils.IntIntMap;
  7.  
  8. public class SICameraController extends InputAdapter {
  9.     private final Camera camera;
  10.     private final IntIntMap keys = new IntIntMap();
  11.     private int STRAFE_LEFT = Input.Keys.A;
  12.     private int STRAFE_RIGHT = Input.Keys.D;
  13.     private int FORWARD = Input.Keys.W;
  14.     private int BACKWARD = Input.Keys.S;
  15.     private int UP = Input.Keys.SPACE;
  16.     private int DOWN = Input.Keys.SHIFT_LEFT;
  17.     private float velocity = 5;
  18.     private float degreesPerPixel = 0.5f;
  19.     private final Vector3 tmp = new Vector3();
  20.     private final Vector3 dir = new Vector3();
  21.  
  22.     public SICameraController (Camera camera) {
  23.         this.camera = camera;
  24.     }
  25.  
  26.     @Override
  27.     public boolean keyDown (int keycode) {
  28.         keys.put(keycode, keycode);
  29.         return true;
  30.     }
  31.  
  32.     @Override
  33.     public boolean keyUp (int keycode) {
  34.         keys.remove(keycode, 0);
  35.         return true;
  36.     }
  37.  
  38.     /** Sets the velocity in units per second for moving forward, backward and strafing left/right.
  39.      * @param velocity the velocity in units per second */
  40.     public void setVelocity (float velocity) {
  41.         this.velocity = velocity;
  42.     }
  43.  
  44.     /** Sets how many degrees to rotate per pixel the mouse moved.
  45.      * @param degreesPerPixel */
  46.     public void setDegreesPerPixel (float degreesPerPixel) {
  47.         this.degreesPerPixel = degreesPerPixel;
  48.     }
  49.  
  50.     @Override
  51.     public boolean touchDragged (int screenX, int screenY, int pointer) {
  52.         float deltaX = -Gdx.input.getDeltaX() * degreesPerPixel;
  53.         float deltaY = -Gdx.input.getDeltaY() * degreesPerPixel;
  54.         camera.direction.rotate(camera.up, deltaX);
  55.         tmp.set(camera.direction).crs(camera.up).nor();
  56.         dir.set(camera.direction).crs(camera.up).nor();
  57.         camera.direction.rotate(tmp, deltaY);
  58. // camera.up.rotate(tmp, deltaY);
  59.         return true;
  60.     }
  61.  
  62.     public void update () {
  63.         update(Gdx.graphics.getDeltaTime());
  64.     }
  65.  
  66.     public void update (float deltaTime) {
  67.         if (keys.containsKey(FORWARD)) {
  68.             tmp.set(dir).crs(camera.up).nor().scl(-deltaTime * velocity);
  69.             camera.position.add(tmp);
  70.         }
  71.         if (keys.containsKey(BACKWARD)) {
  72.             tmp.set(dir).crs(camera.up).nor().scl(deltaTime * velocity);
  73.             camera.position.add(tmp);
  74.         }
  75.         if (keys.containsKey(STRAFE_LEFT)) {
  76.             tmp.set(dir).nor().scl(-deltaTime * velocity);
  77.             camera.position.add(tmp);
  78.         }
  79.         if (keys.containsKey(STRAFE_RIGHT)) {
  80.             tmp.set(dir).nor().scl(deltaTime * velocity);
  81.             camera.position.add(tmp);
  82.         }
  83.         if (keys.containsKey(UP)) {
  84.             tmp.set(camera.up).nor().scl(deltaTime * velocity);
  85.             camera.position.add(tmp);
  86.         }
  87.         if (keys.containsKey(DOWN)) {
  88.             tmp.set(camera.up).nor().scl(-deltaTime * velocity);
  89.             camera.position.add(tmp);
  90.         }
  91.         camera.update(true);
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement