From bdad836e03a652ea28f5c2178f4bc34da13937a9 Mon Sep 17 00:00:00 2001 From: Lightling Date: Sun, 13 Feb 2022 14:55:52 -0500 Subject: [PATCH] implement camera rotation, fix Rotate setting position instead of eulerAngles --- Camera.cpp | 26 ++++++++++++++++++++++++++ Camera.h | 1 + Game.cpp | 9 +++++---- Transform.cpp | 2 +- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Camera.cpp b/Camera.cpp index 11bfa31..8fcb6c8 100644 --- a/Camera.cpp +++ b/Camera.cpp @@ -23,6 +23,7 @@ Camera::~Camera() void Camera::Update(float _dt) { ReadInput(_dt); + ClampRotation(); UpdateViewMatrix(); } @@ -101,4 +102,29 @@ void Camera::ReadInput(float _dt) transform.TranslateRelative(moveLat * _dt * modify, 0, moveLong * _dt * modify); transform.TranslateAbsolute(0, moveVert * _dt * modify, 0); + + if (input.MouseLeftDown()) + { + float cursorX = (float)input.GetMouseXDelta(); + float cursorY = (float)input.GetMouseYDelta(); + static const float mouseSpeed = 0.1f; + + transform.Rotate(cursorY * _dt * mouseSpeed, cursorX * _dt * mouseSpeed, 0); + } + + float rotateX = 0; + float rotateY = 0; + + if (input.KeyDown('C')) rotateX += 1.0f; + if (input.KeyDown('Z')) rotateX -= 1.0f; + if (input.KeyDown('F')) rotateY += 1.0f; + if (input.KeyDown('R')) rotateY -= 1.0f; + transform.Rotate(rotateY * _dt, rotateX * _dt, 0); +} + +void Camera::ClampRotation() +{ + XMFLOAT3 eulerAngles = transform.GetEulerAngles(); + if (eulerAngles.x > XM_PIDIV4) transform.SetRotation(XM_PIDIV4, eulerAngles.y, eulerAngles.z); + if (eulerAngles.x < -XM_PIDIV4) transform.SetRotation(-XM_PIDIV4, eulerAngles.y, eulerAngles.z); } diff --git a/Camera.h b/Camera.h index 384ad64..d18fe99 100644 --- a/Camera.h +++ b/Camera.h @@ -34,4 +34,5 @@ private: Transform transform; void ReadInput(float _dt); + void ClampRotation(); }; diff --git a/Game.cpp b/Game.cpp index afd3244..ab6e7a8 100644 --- a/Game.cpp +++ b/Game.cpp @@ -6,6 +6,7 @@ // Needed for a helper function to read compiled shader files from the hard drive #pragma comment(lib, "d3dcompiler.lib") #include +#include // For the DirectX Math library using namespace DirectX; @@ -32,7 +33,7 @@ Game::Game(HINSTANCE hInstance) CreateConsoleWindow(500, 120, 32, 120); printf("Console window created successfully. Feel free to printf() here.\n"); #endif - camera = std::make_shared(0.0f, 0.0f, -5.0f, (float)width / height, 90, 0.01f, 1000.0f); + camera = std::make_shared(0.0f, 0.0f, -1.0f, (float)width / height, 70, 0.01f, 1000.0f); } // -------------------------------------------------------- @@ -244,17 +245,17 @@ void Game::Update(float deltaTime, float totalTime) // this range uses shapes[0] for testing if (i < 3) { - entities[i]->GetTransform()->SetPosition(tan((double)totalTime * ((double)i + (double)1)) * 0.1f, 0, 0); + entities[i]->GetTransform()->SetPosition(tan((double)totalTime * ((double)i + (double)1)) * 0.1f, sin(totalTime) * 0.1f, (double)i * 0.1f); } // this range uses shapes[1] for testing else if (i < 6) { - entities[i]->GetTransform()->SetPosition(sin((double)totalTime * ((double)i + (double)1)) * 0.1f, 0, 0); + entities[i]->GetTransform()->SetPosition(sin((double)totalTime * ((double)i + (double)1)) * 0.1f, cos(totalTime) * 0.1f, (double)i * 0.1f); } // this range uses shapes[2] for testing else { - entities[i]->GetTransform()->SetPosition(sin((double)totalTime * ((double)i + (double)1)) * cos(totalTime) * 0.1f, 0, 0); + entities[i]->GetTransform()->SetPosition(sin((double)totalTime * ((double)i + (double)1)) * cos(totalTime) * 0.1f, 0, (double)i * 0.1f); } } } diff --git a/Transform.cpp b/Transform.cpp index 2281e0a..dc606cc 100644 --- a/Transform.cpp +++ b/Transform.cpp @@ -96,7 +96,7 @@ void Transform::TranslateRelative(float _x, float _y, float _z) void Transform::Rotate(float _pitch, float _yaw, float _roll) { - XMVECTOR newRotation = XMLoadFloat3(&position); + XMVECTOR newRotation = XMLoadFloat3(&eulerAngles); XMVECTOR offset = XMVectorSet(_pitch, _yaw, _roll, 0); XMStoreFloat3(&eulerAngles, newRotation + offset); worldMatrixChanged = true;