From 1e1a1c148ed0fa30ccffd81349f26a0615fcee27 Mon Sep 17 00:00:00 2001 From: Eideren Date: Tue, 4 Oct 2022 12:17:40 +0200 Subject: [PATCH] [Template] FPS, remove unused logic (#1512) --- .../FirstPersonShooter.Game/FpsCamera.cs | 70 +++++++------------ 1 file changed, 27 insertions(+), 43 deletions(-) diff --git a/samples/Templates/FirstPersonShooter/FirstPersonShooter/FirstPersonShooter.Game/FpsCamera.cs b/samples/Templates/FirstPersonShooter/FirstPersonShooter/FirstPersonShooter.Game/FpsCamera.cs index d9f355551..900107f98 100644 --- a/samples/Templates/FirstPersonShooter/FirstPersonShooter/FirstPersonShooter.Game/FpsCamera.cs +++ b/samples/Templates/FirstPersonShooter/FirstPersonShooter/FirstPersonShooter.Game/FpsCamera.cs @@ -14,9 +14,6 @@ namespace FirstPersonShooter /// public class FpsCamera : AsyncScript { - private float desiredYaw; - private float desiredPitch; - /// /// Gets the camera component used to visualized the scene. /// @@ -27,14 +24,6 @@ namespace FirstPersonShooter /// public float RotationSpeed { get; set; } = 2.355f; - /// - /// Gets or sets the rate at which orientation is adapted to a target value. - /// - /// - /// The adaptation rate. - /// - public float RotationAdaptationSpeed { get; set; } = 5.0f; - /// /// Gets or sets the Yaw rotation of the camera. /// @@ -45,6 +34,16 @@ namespace FirstPersonShooter /// private float Pitch { get; set; } + /// + /// Check to invert the horizontal camera movement + /// + public bool InvertX { get; set; } = false; + + /// + /// Check to invert the vertical camera movement + /// + public bool InvertY { get; set; } = false; + private readonly EventReceiver cameraDirectionEvent = new EventReceiver(PlayerInput.CameraDirectionEventKey); public override async Task Execute() @@ -61,20 +60,18 @@ namespace FirstPersonShooter public void Reset() { - desiredYaw = - Yaw = - (float) - Math.Asin(2 * Entity.Transform.Rotation.X * Entity.Transform.Rotation.Y + - 2 * Entity.Transform.Rotation.Z * Entity.Transform.Rotation.W); - - desiredPitch = - Pitch = - (float) - Math.Atan2( - 2 * Entity.Transform.Rotation.X * Entity.Transform.Rotation.W - - 2 * Entity.Transform.Rotation.Y * Entity.Transform.Rotation.Z, - 1 - 2 * Entity.Transform.Rotation.X * Entity.Transform.Rotation.X - - 2 * Entity.Transform.Rotation.Z * Entity.Transform.Rotation.Z); + Yaw = + (float) + Math.Asin(2 * Entity.Transform.Rotation.X * Entity.Transform.Rotation.Y + + 2 * Entity.Transform.Rotation.Z * Entity.Transform.Rotation.W); + + Pitch = + (float) + Math.Atan2( + 2 * Entity.Transform.Rotation.X * Entity.Transform.Rotation.W - + 2 * Entity.Transform.Rotation.Y * Entity.Transform.Rotation.Z, + 1 - 2 * Entity.Transform.Rotation.X * Entity.Transform.Rotation.X - + 2 * Entity.Transform.Rotation.Z * Entity.Transform.Rotation.Z); } /// @@ -85,25 +82,12 @@ namespace FirstPersonShooter // Camera movement from player input Vector2 cameraMovement; cameraDirectionEvent.TryReceive(out cameraMovement); - // TODO: InvertX - // TODO: InvertY - // Take shortest path - var deltaPitch = desiredPitch - Pitch; - var deltaYaw = (desiredYaw - Yaw) % MathUtil.TwoPi; - if (deltaYaw < 0) - deltaYaw += MathUtil.TwoPi; - if (deltaYaw > MathUtil.Pi) - deltaYaw -= MathUtil.TwoPi; - desiredYaw = Yaw + deltaYaw; - - // Perform orientation transition - var rotationAdaptation = (float)Game.UpdateTime.Elapsed.TotalSeconds * RotationAdaptationSpeed; - Yaw = Math.Abs(deltaYaw) < rotationAdaptation ? desiredYaw : Yaw + rotationAdaptation * Math.Sign(deltaYaw); - Pitch = Math.Abs(deltaPitch) < rotationAdaptation ? desiredPitch : Pitch + rotationAdaptation * Math.Sign(deltaPitch); - - desiredYaw = Yaw -= 1.333f * cameraMovement.X * RotationSpeed; - desiredPitch = Pitch = MathUtil.Clamp(Pitch + cameraMovement.Y * RotationSpeed, -MathUtil.PiOverTwo, MathUtil.PiOverTwo); + if (InvertY) cameraMovement.Y *= -1; + if (InvertX) cameraMovement.X *= -1; + + Yaw -= cameraMovement.X * RotationSpeed; + Pitch = MathUtil.Clamp(Pitch + cameraMovement.Y * RotationSpeed, -MathUtil.PiOverTwo, MathUtil.PiOverTwo); // Update the camera view matrix UpdateViewMatrix();