Camera Jitter fix

This commit is contained in:
MaddoScientisto 2025-12-29 17:27:16 +01:00
commit 4eea1f7389

View file

@ -8,6 +8,9 @@ public partial class CameraController3D : Camera3D
[Export] public bool EnableSmoothing = true;
[Export] public bool FollowTargeting = true;
[Export] public bool SnapCamera = true;
// [Export] public float CameraSnapStep = 0.02f; // 0 = disabled
[Export] public Vector3 DefaultCameraRotation = new Vector3(-36f, 45f, 0f);
@ -59,7 +62,7 @@ public partial class CameraController3D : Camera3D
}
}
public override void _PhysicsProcess(double delta)
public override void _Process(double delta)
{
if (_target is null) return;
@ -94,7 +97,20 @@ public partial class CameraController3D : Camera3D
_currentPosition = desiredCameraPos;
}
GlobalPosition = _currentPosition;
// if (CameraSnapStep > 0f)
// {
// _currentPosition = Snap(_currentPosition, CameraSnapStep);
// }
if (SnapCamera)
{
GlobalPosition = SnapToPixelGrid(_currentPosition);
}
else
{
GlobalPosition = _currentPosition;
}
//GlobalPosition = _currentPosition;
// No LookAt or dynamic rotation — angle is fixed
}
@ -136,4 +152,34 @@ public partial class CameraController3D : Camera3D
return Vector3.Zero;
}
private Vector3 SnapToPixelGrid(Vector3 worldPos)
{
var viewport = GetViewport();
float viewportHeight = viewport.GetVisibleRect().Size.Y;
// World units per screen pixel
float unitsPerPixel = (Size * 2f) / viewportHeight;
return new Vector3(
Mathf.Round(worldPos.X / unitsPerPixel) * unitsPerPixel,
Mathf.Round(worldPos.Y / unitsPerPixel) * unitsPerPixel,
Mathf.Round(worldPos.Z / unitsPerPixel) * unitsPerPixel
);
}
// private static float Snap(float value, float step)
// {
// return Mathf.Round(value / step) * step;
// }
//
// private static Vector3 Snap(Vector3 v, float step)
// {
// return new Vector3(
// Snap(v.X, step),
// Snap(v.Y, step),
// Snap(v.Z, step)
// );
// }
}