implement view bobbing

This commit is contained in:
2025-11-15 01:01:07 +01:00
parent eb1542e161
commit a951b3bcb2

View File

@@ -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