Skip to content

Commit c41dab7

Browse files
committed
Camera now smoothly animates when focusing on the selection
1 parent cac5c70 commit c41dab7

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

DelvEdit/src/com/interrupt/dungeoneer/editor/EditorCameraController.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ public class EditorCameraController extends InputAdapter implements EditorSubsys
2929
double maxRot = 0.8;
3030

3131
int scrollAmount;
32-
32+
33+
boolean isMoving;
34+
float currentMoveTime;
35+
final float moveTime = 0.0625f;
36+
final Vector3 moveStart = new Vector3();
37+
final Vector3 moveEnd = new Vector3();
38+
3339
public EditorCameraController() {}
3440

3541
@Override
@@ -173,6 +179,20 @@ else if (scrollAmount > 0) {
173179
position.set(cameraNewDirection.x, cameraNewDirection.z, cameraNewDirection.y);
174180
}
175181

182+
// Handle smoothly lerping to a destination.
183+
if (isMoving) {
184+
currentMoveTime += Gdx.graphics.getDeltaTime();
185+
186+
position.set(moveStart);
187+
position.lerp(moveEnd, currentMoveTime / moveTime);
188+
189+
if (currentMoveTime >= moveTime) {
190+
currentMoveTime = 0;
191+
isMoving = false;
192+
position.set(moveEnd);
193+
}
194+
}
195+
176196
camera.position.set(position.x, position.z, position.y);
177197

178198
scrollAmount = 0;
@@ -239,6 +259,7 @@ public boolean scrolled(int amount) {
239259
return false;
240260
}
241261

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

269290
Vector3 cameraOffset = new Vector3(camera.direction.x,camera.direction.z,camera.direction.y).scl(orbitDistance);
270291
Vector3 finalPosition = new Vector3(selectedPosition).sub(cameraOffset);
271-
position.set(finalPosition);
292+
moveTo(finalPosition);
293+
}
294+
295+
/** Smoothly move to given destination. */
296+
public void moveTo(Vector3 destination) {
297+
moveStart.set(position);
298+
moveEnd.set(destination);
299+
isMoving = true;
300+
currentMoveTime = 0;
272301
}
273302
}

0 commit comments

Comments
 (0)