1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.graphics.g3d.utils; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.Input.Keys; 21 import com.badlogic.gdx.InputAdapter; 22 import com.badlogic.gdx.graphics.Camera; 23 import com.badlogic.gdx.math.Vector3; 24 import com.badlogic.gdx.utils.IntIntMap; 25 26 /** Takes a {@link Camera} instance and controls it via w,a,s,d and mouse panning. 27 * @author badlogic */ 28 public class FirstPersonCameraController extends InputAdapter { 29 private final Camera camera; 30 private final IntIntMap keys = new IntIntMap(); 31 private int STRAFE_LEFT = Keys.A; 32 private int STRAFE_RIGHT = Keys.D; 33 private int FORWARD = Keys.W; 34 private int BACKWARD = Keys.S; 35 private int UP = Keys.Q; 36 private int DOWN = Keys.E; 37 private float velocity = 5; 38 private float degreesPerPixel = 0.5f; 39 private final Vector3 tmp = new Vector3(); 40 41 public FirstPersonCameraController (Camera camera) { 42 this.camera = camera; 43 } 44 45 @Override 46 public boolean keyDown (int keycode) { 47 keys.put(keycode, keycode); 48 return true; 49 } 50 51 @Override 52 public boolean keyUp (int keycode) { 53 keys.remove(keycode, 0); 54 return true; 55 } 56 57 /** Sets the velocity in units per second for moving forward, backward and strafing left/right. 58 * @param velocity the velocity in units per second */ 59 public void setVelocity (float velocity) { 60 this.velocity = velocity; 61 } 62 63 /** Sets how many degrees to rotate per pixel the mouse moved. 64 * @param degreesPerPixel */ 65 public void setDegreesPerPixel (float degreesPerPixel) { 66 this.degreesPerPixel = degreesPerPixel; 67 } 68 69 @Override 70 public boolean touchDragged (int screenX, int screenY, int pointer) { 71 float deltaX = -Gdx.input.getDeltaX() * degreesPerPixel; 72 float deltaY = -Gdx.input.getDeltaY() * degreesPerPixel; 73 camera.direction.rotate(camera.up, deltaX); 74 tmp.set(camera.direction).crs(camera.up).nor(); 75 camera.direction.rotate(tmp, deltaY); 76 // camera.up.rotate(tmp, deltaY); 77 return true; 78 } 79 80 public void update () { 81 update(Gdx.graphics.getDeltaTime()); 82 } 83 84 public void update (float deltaTime) { 85 if (keys.containsKey(FORWARD)) { 86 tmp.set(camera.direction).nor().scl(deltaTime * velocity); 87 camera.position.add(tmp); 88 } 89 if (keys.containsKey(BACKWARD)) { 90 tmp.set(camera.direction).nor().scl(-deltaTime * velocity); 91 camera.position.add(tmp); 92 } 93 if (keys.containsKey(STRAFE_LEFT)) { 94 tmp.set(camera.direction).crs(camera.up).nor().scl(-deltaTime * velocity); 95 camera.position.add(tmp); 96 } 97 if (keys.containsKey(STRAFE_RIGHT)) { 98 tmp.set(camera.direction).crs(camera.up).nor().scl(deltaTime * velocity); 99 camera.position.add(tmp); 100 } 101 if (keys.containsKey(UP)) { 102 tmp.set(camera.up).nor().scl(deltaTime * velocity); 103 camera.position.add(tmp); 104 } 105 if (keys.containsKey(DOWN)) { 106 tmp.set(camera.up).nor().scl(-deltaTime * velocity); 107 camera.position.add(tmp); 108 } 109 camera.update(true); 110 } 111 } 112