implement camera rotation, fix Rotate setting position instead of eulerAngles

This commit is contained in:
lightling 2022-02-13 14:55:52 -05:00
parent acb842889c
commit bdad836e03
Signed by: lightling
GPG key ID: 016F11E0AA296B67
4 changed files with 33 additions and 5 deletions

View file

@ -23,6 +23,7 @@ Camera::~Camera()
void Camera::Update(float _dt) void Camera::Update(float _dt)
{ {
ReadInput(_dt); ReadInput(_dt);
ClampRotation();
UpdateViewMatrix(); UpdateViewMatrix();
} }
@ -101,4 +102,29 @@ void Camera::ReadInput(float _dt)
transform.TranslateRelative(moveLat * _dt * modify, 0, moveLong * _dt * modify); transform.TranslateRelative(moveLat * _dt * modify, 0, moveLong * _dt * modify);
transform.TranslateAbsolute(0, moveVert * _dt * modify, 0); 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);
} }

View file

@ -34,4 +34,5 @@ private:
Transform transform; Transform transform;
void ReadInput(float _dt); void ReadInput(float _dt);
void ClampRotation();
}; };

View file

@ -6,6 +6,7 @@
// Needed for a helper function to read compiled shader files from the hard drive // Needed for a helper function to read compiled shader files from the hard drive
#pragma comment(lib, "d3dcompiler.lib") #pragma comment(lib, "d3dcompiler.lib")
#include <d3dcompiler.h> #include <d3dcompiler.h>
#include <iostream>
// For the DirectX Math library // For the DirectX Math library
using namespace DirectX; using namespace DirectX;
@ -32,7 +33,7 @@ Game::Game(HINSTANCE hInstance)
CreateConsoleWindow(500, 120, 32, 120); CreateConsoleWindow(500, 120, 32, 120);
printf("Console window created successfully. Feel free to printf() here.\n"); printf("Console window created successfully. Feel free to printf() here.\n");
#endif #endif
camera = std::make_shared<Camera>(0.0f, 0.0f, -5.0f, (float)width / height, 90, 0.01f, 1000.0f); camera = std::make_shared<Camera>(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 // this range uses shapes[0] for testing
if (i < 3) 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 // this range uses shapes[1] for testing
else if (i < 6) 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 // this range uses shapes[2] for testing
else 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);
} }
} }
} }

View file

@ -96,7 +96,7 @@ void Transform::TranslateRelative(float _x, float _y, float _z)
void Transform::Rotate(float _pitch, float _yaw, float _roll) void Transform::Rotate(float _pitch, float _yaw, float _roll)
{ {
XMVECTOR newRotation = XMLoadFloat3(&position); XMVECTOR newRotation = XMLoadFloat3(&eulerAngles);
XMVECTOR offset = XMVectorSet(_pitch, _yaw, _roll, 0); XMVECTOR offset = XMVectorSet(_pitch, _yaw, _roll, 0);
XMStoreFloat3(&eulerAngles, newRotation + offset); XMStoreFloat3(&eulerAngles, newRotation + offset);
worldMatrixChanged = true; worldMatrixChanged = true;