more camera movement

This commit is contained in:
Asuro
2025-02-12 02:01:39 +01:00
parent 03aecb6d44
commit 137d8fa539
6 changed files with 56 additions and 6 deletions

View File

@@ -7,6 +7,8 @@ struct SharedWindowData
int32_t WindowWidth = 1920; int32_t WindowWidth = 1920;
int32_t WindowHeight = 1080; int32_t WindowHeight = 1080;
bool HeldScanCodes[512]{0}; bool HeldScanCodes[512]{0};
float MouseDeltaX = 0.0f;
float MouseDeltaY = 0.0f;
}; };
struct FileChangeNotification struct FileChangeNotification

View File

@@ -57,6 +57,9 @@ void EngineWindow::Update(SharedWindowData& shared)
} }
case SDL_EVENT_MOUSE_MOTION: case SDL_EVENT_MOUSE_MOTION:
{ {
shared.MouseDeltaX += evt.motion.xrel;
shared.MouseDeltaY += evt.motion.yrel;
break;
} }
default: default:
break; break;

View File

@@ -9,6 +9,21 @@ namespace
Game::GameInstance* GameInst = nullptr; Game::GameInstance* GameInst = nullptr;
} // namespace } // namespace
void Vec2::Normalize()
{
float len = bx::sqrt(X * X + Y * Y);
X /= len;
Y /= len;
}
void Vec3::Normalize()
{
float len = bx::sqrt(X * X + Y * Y + Z * Z);
X /= len;
Y /= len;
Z /= len;
}
Quat Quat::FromEuler(float x, float y, float z) Quat Quat::FromEuler(float x, float y, float z)
{ {
x *= bx::kPi / 180.0f; x *= bx::kPi / 180.0f;
@@ -47,13 +62,23 @@ void Mat4::CreateTransform(float* out, Vec3 pos, Quat rot, Vec3 scale)
bx::mtxMul(out, buf, tMat); bx::mtxMul(out, buf, tMat);
} }
void Mat4::Translate(Vec3 offset) void Mat4::TranslateLocal(Vec3 offset)
{ {
M[12] += offset.X; M[12] += offset.X;
M[13] += offset.Y; M[13] += offset.Y;
M[14] += offset.Z; M[14] += offset.Z;
} }
void Mat4::Rotate(Vec3 rotation)
{
float rot[16]{0};
bx::mtxRotateXYZ(rot, rotation.X, rotation.Y, rotation.Z);
float temp[16]{0};
bx::mtxMul(temp, M, rot);
for (int32_t i = 0; i < 16; ++i)
M[i] = temp[i];
}
Vec3 Mat4::Right() Vec3 Mat4::Right()
{ {
return {M[0], M[1], M[2]}; return {M[0], M[1], M[2]};

View File

@@ -20,6 +20,8 @@ struct Vec2
{ {
float X = 0.0f; float X = 0.0f;
float Y = 0.0f; float Y = 0.0f;
void Normalize();
}; };
struct Vec3 struct Vec3
@@ -32,6 +34,8 @@ struct Vec3
{ {
return {X, Y, Z}; return {X, Y, Z};
}; };
void Normalize();
}; };
struct Quat struct Quat
@@ -67,7 +71,8 @@ struct Mat4
}; };
static void CreateTransform(float* out, Vec3 pos, Quat rot = {}, Vec3 scale = {1.0f, 1.0f, 1.0f}); static void CreateTransform(float* out, Vec3 pos, Quat rot = {}, Vec3 scale = {1.0f, 1.0f, 1.0f});
void Translate(Vec3 offset); void TranslateLocal(Vec3 offset);
void Rotate(Vec3 rotation);
Vec3 Right(); Vec3 Right();
Vec3 Up(); Vec3 Up();
Vec3 Forward(); Vec3 Forward();

View File

@@ -57,6 +57,8 @@ namespace Game
GetInstance().Delta = newNow - GetInstance().Now; GetInstance().Delta = newNow - GetInstance().Now;
GetInstance().Now = newNow; GetInstance().Now = newNow;
SetupInstance.Rendering.Update(); SetupInstance.Rendering.Update();
GetShared().Window.MouseDeltaX = 0.0f;
GetShared().Window.MouseDeltaY = 0.0f;
} }
void Shutdown() void Shutdown()

View File

@@ -174,18 +174,31 @@ namespace Game
{ {
GetInstance().GameLevel.Update(); GetInstance().GameLevel.Update();
float delta = GetInstance().Delta;
constexpr float moveSpeed = 10.0f;
constexpr float rotSpeed = 1.0f;
float forwardInput = float forwardInput =
(GetKey(ScanCode::SDL_SCANCODE_W) ? 1.0f : 0.0f) + (GetKey(ScanCode::SDL_SCANCODE_S) ? -1.0f : 0.0f); (GetKey(ScanCode::SDL_SCANCODE_W) ? 1.0f : 0.0f) + (GetKey(ScanCode::SDL_SCANCODE_S) ? -1.0f : 0.0f);
float rightInput = float rightInput =
(GetKey(ScanCode::SDL_SCANCODE_D) ? 1.0f : 0.0f) + (GetKey(ScanCode::SDL_SCANCODE_A) ? -1.0f : 0.0f); (GetKey(ScanCode::SDL_SCANCODE_D) ? 1.0f : 0.0f) + (GetKey(ScanCode::SDL_SCANCODE_A) ? -1.0f : 0.0f);
Vec3 inputVec = {rightInput, 0.0f, forwardInput}; Vec2 moveInput = Vec2{rightInput, forwardInput};
moveInput.Normalize();
Vec3 inputVec = {moveInput.X * delta * moveSpeed, 0.0f, moveInput.Y * delta * moveSpeed};
Vec3 camForward = Cam.Transform.Forward(); Vec3 camForward = Cam.Transform.Forward();
Vec3 camRight = Cam.Transform.Right(); Vec3 camRight = Cam.Transform.Right();
Cam.Transform.Translate( Vec3 rotInput = {shared.Window.MouseDeltaY * delta, shared.Window.MouseDeltaX * delta, 0.0f};
{camForward.X * forwardInput, camForward.Y * forwardInput, camForward.Z * forwardInput}); Cam.Transform.Rotate({0.0f, rotInput.Y, 0.0f});
Cam.Transform.Translate({camRight.X * rightInput, camRight.Y * rightInput, camRight.Z * rightInput}); // TODO: split transform into rot matrix and translation
// Cam.Transform.Translate(
// {camForward.X * forwardInput, camForward.Y * forwardInput, camForward.Z * forwardInput});
Cam.Transform.TranslateLocal({0.0f, 0.0f, -forwardInput});
Cam.Transform.TranslateLocal({-rightInput, 0.0f, 0.0f});
bgfx::dbgTextPrintf(1, 4, 0x0f, "Cam forward: %.2f %.2f %.2f", camForward.X, camForward.Y, camForward.Z);
} }
// Set view and projection matrix for view 0. // Set view and projection matrix for view 0.