Skip to content

Commit

Permalink
feat: Extract common free floating camera from game and model viewer
Browse files Browse the repository at this point in the history
fix #120
  • Loading branch information
demoth committed Jan 24, 2025
1 parent f941902 commit 525f620
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 54 deletions.
25 changes: 2 additions & 23 deletions cake/core/src/main/java/org/demoth/cake/stages/Game3dScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.badlogic.gdx.graphics.g3d.ModelBatch
import com.badlogic.gdx.graphics.g3d.ModelInstance
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController
import com.badlogic.gdx.math.Vector3
import jake2.qcommon.Com
import jake2.qcommon.Defines
Expand All @@ -23,6 +22,7 @@ import jake2.qcommon.usercmd_t
import jake2.qcommon.util.Math3D
import ktx.app.KtxScreen
import org.demoth.cake.*
import org.demoth.cake.clientcommon.FlyingCameraController
import org.demoth.cake.modelviewer.BspLoader
import org.demoth.cake.modelviewer.Md2ModelLoader
import org.demoth.cake.modelviewer.createGrid
Expand All @@ -48,26 +48,7 @@ class Game3dScreen : KtxScreen, InputProcessor, ServerMessageProcessor {

var deltaTime: Float = 0f

// todo: extract into a separate class, will be useful for the model viewer
private val cameraInputController = object : CameraInputController(camera) {
private val tmpV1 = Vector3()
init {
translateUnits = 500f
}

override fun process(deltaX: Float, deltaY: Float, button: Int): Boolean {
if (button == rotateButton) {
// PITCH: rotate around local "right" axis
tmpV1.set(camera.direction).crs(camera.up).nor()
camera.rotateAround(target, tmpV1, deltaY * rotateAngle)

// YAW: rotate around global Z (which we've set as up)
camera.rotateAround(target, Vector3.Z, -deltaX * rotateAngle)
if (autoUpdate) camera.update()
} else super.process(deltaX, deltaY, button)
return true
}
}
private val cameraInputController = FlyingCameraController(camera)

private val gameConfig = GameConfiguration()

Expand Down Expand Up @@ -112,8 +93,6 @@ class Game3dScreen : KtxScreen, InputProcessor, ServerMessageProcessor {
if (!precached)
return



modelBatch.begin(camera)
models.forEach {

Expand Down
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()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.badlogic.gdx.utils.UBJsonReader
import jake2.qcommon.filesystem.PCX
import jake2.qcommon.filesystem.WAL
import ktx.graphics.use
import org.demoth.cake.clientcommon.FlyingCameraController
import java.io.File
import kotlin.system.measureTimeMillis

Expand Down Expand Up @@ -82,41 +83,12 @@ class CakeModelViewer(val args: Array<String>) : ApplicationAdapter() {

modelBatch = ModelBatch()
camera = PerspectiveCamera(90f, Gdx.graphics.width.toFloat(), Gdx.graphics.height.toFloat())
camera.position.set(0f, 64f, 0f);
camera.position.set(0f, 0f, 0f);
camera.lookAt(64f, 32f, 64f);
camera.near = 1f
camera.far = 4096f

cameraInputController = object : CameraInputController(camera) {
val movementDirection = Vector3()

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)

}
}
}.also {
it.scrollFactor = -1.5f
it.translateUnits = 500f
}
cameraInputController = FlyingCameraController(camera)
Gdx.input.inputProcessor = cameraInputController
modelBatch = ModelBatch()

Expand Down

0 comments on commit 525f620

Please sign in to comment.