|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// This code is licensed under the MIT License (MIT). |
| 4 | +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF |
| 5 | +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY |
| 6 | +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR |
| 7 | +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. |
| 8 | +// |
| 9 | +// Developed by Minigraph |
| 10 | +// |
| 11 | +// Author: James Stanard |
| 12 | +// |
| 13 | + |
| 14 | +#include "pch.h" |
| 15 | +#include "CameraController.h" |
| 16 | +#include "Camera.h" |
| 17 | +#include "GameInput.h" |
| 18 | + |
| 19 | +using namespace Math; |
| 20 | +using namespace GameCore; |
| 21 | + |
| 22 | +CameraController::CameraController( Camera& camera, Vector3 worldUp ) : m_TargetCamera( camera ) |
| 23 | +{ |
| 24 | + m_WorldUp = Normalize(worldUp); |
| 25 | + m_WorldNorth = Normalize(Cross(m_WorldUp, Vector3(kXUnitVector))); |
| 26 | + m_WorldEast = Cross(m_WorldNorth, m_WorldUp); |
| 27 | + |
| 28 | + m_HorizontalLookSensitivity = 2.0f; |
| 29 | + m_VerticalLookSensitivity = 2.0f; |
| 30 | + m_MoveSpeed = 1000.0f; |
| 31 | + m_StrafeSpeed = 1000.0f; |
| 32 | + m_MouseSensitivityX = 1.0f; |
| 33 | + m_MouseSensitivityY = 1.0f; |
| 34 | + |
| 35 | + m_CurrentPitch = Sin(Dot(camera.GetForwardVec(), m_WorldUp)); |
| 36 | + |
| 37 | + Vector3 forward = Normalize(Cross(m_WorldUp, camera.GetRightVec())); |
| 38 | + m_CurrentHeading = ATan2(-Dot(forward, m_WorldEast), Dot(forward, m_WorldNorth)); |
| 39 | + |
| 40 | + m_FineMovement = false; |
| 41 | + m_FineRotation = false; |
| 42 | + m_Momentum = true; |
| 43 | + |
| 44 | + m_LastYaw = 0.0f; |
| 45 | + m_LastPitch = 0.0f; |
| 46 | + m_LastForward = 0.0f; |
| 47 | + m_LastStrafe = 0.0f; |
| 48 | + m_LastAscent = 0.0f; |
| 49 | +} |
| 50 | + |
| 51 | +namespace Graphics |
| 52 | +{ |
| 53 | + extern EnumVar DebugZoom; |
| 54 | +} |
| 55 | + |
| 56 | +void CameraController::Update( float deltaTime ) |
| 57 | +{ |
| 58 | + (deltaTime); |
| 59 | + |
| 60 | + float timeScale = Graphics::DebugZoom == 0 ? 1.0f : Graphics::DebugZoom == 1 ? 0.5f : 0.25f; |
| 61 | + |
| 62 | + if (GameInput::IsFirstPressed(GameInput::kLThumbClick) || GameInput::IsFirstPressed(GameInput::kKey_lshift)) |
| 63 | + m_FineMovement = !m_FineMovement; |
| 64 | + |
| 65 | + if (GameInput::IsFirstPressed(GameInput::kRThumbClick)) |
| 66 | + m_FineRotation = !m_FineRotation; |
| 67 | + |
| 68 | + float speedScale = (m_FineMovement ? 0.1f : 1.0f) * timeScale; |
| 69 | + float panScale = (m_FineRotation ? 0.5f : 1.0f) * timeScale; |
| 70 | + |
| 71 | + float yaw = GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogRightStickX ) * m_HorizontalLookSensitivity * panScale; |
| 72 | + float pitch = GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogRightStickY ) * m_VerticalLookSensitivity * panScale; |
| 73 | + float forward = m_MoveSpeed * speedScale * ( |
| 74 | + GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogLeftStickY ) + |
| 75 | + (GameInput::IsPressed( GameInput::kKey_w ) ? deltaTime : 0.0f) + |
| 76 | + (GameInput::IsPressed( GameInput::kKey_s ) ? -deltaTime : 0.0f) |
| 77 | + ); |
| 78 | + float strafe = m_StrafeSpeed * speedScale * ( |
| 79 | + GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogLeftStickX ) + |
| 80 | + (GameInput::IsPressed( GameInput::kKey_d ) ? deltaTime : 0.0f) + |
| 81 | + (GameInput::IsPressed( GameInput::kKey_a ) ? -deltaTime : 0.0f) |
| 82 | + ); |
| 83 | + float ascent = m_StrafeSpeed * speedScale * ( |
| 84 | + GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogRightTrigger ) - |
| 85 | + GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogLeftTrigger ) + |
| 86 | + (GameInput::IsPressed( GameInput::kKey_e ) ? deltaTime : 0.0f) + |
| 87 | + (GameInput::IsPressed( GameInput::kKey_q ) ? -deltaTime : 0.0f) |
| 88 | + ); |
| 89 | + |
| 90 | + if (m_Momentum) |
| 91 | + { |
| 92 | + ApplyMomentum(m_LastYaw, yaw, deltaTime); |
| 93 | + ApplyMomentum(m_LastPitch, pitch, deltaTime); |
| 94 | + ApplyMomentum(m_LastForward, forward, deltaTime); |
| 95 | + ApplyMomentum(m_LastStrafe, strafe, deltaTime); |
| 96 | + ApplyMomentum(m_LastAscent, ascent, deltaTime); |
| 97 | + } |
| 98 | + |
| 99 | + // don't apply momentum to mouse inputs |
| 100 | + yaw += GameInput::GetAnalogInput(GameInput::kAnalogMouseX) * m_MouseSensitivityX; |
| 101 | + pitch += GameInput::GetAnalogInput(GameInput::kAnalogMouseY) * m_MouseSensitivityY; |
| 102 | + |
| 103 | + m_CurrentPitch += pitch; |
| 104 | + m_CurrentPitch = XMMin( XM_PIDIV2, m_CurrentPitch); |
| 105 | + m_CurrentPitch = XMMax(-XM_PIDIV2, m_CurrentPitch); |
| 106 | + |
| 107 | + m_CurrentHeading -= yaw; |
| 108 | + if (m_CurrentHeading > XM_PI) |
| 109 | + m_CurrentHeading -= XM_2PI; |
| 110 | + else if (m_CurrentHeading <= -XM_PI) |
| 111 | + m_CurrentHeading += XM_2PI; |
| 112 | + |
| 113 | + Matrix3 orientation = Matrix3(m_WorldEast, m_WorldUp, -m_WorldNorth) * Matrix3::MakeYRotation( m_CurrentHeading ) * Matrix3::MakeXRotation( m_CurrentPitch ); |
| 114 | + Vector3 position = orientation * Vector3( strafe, ascent, -forward ) + m_TargetCamera.GetPosition(); |
| 115 | + m_TargetCamera.SetTransform( AffineTransform( orientation, position ) ); |
| 116 | + m_TargetCamera.Update(); |
| 117 | +} |
| 118 | + |
| 119 | +void CameraController::ApplyMomentum( float& oldValue, float& newValue, float deltaTime ) |
| 120 | +{ |
| 121 | + float blendedValue; |
| 122 | + if (Abs(newValue) > Abs(oldValue)) |
| 123 | + blendedValue = Lerp(newValue, oldValue, Pow(0.6f, deltaTime * 60.0f)); |
| 124 | + else |
| 125 | + blendedValue = Lerp(newValue, oldValue, Pow(0.8f, deltaTime * 60.0f)); |
| 126 | + oldValue = blendedValue; |
| 127 | + newValue = blendedValue; |
| 128 | +} |
0 commit comments