3d movement

This commit is contained in:
Asuro
2025-02-13 01:58:03 +01:00
parent 137d8fa539
commit 6e82678ade
7 changed files with 429 additions and 468 deletions

View File

@@ -6,6 +6,7 @@
#include "../Mesh.h"
#include "Rendering.h"
#include "bgfx/defines.h"
#include "bx/math.h"
#include "bx/timer.h"
#include <bgfx/bgfx.h>
#include <bx/file.h>
@@ -179,26 +180,24 @@ namespace Game
constexpr float moveSpeed = 10.0f;
constexpr float rotSpeed = 1.0f;
float forwardInput =
(GetKey(ScanCode::SDL_SCANCODE_W) ? 1.0f : 0.0f) + (GetKey(ScanCode::SDL_SCANCODE_S) ? -1.0f : 0.0f);
float rightInput =
(GetKey(ScanCode::SDL_SCANCODE_D) ? 1.0f : 0.0f) + (GetKey(ScanCode::SDL_SCANCODE_A) ? -1.0f : 0.0f);
Vec2 moveInput = Vec2{rightInput, forwardInput};
moveInput.Normalize();
Vec3 inputVec = {moveInput.X * delta * moveSpeed, 0.0f, moveInput.Y * delta * moveSpeed};
float forwardInput = (GetKey(ScanCode::W) ? 1.0f : 0.0f) + (GetKey(ScanCode::S) ? -1.0f : 0.0f);
float rightInput = (GetKey(ScanCode::D) ? 1.0f : 0.0f) + (GetKey(ScanCode::A) ? -1.0f : 0.0f);
bx::Vec3 moveInput = bx::Vec3{rightInput, forwardInput, 0.0f};
moveInput = bx::normalize(moveInput);
bx::Vec3 inputVec = {moveInput.x * delta * moveSpeed, 0.0f, moveInput.y * delta * moveSpeed};
Vec3 camForward = Cam.Transform.Forward();
Vec3 camRight = Cam.Transform.Right();
bx::Vec3 camForward = Cam.Transform.Forward();
bx::Vec3 camRight = Cam.Transform.Right();
Vec3 rotInput = {shared.Window.MouseDeltaY * delta, shared.Window.MouseDeltaX * delta, 0.0f};
Cam.Transform.Rotate({0.0f, rotInput.Y, 0.0f});
// TODO: split transform into rot matrix and translation
bx::Vec3 rotInput = {shared.Window.MouseDeltaY * delta, shared.Window.MouseDeltaX * delta, 0.0f};
Cam.FreelookXRot += rotInput.x;
Cam.FreelookYRot += rotInput.y;
bx::mtxRotateY(Cam.Transform.Rotation.M, Cam.FreelookYRot);
Cam.Transform.RotateLocal({Cam.FreelookXRot, 0.0f, 0.0f});
// 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);
bgfx::dbgTextPrintf(1, 4, 0x0f, "Cam forward: %.2f %.2f %.2f", camForward.x, camForward.y, camForward.z);
}
// Set view and projection matrix for view 0.
@@ -210,7 +209,8 @@ namespace Game
0.1f,
1000.0f,
bgfx::getCaps()->homogeneousDepth);
bgfx::setViewTransform(0, Cam.Transform.M, proj);
Cam.Transform.UpdateMatrix();
bgfx::setViewTransform(0, Cam.Transform.M.M, proj);
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight);
@@ -224,7 +224,7 @@ namespace Game
Cube* c = GetInstance().GameLevel.Cubes.Get(i);
if (c)
{
bgfx::setTransform(c->Transform.M);
bgfx::setTransform(c->Transform.M.M);
Model& currentModel = Models[c->ModelIdx];
Material& currentMaterial = Materials[c->MaterialIdx];