Compare commits
3 Commits
883e2af317
...
ea0589bb09
Author | SHA1 | Date |
---|---|---|
copygirl | ea0589bb09 | 2 months ago |
copygirl | 3462493932 | 2 months ago |
copygirl | 6decf97524 | 2 months ago |
12 changed files with 230 additions and 52 deletions
@ -1,14 +0,0 @@ |
|||||||
public partial class CameraController : Node3D |
|
||||||
{ |
|
||||||
private Vector3 Offset; |
|
||||||
public override void _Ready() |
|
||||||
{ |
|
||||||
Offset = Position; |
|
||||||
} |
|
||||||
|
|
||||||
public override void _Process(double delta) |
|
||||||
{ |
|
||||||
var target = GetParent<Node3D>().GlobalPosition + Offset; |
|
||||||
GlobalPosition = GlobalPosition.Lerp(target, 1 - Pow(0.05f, (float)delta)); |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,69 @@ |
|||||||
|
public partial class CameraController : SpringArm3D |
||||||
|
{ |
||||||
|
[Export] public float MovementSmoothing { get; set; } = 8.0f; |
||||||
|
|
||||||
|
[Export] public Vector2 MouseSensitivity { get; set; } = new(0.2f, 0.2f); // Degrees per pixel. |
||||||
|
|
||||||
|
[Export] public float PitchMinimum { get; set; } = 30; |
||||||
|
[Export] public float PitchMaximum { get; set; } = 65; |
||||||
|
[Export] public float PitchSmoothing { get; set; } = 12.0f; |
||||||
|
|
||||||
|
// FIXME: Fix the "snappyness" when moving slowly. |
||||||
|
// FIXME: Fix spring arm clipping through terrain and similar. |
||||||
|
// TODO: Gradually return to maximum spring length. |
||||||
|
// TODO: Turn player transparent as the camera moves closer. |
||||||
|
|
||||||
|
public static bool IsMouseCaptured |
||||||
|
=> Input.MouseMode == Input.MouseModeEnum.Captured; |
||||||
|
|
||||||
|
Vector3 _offset; |
||||||
|
Node3D _player; |
||||||
|
public override void _Ready() |
||||||
|
{ |
||||||
|
_offset = Position; |
||||||
|
_player = this.GetParentOrThrow<Node3D>(); |
||||||
|
Transform = _player.GlobalTransform.Translated(_offset); |
||||||
|
} |
||||||
|
|
||||||
|
public override void _Input(InputEvent ev) |
||||||
|
{ |
||||||
|
if (IsMouseCaptured && ev.IsActionPressed("ui_cancel")) { |
||||||
|
// When escape is pressed, release the mouse. |
||||||
|
Input.MouseMode = Input.MouseModeEnum.Visible; |
||||||
|
GetViewport().SetInputAsHandled(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public override void _UnhandledInput(InputEvent ev) |
||||||
|
{ |
||||||
|
switch (ev) { |
||||||
|
case InputEventMouseButton { ButtonIndex: MouseButton.Left, Pressed: true } when !IsMouseCaptured: |
||||||
|
// When left mouse button is pressed, capture the mouse. |
||||||
|
Input.MouseMode = Input.MouseModeEnum.Captured; |
||||||
|
GetViewport().SetInputAsHandled(); |
||||||
|
break; |
||||||
|
case InputEventMouseMotion motion when IsMouseCaptured: |
||||||
|
ApplyRotation(-motion.Relative * MouseSensitivity); |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void ApplyRotation(Vector2 delta) |
||||||
|
{ |
||||||
|
var (pitch, yaw, _) = RotationDegrees; |
||||||
|
pitch += delta.Y; |
||||||
|
yaw += delta.X; |
||||||
|
RotationDegrees = new(pitch, yaw, 0); |
||||||
|
} |
||||||
|
|
||||||
|
public override void _Process(double delta) |
||||||
|
{ |
||||||
|
Position = Position.Damp(_offset + _player.GlobalPosition, MovementSmoothing, delta); |
||||||
|
|
||||||
|
var pitch = RotationDegrees.X; |
||||||
|
var (min, max) = (-PitchMaximum, -PitchMinimum); |
||||||
|
if (pitch < min) pitch = pitch.Damp(min, PitchSmoothing, delta); |
||||||
|
if (pitch > max) pitch = pitch.Damp(max, PitchSmoothing, delta); |
||||||
|
RotationDegrees = RotationDegrees with { X = pitch }; |
||||||
|
} |
||||||
|
} |
File diff suppressed because one or more lines are too long
@ -0,0 +1,12 @@ |
|||||||
|
public static class MathExtensions |
||||||
|
{ |
||||||
|
// Framerate independent dampening functions, similar to lerp. |
||||||
|
// https://rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/ |
||||||
|
public static float Damp(this float from, float to, float lambda, double delta) |
||||||
|
=> Lerp(from, to, 1 - Exp(-lambda * (float)delta)); |
||||||
|
public static Vector3 Damp(this Vector3 from, Vector3 to, float lambda, double delta) |
||||||
|
=> from.Lerp(to, 1 - Exp(-lambda * (float)delta)); |
||||||
|
|
||||||
|
public static Vector2I RoundToVector2I(this Vector2 vector) |
||||||
|
=> new(RoundToInt(vector.X), RoundToInt(vector.Y)); |
||||||
|
} |
Loading…
Reference in new issue