Probleme mit der Camera und Voxel Terrain behoben

This commit is contained in:
2026-08-21 15:56:41 +02:00
parent 08e1df06d4
commit 528b2ea576
2 changed files with 33 additions and 0 deletions

View File

@@ -26,12 +26,15 @@ public class ThirdPersonCamera {
private static final float MIN_VERTICAL_ANGLE = -0.12f; // Kamera kann bis Kniehöhe sinken
private static final float MAX_VERTICAL_ANGLE = FastMath.HALF_PI - 0.1f;
private static final float TARGET_HEIGHT = 1.0f;
private static final float CAM_CLEARANCE = 0.3f; // Rückzugsabstand bei Terrain-Kollision
private final Camera cam;
private final InputManager inputManager;
private final KeyBindings keyBindings;
private Spatial target;
/** Liefert den Trefferanteil (0..1) eines Strahls von from→to; 1 = kein Treffer. */
private java.util.function.BiFunction<com.jme3.math.Vector3f, com.jme3.math.Vector3f, Float> raycastProvider;
private float yaw = 0f;
private float pitch = 0.4f;
@@ -59,6 +62,11 @@ public class ThirdPersonCamera {
public void setPaused(boolean paused) { this.paused = paused; }
/** Raycast-Anbieter: gibt Trefferanteil (0..1) zurück; 1 = freie Sichtlinie. */
public void setRaycastProvider(java.util.function.BiFunction<com.jme3.math.Vector3f, com.jme3.math.Vector3f, Float> provider) {
this.raycastProvider = provider;
}
// -----------------------------------------------------------------------
private void registerMappings() {
@@ -111,6 +119,18 @@ public class ThirdPersonCamera {
float z = distance * FastMath.cos(pitch) * FastMath.cos(yaw);
Vector3f camPos = pivot.add(x, y, z);
if (raycastProvider != null) {
float fraction = raycastProvider.apply(pivot, camPos);
if (fraction < 1f) {
Vector3f dir = camPos.subtract(pivot);
float rayLen = dir.length();
dir.normalizeLocal();
float safeLen = Math.max(MIN_DISTANCE * 0.5f, fraction * rayLen - CAM_CLEARANCE);
camPos = pivot.add(dir.mult(safeLen));
}
}
cam.setLocation(camPos);
cam.lookAt(pivot, Vector3f.UNIT_Y);
}

View File

@@ -318,6 +318,19 @@ public class WorldScene extends BaseAppState {
thirdPersonCam = new ThirdPersonCamera(app.getCamera(), app.getInputManager(), keyBindings);
thirdPersonCam.setTarget(character);
thirdPersonCam.setRaycastProvider((from, to) -> {
com.jme3.bullet.PhysicsSpace ps =
bulletAppState != null ? bulletAppState.getPhysicsSpace() : null;
if (ps == null) return 1f;
java.util.List<com.jme3.bullet.collision.PhysicsRayTestResult> hits = ps.rayTest(from, to);
float best = 1f;
for (com.jme3.bullet.collision.PhysicsRayTestResult hit : hits) {
Object uo = hit.getCollisionObject().getUserObject();
if (uo == physicsChar || uo == character) continue;
if (hit.getHitFraction() < best) best = hit.getHitFraction();
}
return best;
});
MainCharacter mc = findMainCharacter();
if (mc != null) {