From a951b3bcb21a1e8e41c4d272066418a46df5136f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sat, 15 Nov 2025 01:01:07 +0100 Subject: [PATCH] implement view bobbing --- .../engine/FpsCameraController.java | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/wtf/beatrice/retrorender/engine/FpsCameraController.java b/core/src/main/java/wtf/beatrice/retrorender/engine/FpsCameraController.java index 665fc9f..d870600 100644 --- a/core/src/main/java/wtf/beatrice/retrorender/engine/FpsCameraController.java +++ b/core/src/main/java/wtf/beatrice/retrorender/engine/FpsCameraController.java @@ -22,8 +22,14 @@ public class FpsCameraController { private float velocityY = 0f; private boolean grounded = false; + // view bobbing + private float bobTime = 0f; + private float bobOffsetY = 0f; + private final float bobAmount = 0.04f; // amplitude in world units + private final float bobSpeed = 16f; + // --- approximation toggles - private final boolean retroQuantization = true; + private final boolean retroQuantization = false; // how coarse the rotation is (0.25f = round to 0.25°) private final float yawStepDegrees = 0.2f; @@ -104,6 +110,10 @@ public class FpsCameraController { /** Update each frame with delta time */ public void update(float delta, World3D world) { + camera.position.y -= bobOffsetY; + bobOffsetY = 0f; + + if (!mouseCaptured) { return; } @@ -307,6 +317,27 @@ public class FpsCameraController { camera.position.y = Math.round(camera.position.y * q) / q; camera.position.z = Math.round(camera.position.z * q) / q; } + + // ========================================================= + // 4) VIEW BOBBING (purely visual) + // ========================================================= + // horizontal movement magnitude (world units per frame) + float horizLen = (float)Math.sqrt(moveX * moveX + moveZ * moveZ); + + if (grounded && horizLen > 0.0001f) { + // scale bob speed by how fast you're moving + float speedFactor = horizLen / (settings.moveSpeed * delta + 1e-6f); + bobTime += delta * bobSpeed * speedFactor; + + bobOffsetY = (float)Math.sin(bobTime) * bobAmount; + } else { + // not moving or in the air → relax bobbing back to zero + bobTime = 0f; + // optional: smooth return to 0 instead of snapping + bobOffsetY += (0f - bobOffsetY) * 10f * delta; + } + + camera.position.y += bobOffsetY; } private void updateCameraDirection() { // build direction vector from yaw/pitch