Skip to content

Commit

Permalink
Camera now smoothly animates when focusing on the selection
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaskelly committed May 1, 2020
1 parent cac5c70 commit c41dab7
Showing 1 changed file with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ public class EditorCameraController extends InputAdapter implements EditorSubsys
double maxRot = 0.8;

int scrollAmount;


boolean isMoving;
float currentMoveTime;
final float moveTime = 0.0625f;
final Vector3 moveStart = new Vector3();
final Vector3 moveEnd = new Vector3();

public EditorCameraController() {}

@Override
Expand Down Expand Up @@ -173,6 +179,20 @@ else if (scrollAmount > 0) {
position.set(cameraNewDirection.x, cameraNewDirection.z, cameraNewDirection.y);
}

// Handle smoothly lerping to a destination.
if (isMoving) {
currentMoveTime += Gdx.graphics.getDeltaTime();

position.set(moveStart);
position.lerp(moveEnd, currentMoveTime / moveTime);

if (currentMoveTime >= moveTime) {
currentMoveTime = 0;
isMoving = false;
position.set(moveEnd);
}
}

camera.position.set(position.x, position.z, position.y);

scrollAmount = 0;
Expand Down Expand Up @@ -239,6 +259,7 @@ public boolean scrolled(int amount) {
return false;
}

/** Position the camera to view the current selection. */
public void viewSelected() {
PerspectiveCamera camera = Editor.app.camera;
Level level = Editor.app.level;
Expand Down Expand Up @@ -268,6 +289,14 @@ else if (Editor.app.selected) {

Vector3 cameraOffset = new Vector3(camera.direction.x,camera.direction.z,camera.direction.y).scl(orbitDistance);
Vector3 finalPosition = new Vector3(selectedPosition).sub(cameraOffset);
position.set(finalPosition);
moveTo(finalPosition);
}

/** Smoothly move to given destination. */
public void moveTo(Vector3 destination) {
moveStart.set(position);
moveEnd.set(destination);
isMoving = true;
currentMoveTime = 0;
}
}

0 comments on commit c41dab7

Please sign in to comment.