-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Extract common free floating camera from game and model viewer
fix #120
- Loading branch information
Showing
3 changed files
with
64 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
cake/core/src/main/kotlin/org/demoth/cake/clientcommon/FlyingCameraController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.demoth.cake.clientcommon | ||
|
||
import com.badlogic.gdx.Gdx | ||
import com.badlogic.gdx.graphics.Camera | ||
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController | ||
import com.badlogic.gdx.math.Vector3 | ||
|
||
class FlyingCameraController(camera: Camera): CameraInputController(camera) { | ||
private val right = Vector3() | ||
val movementDirection = Vector3() | ||
|
||
init { | ||
translateUnits = 500f | ||
} | ||
|
||
override fun process(deltaX: Float, deltaY: Float, button: Int): Boolean { | ||
if (button == rotateButton) { | ||
// PITCH: rotate around local "right" axis | ||
right.set(camera.direction).crs(camera.up).nor() | ||
camera.rotateAround(target, right, deltaY * rotateAngle) | ||
|
||
// YAW: rotate around global Z | ||
camera.rotateAround(target, Vector3.Z, -deltaX * rotateAngle) | ||
|
||
if (autoUpdate) camera.update() | ||
} else { | ||
super.process(deltaX, deltaY, button) | ||
} | ||
return true | ||
} | ||
|
||
override fun update() { | ||
// WASD movement | ||
if (rotateRightPressed || rotateLeftPressed || forwardPressed || backwardPressed) { | ||
movementDirection.set(0f, 0f, 0f) | ||
if (rotateRightPressed) { | ||
movementDirection.set(camera.direction) | ||
movementDirection.crs(camera.up) | ||
movementDirection.scl(-1f) | ||
} else if (rotateLeftPressed) { | ||
movementDirection.set(camera.direction) | ||
movementDirection.crs(camera.up) | ||
} | ||
if (forwardPressed) { | ||
movementDirection.add(camera.direction) | ||
} else if (backwardPressed) { | ||
movementDirection.add(-camera.direction.x, -camera.direction.y, -camera.direction.z) | ||
} | ||
movementDirection.nor()//malize | ||
camera.translate(movementDirection.scl(Gdx.graphics.deltaTime * translateUnits)) | ||
target.add(movementDirection) | ||
|
||
if (autoUpdate) camera.update() | ||
|
||
} else { | ||
super.update() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters