From 4eea1f7389b46f9803242a8cb19cca5c1ed7b492 Mon Sep 17 00:00:00 2001 From: MaddoScientisto Date: Mon, 29 Dec 2025 17:27:16 +0100 Subject: [PATCH] Camera Jitter fix --- Scripts/Misc/CameraController3D.cs | 50 ++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/Scripts/Misc/CameraController3D.cs b/Scripts/Misc/CameraController3D.cs index e5b9c42f..4e3c2ba2 100644 --- a/Scripts/Misc/CameraController3D.cs +++ b/Scripts/Misc/CameraController3D.cs @@ -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) + // ); + // } + } \ No newline at end of file