Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Input;
- import com.badlogic.gdx.InputAdapter;
- import com.badlogic.gdx.graphics.Camera;
- import com.badlogic.gdx.math.Vector3;
- import com.badlogic.gdx.utils.IntIntMap;
- public class SICameraController extends InputAdapter {
- private final Camera camera;
- private final IntIntMap keys = new IntIntMap();
- private int STRAFE_LEFT = Input.Keys.A;
- private int STRAFE_RIGHT = Input.Keys.D;
- private int FORWARD = Input.Keys.W;
- private int BACKWARD = Input.Keys.S;
- private int UP = Input.Keys.SPACE;
- private int DOWN = Input.Keys.SHIFT_LEFT;
- private float velocity = 5;
- private float degreesPerPixel = 0.5f;
- private final Vector3 tmp = new Vector3();
- private final Vector3 dir = new Vector3();
- public SICameraController (Camera camera) {
- this.camera = camera;
- }
- @Override
- public boolean keyDown (int keycode) {
- keys.put(keycode, keycode);
- return true;
- }
- @Override
- public boolean keyUp (int keycode) {
- keys.remove(keycode, 0);
- return true;
- }
- /** Sets the velocity in units per second for moving forward, backward and strafing left/right.
- * @param velocity the velocity in units per second */
- public void setVelocity (float velocity) {
- this.velocity = velocity;
- }
- /** Sets how many degrees to rotate per pixel the mouse moved.
- * @param degreesPerPixel */
- public void setDegreesPerPixel (float degreesPerPixel) {
- this.degreesPerPixel = degreesPerPixel;
- }
- @Override
- public boolean touchDragged (int screenX, int screenY, int pointer) {
- float deltaX = -Gdx.input.getDeltaX() * degreesPerPixel;
- float deltaY = -Gdx.input.getDeltaY() * degreesPerPixel;
- camera.direction.rotate(camera.up, deltaX);
- tmp.set(camera.direction).crs(camera.up).nor();
- dir.set(camera.direction).crs(camera.up).nor();
- camera.direction.rotate(tmp, deltaY);
- // camera.up.rotate(tmp, deltaY);
- return true;
- }
- public void update () {
- update(Gdx.graphics.getDeltaTime());
- }
- public void update (float deltaTime) {
- if (keys.containsKey(FORWARD)) {
- tmp.set(dir).crs(camera.up).nor().scl(-deltaTime * velocity);
- camera.position.add(tmp);
- }
- if (keys.containsKey(BACKWARD)) {
- tmp.set(dir).crs(camera.up).nor().scl(deltaTime * velocity);
- camera.position.add(tmp);
- }
- if (keys.containsKey(STRAFE_LEFT)) {
- tmp.set(dir).nor().scl(-deltaTime * velocity);
- camera.position.add(tmp);
- }
- if (keys.containsKey(STRAFE_RIGHT)) {
- tmp.set(dir).nor().scl(deltaTime * velocity);
- camera.position.add(tmp);
- }
- if (keys.containsKey(UP)) {
- tmp.set(camera.up).nor().scl(deltaTime * velocity);
- camera.position.add(tmp);
- }
- if (keys.containsKey(DOWN)) {
- tmp.set(camera.up).nor().scl(-deltaTime * velocity);
- camera.position.add(tmp);
- }
- camera.update(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement