Skip to content

Commit

Permalink
Snap the camera to the nearest 90 degrees if we're close enough
Browse files Browse the repository at this point in the history
While playing the game I was bothered by the fact that the camera can get misaligned to where it's just a few degrees shy of straight forward. This makes it annoying to line up some jumps. So now, when you get within 18 degrees of one of the four 90 degree directions, you'll snap straight ahead. I played around with the numbers, and this amount of sensitivity feels best. If you think the number should be lower or higher, let me know. Here is a demonstration of the change: https://www.youtube.com/watch?v=sUO0gDmANLQ
  • Loading branch information
ZakFahey committed Feb 4, 2024
1 parent 8e7f57a commit f1444ec
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions Source/Actors/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,20 +268,27 @@ public override void Update()
{
// Rotate Camera
{
if (!Controls.BackCamera.Pressed)
var rot = new Vec2(cameraTargetForward.X, cameraTargetForward.Y).Angle();
if (Controls.BackCamera.Pressed)
{
rot = Facing.Angle();
}
else if (Controls.Camera.Value.X != 0)
{
var rot = new Vec2(cameraTargetForward.X, cameraTargetForward.Y).Angle();
rot -= Controls.Camera.Value.X * Time.Delta * 4;

var angle = Calc.AngleToVector(rot);
cameraTargetForward = new(angle, 0);
}
else
{
var rot = Facing.Angle();
var angle = Calc.AngleToVector(rot);
cameraTargetForward = new(angle, 0);
// If the camera isn't moving, snap the angle to the nearest 90 degrees if we're close enough
float? cameraPositionToSnapTo = CameraPositionToSnapTo(rot, MathF.PI / 10);
if (cameraPositionToSnapTo.HasValue)
{
rot = Calc.AngleApproach(rot, cameraPositionToSnapTo.Value, 4);
}
}

var angle = Calc.AngleToVector(rot);
cameraTargetForward = new(angle, 0);
}

// Move Camera in / out
Expand Down Expand Up @@ -597,6 +604,17 @@ public void GetCameraTarget(out Vec3 cameraLookAt, out Vec3 cameraPosition, out
}
}

private float? CameraPositionToSnapTo(float currentAngle, float sensitivity)
{
for (int i = 0; i < 4; i++)
{
float angle = i * MathF.PI / 2;
if (Calc.AbsAngleDiff(currentAngle, angle) < sensitivity)
return angle;
}
return null;
}

#endregion

#region Various Methods
Expand Down

0 comments on commit f1444ec

Please sign in to comment.