fix camera matrix hacks

This commit is contained in:
Asuro
2025-03-29 17:10:26 +01:00
parent dbc65fec9a
commit ef7833caa6
5 changed files with 47 additions and 59 deletions

View File

@@ -82,14 +82,6 @@ void Transform::UpdateMatrix()
bx::mtxInverse(MI.M, M.M); bx::mtxInverse(MI.M, M.M);
} }
void Transform::UpdateMatrixForCam()
{
Mat4 pos;
bx::mtxTranslate(pos.M, Position.x, Position.y, Position.z);
bx::mtxMul(M.M, pos.M, Rotation.M);
bx::mtxInverse(MI.M, M.M);
}
namespace Game namespace Game
{ {
SharedData& GetShared() SharedData& GetShared()

View File

@@ -36,6 +36,8 @@ namespace Game
Transform PlayerCamTransform; Transform PlayerCamTransform;
CameraMode CameraM = CameraMode::Freefly; CameraMode CameraM = CameraMode::Freefly;
InputMode InputM = InputMode::Game; InputMode InputM = InputMode::Game;
float MouseSensitivity = 1.0f;
float MovementSpeed = 10.0f;
}; };
struct InstanceDebugData struct InstanceDebugData

View File

@@ -6,6 +6,7 @@
#include "Level.h" #include "Level.h"
#include "Log.h" #include "Log.h"
#include "Puzzle.h" #include "Puzzle.h"
#include "bx/constants.h"
#include "rendering/Rendering.h" #include "rendering/Rendering.h"
#include "SDL3/SDL_mouse.h" #include "SDL3/SDL_mouse.h"
@@ -93,7 +94,6 @@ namespace Game
needReset |= UIQuads.Setup(storagePtr, needReset); needReset |= UIQuads.Setup(storagePtr, needReset);
Tests.IsEnabled = false; Tests.IsEnabled = false;
Cubes.IsEnabled = false;
Puzzle::Setup(); Puzzle::Setup();
@@ -148,20 +148,12 @@ namespace Game
LOG_ERROR("Failed to open puzzle dir at %s:\n%s", puzzleDirPath.getCPtr(), err.getMessage().getCPtr()); LOG_ERROR("Failed to open puzzle dir at %s:\n%s", puzzleDirPath.getCPtr(), err.getMessage().getCPtr());
} }
if (Cubes.Count == 0) if (!IsValid(PlayerOutsideViewCube))
{ {
for (uint32_t yy = 0; yy < 11; ++yy) PlayerOutsideViewCube = Cubes.New();
{ Cubes.Get(PlayerOutsideViewCube).Setup();
for (uint32_t xx = 0; xx < 11; ++xx)
{
Cube& c = Cubes.Get(Cubes.New());
c.TestX = xx;
c.TestY = yy;
c.Setup();
}
}
Cubes.New(); // Floor
} }
if (Tests.Count == 0) if (Tests.Count == 0)
{ {
Tests.Get(Tests.New()).Setup(); Tests.Get(Tests.New()).Setup();
@@ -187,8 +179,8 @@ namespace Game
// Input // Input
float delta = GetInstance().Time.Delta; float delta = GetInstance().Time.Delta;
constexpr float moveSpeed = 10.0f; float moveSpeed = player.MovementSpeed;
constexpr float rotSpeed = 1.6f; float rotSpeed = player.MouseSensitivity;
float forwardInput = (GetKey(ScanCode::W) ? 1.0f : 0.0f) + (GetKey(ScanCode::S) ? -1.0f : 0.0f); 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); float rightInput = (GetKey(ScanCode::D) ? 1.0f : 0.0f) + (GetKey(ScanCode::A) ? -1.0f : 0.0f);
@@ -196,7 +188,7 @@ namespace Game
moveInput = bx::normalize(moveInput); moveInput = bx::normalize(moveInput);
bx::Vec3 inputVec = {moveInput.x * delta * moveSpeed, 0.0f, moveInput.y * delta * moveSpeed}; bx::Vec3 inputVec = {moveInput.x * delta * moveSpeed, 0.0f, moveInput.y * delta * moveSpeed};
Vec2 mouseMovement = GetMouseMovement(); Vec2 mouseMovement = GetMouseMovement();
bx::Vec3 rotInput = {mouseMovement.y * delta * rotSpeed, mouseMovement.x * delta * rotSpeed, 0.0f}; bx::Vec3 rotInput = {mouseMovement.y * delta * -rotSpeed, mouseMovement.x * delta * -rotSpeed, 0.0f};
if (GetKeyPressedNow(ScanCode::F1)) if (GetKeyPressedNow(ScanCode::F1))
{ {
@@ -222,23 +214,21 @@ namespace Game
{ {
player.FreeflyXRot += rotInput.x; player.FreeflyXRot += rotInput.x;
player.FreeflyYRot += rotInput.y; player.FreeflyYRot += rotInput.y;
bx::mtxRotateY(player.FreeflyCamTransform.Rotation.M, player.FreeflyYRot); bx::mtxRotateXYZ(player.FreeflyCamTransform.Rotation.M, player.FreeflyXRot, player.FreeflyYRot, 0.0f);
player.FreeflyCamTransform.RotateLocal({player.FreeflyXRot, 0.0f, 0.0f});
} }
player.FreeflyCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z}); player.FreeflyCamTransform.TranslateLocal({0.0f, 0.0f, inputVec.z});
player.FreeflyCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f}); player.FreeflyCamTransform.TranslateLocal({inputVec.x, 0.0f, 0.0f});
} }
else if (player.CameraM == CameraMode::Walk) else if (player.CameraM == CameraMode::Walk)
{ {
player.PlayerCamTransform.TranslateLocal({0.0f, 0.0f, -inputVec.z}); player.PlayerCamTransform.TranslateLocal({0.0f, 0.0f, inputVec.z});
player.PlayerCamTransform.TranslateLocal({-inputVec.x, 0.0f, 0.0f}); player.PlayerCamTransform.TranslateLocal({inputVec.x, 0.0f, 0.0f});
player.PlayerCamTransform.Position.y = -3.0f; player.PlayerCamTransform.Position.y = 3.0f;
player.WalkXRot += rotInput.x; player.WalkXRot += rotInput.x;
player.WalkYRot += rotInput.y; player.WalkYRot += rotInput.y;
bx::mtxRotateY(player.PlayerCamTransform.Rotation.M, player.WalkYRot); bx::mtxRotateXYZ(player.PlayerCamTransform.Rotation.M, player.WalkXRot, player.WalkYRot, 0.0f);
player.PlayerCamTransform.RotateLocal({player.WalkXRot, 0.0f, 0.0f});
} }
// Cubes // Cubes
@@ -270,15 +260,17 @@ namespace Game
bgfx::getCaps()->homogeneousDepth); bgfx::getCaps()->homogeneousDepth);
auto& player = GetInstance().Player; auto& player = GetInstance().Player;
if (player.CameraM == CameraMode::Freefly) bool isFreefly = player.CameraM == CameraMode::Freefly;
Cubes.Get(PlayerOutsideViewCube).EData.Visible = isFreefly;
if (isFreefly)
{ {
player.FreeflyCamTransform.UpdateMatrixForCam(); player.FreeflyCamTransform.UpdateMatrix();
bgfx::setViewTransform(viewId, player.FreeflyCamTransform.M.M, proj); bgfx::setViewTransform(viewId, player.FreeflyCamTransform.MI.M, proj);
} }
else else
{ {
player.PlayerCamTransform.UpdateMatrixForCam(); player.PlayerCamTransform.UpdateMatrix();
bgfx::setViewTransform(viewId, player.PlayerCamTransform.M.M, proj); bgfx::setViewTransform(viewId, player.PlayerCamTransform.MI.M, proj);
} }
bgfx::touch(viewId); bgfx::touch(viewId);
@@ -291,25 +283,15 @@ namespace Game
void Cube::Setup() void Cube::Setup()
{ {
EData.MaterialHandle = EMaterial::Default; EData.MaterialHandle = EMaterial::UI;
EData.ModelH = GameRendering::Get().GetModelHandleFromPath("models/cube.gltf"); EData.ModelH = GameRendering::Get().GetModelHandleFromPath("models/cube.gltf");
} }
void Cube::Update() void Cube::Update()
{ {
if (TestX >= 0 && TestY >= 0) EData.Transform.Position = GetInstance().Player.PlayerCamTransform.Position;
{ EData.Transform.Rotation = GetInstance().Player.PlayerCamTransform.Rotation;
double globalTime = GetInstance().Time.Now; EData.Transform.Scale = {0.2f, 0.2f, 0.2f};
double time = TestY <= 5 ? globalTime * 1.0f : 0.0f;
float scale = 1.0f + TestX * 0.4f;
EData.Transform.Position = bx::Vec3{TestX * 2.0f, TestY * 2.0f, 0.0f};
EData.Transform.Scale = {scale, scale, scale};
}
else
{
EData.Transform.Position = {0.0f, -1.0f, 0.0f};
EData.Transform.Scale = {100.0f, 1.0f, 100.0f};
}
} }
void TestEntity::Setup() void TestEntity::Setup()
@@ -342,16 +324,17 @@ namespace Game
void WorldPuzzle::Update() void WorldPuzzle::Update()
{ {
Transform& camTransform = GetInstance().Player.PlayerCamTransform;
camTransform.UpdateMatrixForCam();
Vec3 cameraPos = camTransform.GetPosition() * -1;
Level& level = GetInstance().GameLevel; Level& level = GetInstance().GameLevel;
auto& staticCards = Puzzle::GetStaticPuzzleData().Cards; auto& staticCards = Puzzle::GetStaticPuzzleData().Cards;
Transform& camTransform = GetInstance().Player.PlayerCamTransform;
camTransform.UpdateMatrix();
Vec3 cameraPos = camTransform.GetPosition();
Transform boardTransform; Transform boardTransform;
boardTransform.Rotation = camTransform.Rotation.Inverse(); boardTransform.Rotation = camTransform.Rotation;
Vec3 fw = {camTransform.M.M[2], camTransform.M.M[6], camTransform.M.M[10]}; Vec3 fw = {camTransform.M.M[8], camTransform.M.M[9], camTransform.M.M[10]};
Vec3 pos = camTransform.GetPosition() * -1; Vec3 pos = cameraPos;
pos += fw; pos += fw;
boardTransform.SetPosition(pos); boardTransform.SetPosition(pos);

View File

@@ -11,7 +11,11 @@
struct X \ struct X \
{ \ { \
uint16_t Idx = UINT16_MAX; \ uint16_t Idx = UINT16_MAX; \
}; }; \
inline bool IsValid(X h) \
{ \
return h.Idx != UINT16_MAX; \
}
namespace Game namespace Game
{ {
@@ -149,6 +153,7 @@ namespace Game
EntityManager<TestEntity, TestEntityHandle, 32> Tests; EntityManager<TestEntity, TestEntityHandle, 32> Tests;
EntityManager<PuzzleTileEntity, PuzzleTileEntityHandle, 1024> PuzzleTiles; EntityManager<PuzzleTileEntity, PuzzleTileEntityHandle, 1024> PuzzleTiles;
EntityManager<UIQuadEntity, UIQuadEntityHandle, 1024> UIQuads; EntityManager<UIQuadEntity, UIQuadEntityHandle, 1024> UIQuads;
CubeHandle PlayerOutsideViewCube;
public: public:
Generated::StaticPuzzleData PuzzleData; Generated::StaticPuzzleData PuzzleData;

View File

@@ -1,3 +1,4 @@
#include "Global.h"
#include "Instance.h" #include "Instance.h"
#include "Mesh.h" #include "Mesh.h"
#include "Tools.h" #include "Tools.h"
@@ -75,6 +76,7 @@ namespace Tools
auto& shared = Game::GetShared(); auto& shared = Game::GetShared();
auto& debug = Game::GetInstance().DebugData; auto& debug = Game::GetInstance().DebugData;
auto& level = Game::GetInstance().GameLevel; auto& level = Game::GetInstance().GameLevel;
auto& player = Game::GetInstance().Player;
if (rendering.UIVisible == Game::UIVisibilityState::Debug) if (rendering.UIVisible == Game::UIVisibilityState::Debug)
{ {
@@ -148,6 +150,10 @@ namespace Tools
level = {}; level = {};
level.Setup(shared.Game); level.Setup(shared.Game);
} }
ImGui::SliderFloat("Mouse Sensitivity", &player.MouseSensitivity, 0.1f, 5.0f);
ImGui::SliderFloat("Player Speed", &player.MovementSpeed, 1.0f, 30.0f);
ImGui::Checkbox("Show ImGui Demo", &debug.ShowImguiDemo); ImGui::Checkbox("Show ImGui Demo", &debug.ShowImguiDemo);
ImGui::Checkbox("Show Stats", &debug.ShowStats); ImGui::Checkbox("Show Stats", &debug.ShowStats);
if (debug.ShowImguiDemo) ImGui::ShowDemoWindow(&debug.ShowImguiDemo); if (debug.ShowImguiDemo) ImGui::ShowDemoWindow(&debug.ShowImguiDemo);