From 49cb9e25af39a86a09d26709fed6bafcf4f2dccb Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Tue, 28 Jun 2022 19:41:49 +0300 Subject: [PATCH] viewangles refactor, fix view angles wraparound --- .../EngineSettings/InputSettings.json | 4 +- Source/Game/Player/PlayerInputLocal.cs | 11 ++- Source/Game/Player/PlayerMovement.cs | 77 +++++++++++-------- Source/Game/Utility/AssetManager.cs | 3 + 4 files changed, 58 insertions(+), 37 deletions(-) diff --git a/Content/Settings/EngineSettings/InputSettings.json b/Content/Settings/EngineSettings/InputSettings.json index da90c7d..0c27862 100644 --- a/Content/Settings/EngineSettings/InputSettings.json +++ b/Content/Settings/EngineSettings/InputSettings.json @@ -85,7 +85,7 @@ "PositiveButton": 0, "NegativeButton": 0, "DeadZone": 0.0, - "Sensitivity": 0.02, + "Sensitivity": 0.022, "Gravity": 1.0, "Scale": 1.0, "Snap": false @@ -97,7 +97,7 @@ "PositiveButton": 0, "NegativeButton": 0, "DeadZone": 0.0, - "Sensitivity": 0.02, + "Sensitivity": 0.022, "Gravity": 1.0, "Scale": 1.0, "Snap": false diff --git a/Source/Game/Player/PlayerInputLocal.cs b/Source/Game/Player/PlayerInputLocal.cs index ed1d1f2..0572f77 100644 --- a/Source/Game/Player/PlayerInputLocal.cs +++ b/Source/Game/Player/PlayerInputLocal.cs @@ -33,8 +33,15 @@ namespace Game { // Collect all input here // All axis values here should be accumulated - currentState.input.viewDeltaX += InputManager.GetAxisRaw("Mouse X"); - currentState.input.viewDeltaY += InputManager.GetAxisRaw("Mouse Y"); + float sensitivity = 1.0f / 8.0f; + sensitivity = 1.0f; + + //var asf = InputManager.GetAxisRaw("Mouse X"); + //if (asf != 0.0f) + // Console.Print(InputManager.GetAxisRaw("Mouse X").ToString("G9", System.Globalization.CultureInfo.InvariantCulture)); + + currentState.input.viewDeltaX += InputManager.GetAxisRaw("Mouse X") * sensitivity; + currentState.input.viewDeltaY += InputManager.GetAxisRaw("Mouse Y") * sensitivity; currentState.input.viewDeltaX += InputManager.GetAxisRaw("LookRight") * Time.DeltaTime * 100; currentState.input.viewDeltaY += -InputManager.GetAxisRaw("LookUp") * Time.DeltaTime * 100; diff --git a/Source/Game/Player/PlayerMovement.cs b/Source/Game/Player/PlayerMovement.cs index e963bab..b548971 100644 --- a/Source/Game/Player/PlayerMovement.cs +++ b/Source/Game/Player/PlayerMovement.cs @@ -102,13 +102,8 @@ namespace Game private Actor rootActor; private float startupTime; - private float viewPitch; - - private float viewPitchLastFrame; - private float viewRoll; - private float viewRollLastFrame; - private float viewYaw; - private float viewYawLastFrame; + private Float3 viewAngles; + private Float3 viewAnglesLastFrame; [Limit(0, 9000)] [Tooltip("Base Movement speed")] @@ -186,6 +181,14 @@ namespace Game public void SetInput(string demoFile) { input = new PlayerInputDemo(demoFile); + + /*Actor.Position = input.GetCurrentActorState().position; + currentVelocity = input.GetCurrentActorState().velocity; + SetCameraEulerAngles(input.GetCurrentActorState().viewAngles);*/ + Actor.Position = input.GetCurrentInputState().verificationPosition; + //rootActor.Orientation = input.GetCurrentInputState().verificationOrientation; + currentVelocity = input.GetCurrentInputState().verificationVelocity; + SetCameraEulerAngles(input.GetCurrentInputState().verificationViewAngles); } public override void OnDisable() @@ -232,14 +235,10 @@ namespace Game public void ResetRotation(Float3 eulerAngles) { - viewPitch = eulerAngles.X; - viewYaw = eulerAngles.Y; - viewRoll = eulerAngles.Z; - viewPitchLastFrame = viewPitch; - viewYawLastFrame = viewYaw; - viewRollLastFrame = viewRoll; + viewAngles = eulerAngles; + viewAnglesLastFrame = eulerAngles; - SetCameraEulerAngles(new Float3(viewYaw, viewPitch, viewRoll)); + SetCameraEulerAngles(new Float3(eulerAngles.Y, eulerAngles.X, eulerAngles.Z)); } public override void OnUpdate() @@ -263,9 +262,7 @@ namespace Game PlayerInputState inputState = input.GetCurrentInputState(); - viewYaw = viewYawLastFrame; - viewPitch = viewPitchLastFrame; - viewRoll = viewRollLastFrame; + viewAngles = viewAnglesLastFrame; ApplyInputToCamera(inputState); /*input.RecordCurrentActorState(new PlayerActorState() @@ -294,18 +291,18 @@ namespace Game if (input is PlayerInputDemo) { - ApplyInputToCamera(inputState); + ApplyInputToCamera(inputState, false); // Verify view angles first if (demoDeltasVerify) { - Float3 viewAngles = new Float3(viewYaw, viewPitch, viewRoll); - float viewAnglesDelta = (viewAngles - inputState.verificationViewAngles).Length; + Float3 verifAngles = new Float3(inputState.verificationViewAngles.Y, inputState.verificationViewAngles.X, inputState.verificationViewAngles.Z); + float viewAnglesDelta = (viewAngles - verifAngles).Length; if (viewAnglesDelta > 0.00001) { - Console.PrintError("Demo verification failed, view angles delta: " + viewAnglesDelta); + Console.PrintError($"Demo verification failed, view angles delta: {viewAnglesDelta}, viewAngles:{viewAngles}, verif:{verifAngles}"); if (demoDeltasCorrect) - SetCameraEulerAngles(inputState.verificationViewAngles); + SetCameraEulerAngles(verifAngles, false); } } } @@ -317,11 +314,11 @@ namespace Game // verify float positionDelta = (Actor.Position - inputState.verificationPosition).Length; if (positionDelta > 0.00001) - Console.Print("Demo verification failed, position delta: " + positionDelta); + Console.PrintError("Demo verification failed, position delta: " + positionDelta); float velocityDelta = (currentVelocity - inputState.verificationVelocity).Length; if (velocityDelta > 0.00001) - Console.Print("Demo verification failed, velocity delta: " + velocityDelta); + Console.PrintError("Demo verification failed, velocity delta: " + velocityDelta); float orientationDelta = (rootActor.Orientation - inputState.verificationOrientation).Length; if (orientationDelta > 0.00001) @@ -347,7 +344,7 @@ namespace Game position = Actor.Position, velocity = currentVelocity, orientation = rootActor.Orientation, - viewAngles = new Float3(viewYaw, viewPitch, viewRoll) + viewAngles = new Float3(viewAngles.Y, viewAngles.X, viewAngles.Z) }); input.OnEndFrame(); @@ -355,12 +352,10 @@ namespace Game lastInputFrame = currentInputFrame; currentInputFrame++; - viewPitchLastFrame = viewPitch; - viewYawLastFrame = viewYaw; - viewRollLastFrame = viewRoll; + viewAnglesLastFrame = viewAngles; } - private void ApplyInputToCamera(PlayerInputState inputState) + private void ApplyInputToCamera(PlayerInputState inputState, bool wrapAround = true) { // Update camera viewf float xAxis = inputState.viewDeltaX; @@ -368,17 +363,33 @@ namespace Game if (xAxis == 0.0f && yAxis == 0.0f) return; + float viewPitch = viewAngles.X; + float viewYaw = viewAngles.Y; viewPitch = Mathf.Clamp(viewPitch + yAxis, -90.0f, 90.0f); viewYaw += xAxis; - SetCameraEulerAngles(new Float3(viewYaw, viewPitch, viewRoll)); + SetCameraEulerAngles(new Float3(viewYaw, viewPitch, viewAngles.Z), wrapAround); } - private void SetCameraEulerAngles(Float3 viewAngles) + private void SetCameraEulerAngles(Float3 angles, bool wrapAround = true) { + // Very slight drift + if (wrapAround) + { + angles.X = Mathf.Mod(angles.X, 360.0f); + angles.Y = Mathf.Mod(angles.Y, 360.0f); + angles.Z = Mathf.Mod(angles.Z, 360.0f); + } + // Root orientation must be set first - rootActor.Orientation = Quaternion.Euler(0, viewAngles.X, 0); - cameraHolder.Orientation = Quaternion.Euler(viewAngles.Y, viewAngles.X, viewAngles.Z); + var or1 = Quaternion.Euler(0, angles.X, 0); + var or2 = Quaternion.Euler(angles.Y, angles.X, angles.Z); + rootActor.Orientation = or1; + cameraHolder.Orientation = or2; + + Console.Print(angles.X.ToString()); + + viewAngles = new Float3(angles.Y, angles.X, angles.Z); } private static bool SweepPlayerCollider(Actor actor, Float3 start, Vector3 end, out RayCastHit[] hits) diff --git a/Source/Game/Utility/AssetManager.cs b/Source/Game/Utility/AssetManager.cs index 33229d2..d924979 100644 --- a/Source/Game/Utility/AssetManager.cs +++ b/Source/Game/Utility/AssetManager.cs @@ -8,6 +8,9 @@ namespace Game public static string ContentPath { get; private set; } = Path.Combine(Directory.GetCurrentDirectory(), "Content"); + public static string DemoPath { get; private set; } = + Path.Combine(Directory.GetCurrentDirectory(), "Demos"); + public static GameplayGlobals Globals { get; private set; } public static Config Config { get; private set; }