From b91d5eee3122664d3c60ef91c35cb83fbea5c2f1 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Tue, 11 Apr 2023 15:39:27 -0500 Subject: [PATCH] Fix Collision of bullets on walls. (Forgot to factor in fElapsedTime) --- Faceball2030/main.cpp | 15 ++++++++------- Faceball2030/main.h | 3 ++- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Faceball2030/main.cpp b/Faceball2030/main.cpp index 02967c4..bcd89df 100644 --- a/Faceball2030/main.cpp +++ b/Faceball2030/main.cpp @@ -462,7 +462,7 @@ void FaceBall::RenderMesh(mat4x4&matView,std::vector&vecTrianglesToRas mat4x4 localMat = Matrix_MakeIdentity(); mat4x4 rotMat = Matrix_MakeRotationY(o.rot); localMat = Matrix_MultiplyMatrix(localMat, rotMat); - mat4x4 matTrans = Matrix_MakeTranslation(o.pos.x-o.radius, o.pos.y-o.radius, o.pos.z - o.radius); + mat4x4 matTrans = Matrix_MakeTranslation(o.pos.x, o.pos.y, o.pos.z); localMat = Matrix_MultiplyMatrix(localMat, matTrans); triTransformed.p[0] = Matrix_MultiplyVector(localMat, tri.p[0]); @@ -495,7 +495,7 @@ void FaceBall::RenderMesh(mat4x4&matView,std::vector&vecTrianglesToRas triViewed.uv[1] = triTransformed.uv[1]; triViewed.uv[2] = triTransformed.uv[2]; triViewed.col = Pixel(triTransformed.col.r * dp * dp, triTransformed.col.g * dp * dp, triTransformed.col.b * dp * dp); - float dist = std::sqrtf(std::powf((freeRoam ? freeRoamCamera : player.GetPos()).x - tri.p[0].x, 2) + std::powf((freeRoam ? freeRoamCamera : player.GetPos()).y - tri.p[0].y, 2) + std::powf((freeRoam ? freeRoamCamera : player.GetPos()).z - tri.p[0].z, 2)); + float dist = std::sqrtf(std::powf((freeRoam ? freeRoamCamera : player.GetPos()).x - triTransformed.p[0].x, 2) + std::powf((freeRoam ? freeRoamCamera : player.GetPos()).y - triTransformed.p[0].y, 2) + std::powf((freeRoam ? freeRoamCamera : player.GetPos()).z - triTransformed.p[0].z, 2)); float colorMult = dist > 5 * PI / 3 ? 0 : std::sinf(0.3 * dist + PI / 2); triViewed.col = Pixel(triViewed.col.r * colorMult, triViewed.col.g * colorMult, triViewed.col.b * colorMult); //triViewed.col = triTransformed.col; @@ -663,7 +663,7 @@ void FaceBall::RenderWorld() { } void FaceBall::HandleKeys(float fElapsedTime) { - vec3d vForward = Vector_Mul(vLookDir, std::min(player.GetRadius()-0.00001f,2*fElapsedTime)); + vec3d vForward = Vector_Mul(vLookDir, std::min(player.GetRadius()-0.00001f,moveSpd*fElapsedTime)); if (freeRoam) { if (GetKey(DOWN).bHeld) { pitch -= 1 * fElapsedTime; @@ -675,7 +675,7 @@ void FaceBall::HandleKeys(float fElapsedTime) { else { pitch = 0; if (GetMouse(0).bPressed) { - bullets.push_back({ bullet,{player.GetPos().x + player.GetRadius(),player.GetPos().y, player.GetPos().z + player.GetRadius()},fYaw,0.25f,{shotSpd * std::cosf(fYaw),shotSpd * std::sinf(fYaw)} }); + bullets.push_back({ bullet,{player.GetPos().x,player.GetPos().y - 0.15f, player.GetPos().z},fYaw,0.125f,{shotSpd * std::cosf(fYaw),shotSpd * std::sinf(fYaw)} }); } } if (GetKey(W).bHeld) { @@ -771,9 +771,10 @@ bool FaceBall::OnUserCreate() } bool Bullet::Update(float fElapsedTime) { - if (!FaceBall::CheckCollision({ spd.x,0,spd.y }, {pos.x,pos.z}, radius)) { - pos.x += spd.x*fElapsedTime; - pos.z += spd.y*fElapsedTime; + vec3d adjustedSpd = { spd.x * fElapsedTime,0,spd.y * fElapsedTime }; + if (!FaceBall::CheckCollision(adjustedSpd, {pos.x,pos.z}, 0.05)) { + pos.x += adjustedSpd.x; + pos.z += adjustedSpd.z; } else { return false; diff --git a/Faceball2030/main.h b/Faceball2030/main.h index fc02bb9..5e6d19f 100644 --- a/Faceball2030/main.h +++ b/Faceball2030/main.h @@ -202,7 +202,8 @@ class FaceBall : public PixelGameEngine Player player = { {3.7,0.3,0.7}, {{0.5,0.5},0.2} }; vec3d freeRoamCamera = { 1,0.5,1 }; - float shotSpd = 1.5f; + float shotSpd = 3.0f; + float moveSpd = 2.0f; vec3d Matrix_MultiplyVector(mat4x4& m, vec3d& i); mat4x4 Matrix_MakeIdentity();