Implement our own spring-arm-like functionality

copygirl 2 months ago
parent d9b27e274b
commit 038dd75344
  1. 48
      player/CameraController.cs
  2. 20
      player/character.tscn

@ -1,28 +1,29 @@
public partial class CameraController : SpringArm3D public partial class CameraController : Camera3D
{ {
[Export] public float MovementSmoothing { get; set; } = 8.0f; [ExportCategory("Follow")]
[Export] public float FollowDistance { get; set; } = 1.2f;
[Export] public float FollowYOffset { get; set; } = 1.0f;
[Export] public float FollowSmoothing { get; set; } = 12.0f;
[ExportCategory("Rotation")]
[Export] public Vector2 MouseSensitivity { get; set; } = new(0.2f, 0.2f); // Degrees per pixel. [Export] public Vector2 MouseSensitivity { get; set; } = new(0.2f, 0.2f); // Degrees per pixel.
[Export] public float PitchMinimum { get; set; } = 20;
[Export] public float PitchMinimum { get; set; } = 30;
[Export] public float PitchMaximum { get; set; } = 65; [Export] public float PitchMaximum { get; set; } = 65;
[Export] public float PitchSmoothing { get; set; } = 12.0f; [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: Gradually return to maximum spring length.
// TODO: Turn player transparent as the camera moves closer. // TODO: Turn player transparent as the camera moves closer.
public static bool IsMouseCaptured public static bool IsMouseCaptured
=> Input.MouseMode == Input.MouseModeEnum.Captured; => Input.MouseMode == Input.MouseModeEnum.Captured;
Vector3 _offset;
Node3D _player; Node3D _player;
Vector3 _smoothPlayerPos;
public override void _Ready() public override void _Ready()
{ {
_offset = Position;
_player = this.GetParentOrThrow<Node3D>(); _player = this.GetParentOrThrow<Node3D>();
Transform = _player.GlobalTransform.Translated(_offset); _smoothPlayerPos = _player.GlobalPosition;
Transform = _player.GlobalTransform.Translated(new(0.0f, FollowYOffset, 0.0f));
} }
public override void _Input(InputEvent ev) public override void _Input(InputEvent ev)
@ -56,10 +57,35 @@ public partial class CameraController : SpringArm3D
RotationDegrees = new(pitch, yaw, 0); RotationDegrees = new(pitch, yaw, 0);
} }
public override void _Process(double delta) public override void _PhysicsProcess(double delta)
{ {
Position = Position.Damp(_offset + _player.GlobalPosition, MovementSmoothing, delta); _smoothPlayerPos = _smoothPlayerPos.Damp(_player.GlobalPosition, FollowSmoothing, delta);
var target = _smoothPlayerPos
+ Basis.Z * FollowDistance
+ Vector3.Up * FollowYOffset;
Position = OffsetRayIntersection(_smoothPlayerPos, target, 0.2f);
}
Vector3 OffsetRayIntersection(Vector3 from, Vector3 to, float margin)
{
const PhysicsLayer CollisionMask = PhysicsLayer.Terrain
| PhysicsLayer.Objects;
var query = PhysicsRayQueryParameters3D.Create(from, to, (uint)CollisionMask);
var result = GetWorld3D().DirectSpaceState.IntersectRay(query);
if (result.Count > 0) {
var hit = (Vector3)result["position"];
var safeDistance = Max(0, from.DistanceTo(hit) - margin);
return from + (to - from).Normalized() * safeDistance;
} else {
// No intersection occured,
return to;
}
}
public override void _Process(double delta)
{
var pitch = RotationDegrees.X; var pitch = RotationDegrees.X;
var (min, max) = (-PitchMaximum, -PitchMinimum); var (min, max) = (-PitchMaximum, -PitchMinimum);
if (pitch < min) pitch = pitch.Damp(min, PitchSmoothing, delta); if (pitch < min) pitch = pitch.Damp(min, PitchSmoothing, delta);

@ -20,26 +20,18 @@ collision_mask = 3
floor_max_angle = 0.698132 floor_max_angle = 0.698132
[node name="MeshInstance3D" type="MeshInstance3D" parent="."] [node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.15, 0)
mesh = SubResource("SphereMesh_7ljg8") mesh = SubResource("SphereMesh_7ljg8")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.2, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.05, 0)
shape = SubResource("SphereShape3D_6qbb2") shape = SubResource("SphereShape3D_6qbb2")
[node name="MovementController" type="Node" parent="."] [node name="Camera" type="Camera3D" parent="."]
script = ExtResource("1_akh08")
[node name="CameraArm" type="SpringArm3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.766044, 0.642788, 0, -0.642788, 0.766044, 0, 0.1, 0)
top_level = true top_level = true
collision_mask = 3
spring_length = 2.0
margin = 0.05
script = ExtResource("2_2din1")
[node name="Camera" type="Camera3D" parent="CameraArm"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 2)
current = true current = true
size = 4.5 size = 4.5
far = 500.0 far = 500.0
script = ExtResource("2_2din1")
[node name="MovementController" type="Node" parent="."]
script = ExtResource("1_akh08")

Loading…
Cancel
Save