From 5f6fb704755323049e27f93abd9e3f0446fa1222 Mon Sep 17 00:00:00 2001 From: Safariminer Date: Sat, 7 Jun 2025 16:15:55 -0400 Subject: [PATCH] Basic collision implementation; TODO: fix collision bug when touching lines outside of current sector(eg. touching an angle). --- ProjectMorault/Morault_Player.cpp | 44 +++++++++++++++++++++++-------- ProjectMorault/Morault_Player.h | 1 + 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/ProjectMorault/Morault_Player.cpp b/ProjectMorault/Morault_Player.cpp index 7648bc3..ee0fb3b 100644 --- a/ProjectMorault/Morault_Player.cpp +++ b/ProjectMorault/Morault_Player.cpp @@ -43,16 +43,19 @@ float gravity = 0; void Morault::Gameplay::PlayerController::UpdatePlayerController(bool focus, Morault::Maps::Map* map, float renderScale) { + + Vector3 futurePos = camera.position; + if (focus) { rotation = Vector3RotateByAxisAngle(rotation, { 0,1,0 }, -GetMouseDelta().x * 0.05 * sensitivityMultiplier); rotation.y = Clamp(rotation.y - GetMouseDelta().y * 0.3 * sensitivityMultiplier, -15, 15); - if (IsKeyDown(KEY_W)) camera.position = Vector3Add(camera.position, Vector3Multiply({rotation.x, 0, rotation.z}, {GetFrameTime(), GetFrameTime(), GetFrameTime()})); - if (IsKeyDown(KEY_S)) camera.position = Vector3Subtract(camera.position, Vector3Multiply({ rotation.x, 0, rotation.z }, { GetFrameTime(), GetFrameTime(), GetFrameTime() })); + if (IsKeyDown(KEY_W)) futurePos = Vector3Add(futurePos, Vector3Multiply({rotation.x, 0, rotation.z}, {GetFrameTime(), GetFrameTime(), GetFrameTime()})); + if (IsKeyDown(KEY_S)) futurePos = Vector3Subtract(futurePos, Vector3Multiply({ rotation.x, 0, rotation.z }, { GetFrameTime(), GetFrameTime(), GetFrameTime() })); - if (IsKeyDown(KEY_A)) camera.position = Vector3Add(camera.position, Vector3Multiply(Vector3RotateByAxisAngle({ rotation.x, 0, rotation.z }, {0,1,0}, DEG2RAD*90), {GetFrameTime(), GetFrameTime(), GetFrameTime()})); - if (IsKeyDown(KEY_D)) camera.position = Vector3Add(camera.position, Vector3Multiply(Vector3RotateByAxisAngle({ rotation.x, 0, rotation.z }, { 0,1,0 }, DEG2RAD * -90), { GetFrameTime(), GetFrameTime(), GetFrameTime() })); + if (IsKeyDown(KEY_A)) futurePos = Vector3Add(futurePos, Vector3Multiply(Vector3RotateByAxisAngle({ rotation.x, 0, rotation.z }, {0,1,0}, DEG2RAD*90), {GetFrameTime(), GetFrameTime(), GetFrameTime()})); + if (IsKeyDown(KEY_D)) futurePos = Vector3Add(futurePos, Vector3Multiply(Vector3RotateByAxisAngle({ rotation.x, 0, rotation.z }, { 0,1,0 }, DEG2RAD * -90), { GetFrameTime(), GetFrameTime(), GetFrameTime() })); if (IsKeyPressed(KEY_SPACE)) { gravity -= 10; } @@ -63,25 +66,44 @@ void Morault::Gameplay::PlayerController::UpdatePlayerController(bool focus, Mor rotation = { 0,0,0 }; } - camera.target = Vector3Add(camera.position, rotation); for (int i = 0; i < map->MapTriangles.size(); i++) { - if (CheckCollisionPointTriangle({ camera.position.x / renderScale, camera.position.z / renderScale }, map->MapTriangles[i].a, map->MapTriangles[i].b, map->MapTriangles[i].c)) { + if (CheckCollisionPointTriangle({ futurePos.x / renderScale, futurePos.z / renderScale }, map->MapTriangles[i].a, map->MapTriangles[i].b, map->MapTriangles[i].c)) { collfloor = map->MapTriangles[i].heightFloor * renderScale + 5; collceiling = map->MapTriangles[i].heightCeiling * renderScale-2; + if (map->MapTriangles[i].wallAB != "pms:none") { + if (CheckCollisionCircleLine({ futurePos.x, futurePos.z }, obesity, Vector2Multiply(map->MapTriangles[i].a, { renderScale,renderScale }), Vector2Multiply(map->MapTriangles[i].b, { renderScale,renderScale }))) { + futurePos = camera.position; + } + } + if (map->MapTriangles[i].wallBC != "pms:none") { + if (CheckCollisionCircleLine({ futurePos.x, futurePos.z }, obesity, Vector2Multiply(map->MapTriangles[i].b, { renderScale,renderScale }), Vector2Multiply(map->MapTriangles[i].c, { renderScale,renderScale }))) { + futurePos = camera.position; + } + } + if (map->MapTriangles[i].wallCA != "pms:none") { + if (CheckCollisionCircleLine({ futurePos.x, futurePos.z }, obesity, Vector2Multiply(map->MapTriangles[i].c, { renderScale,renderScale }), Vector2Multiply(map->MapTriangles[i].a, { renderScale,renderScale }))) { + futurePos = camera.position; + } + } } } - camera.position.y -= gravity*GetFrameTime(); + futurePos.y -= gravity*GetFrameTime(); gravity += 10*GetFrameTime(); - if (camera.position.y < collfloor) { - camera.position.y = collfloor; + if (futurePos.y < collfloor) { + futurePos.y = collfloor; gravity = 0; } - if (camera.position.y > collceiling) { - camera.position.y = collceiling; + if (futurePos.y > collceiling) { + futurePos.y = collceiling; gravity = 0; } + camera.position = futurePos; + + + + camera.target = Vector3Add(camera.position, rotation); } Morault::Gameplay::PlayerController::~PlayerController() diff --git a/ProjectMorault/Morault_Player.h b/ProjectMorault/Morault_Player.h index c377d77..01650b2 100644 --- a/ProjectMorault/Morault_Player.h +++ b/ProjectMorault/Morault_Player.h @@ -27,6 +27,7 @@ namespace Morault { Vector3 rotation; Camera camera; float sensitivityMultiplier = 0.5; + float obesity = 2; void UpdatePlayerController(bool focus, Maps::Map *map, float renderScale); ~PlayerController(); };