Skip to content

Commit

Permalink
Add collision culling for Underworld
Browse files Browse the repository at this point in the history
This should perhaps be handled by a game subsystem that does collision culling in the future, but adds some FPS in big levels for now
  • Loading branch information
TheIndra55 committed Aug 24, 2024
1 parent 71c7fef commit 2eaa6cd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/cdc/math/Vector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Vector.h"

#include <math.h>

void cdc::Vector::operator +=(Vector* b)
{
vec128 = _mm_add_ps(vec128, b->vec128);
Expand Down Expand Up @@ -28,4 +30,13 @@ void cdc::Vector::operator *=(float b)
void cdc::Vector::operator /=(float b)
{
vec128 = _mm_div_ps(vec128, _mm_shuffle_ps(_mm_set_ss(b), _mm_set_ss(b), _MM_SHUFFLE(0, 0, 0, 0)));
}

float cdc::Vector::operator -(cdc::Vector* b)
{
// https://stackoverflow.com/a/2264878/9398242
auto diff = _mm_sub_ps(vec128, b->vec128);
auto sqr = _mm_mul_ps(diff, diff);

return sqrtf(sqr.m128_f32[0] + sqr.m128_f32[1] + sqr.m128_f32[2]);
}
2 changes: 2 additions & 0 deletions src/cdc/math/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace cdc

void operator *=(float b);
void operator /=(float b);

float operator -(cdc::Vector* b);
};

class Vector2 : public Vector
Expand Down
12 changes: 12 additions & 0 deletions src/modules/Draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ void Draw::OnDraw()
ImGui::Checkbox("Player collision", &m_drawPlayerCollision);
ImGui::Checkbox("Enemy collision", &m_drawEnemyCollision);

#ifdef TR8
ImGui::Checkbox("Cull close collision", &m_cull);
#endif

ImGui::InputInt("Terrain group", &m_terrainGroup);
}

Expand Down Expand Up @@ -385,6 +389,7 @@ void Draw::DrawCollision(Level* level)
void Draw::DrawCollision(TerrainGroup* terrainGroup, int flags)
{
auto mesh = terrainGroup->mesh;
auto player = Game::GetPlayerInstance();

// Draw all mesh faces
for (int i = 0; i < mesh->m_numFaces; i++)
Expand All @@ -396,6 +401,13 @@ void Draw::DrawCollision(TerrainGroup* terrainGroup, int flags)
auto y = GetVertice(face->i1, mesh, &mesh->m_position);
auto z = GetVertice(face->i2, mesh, &mesh->m_position);

#ifdef TR8
if (m_cull && (!player || (player->position - &x) > kCollisionFar))
{
continue;
}
#endif

// Draw the face
auto color = flags & kEnemyCollision ? RGBA(255, 0, 255, 10) : RGBA(0, 255, 0, 10);
DrawTriangle(&x, &y, &z, color);
Expand Down
5 changes: 4 additions & 1 deletion src/modules/Draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Draw : public Module
// Collision options
bool m_drawPlayerCollision = true;
bool m_drawEnemyCollision = true;
bool m_cull = true;

int m_terrainGroup = -1;

Expand All @@ -53,4 +54,6 @@ class Draw : public Module
constexpr auto kEnemyCollision = 0x4000;
#else
constexpr auto kEnemyCollision = 0x800;
#endif
#endif

constexpr auto kCollisionFar = 20000;
2 changes: 1 addition & 1 deletion src/render/DrawBatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ DrawBatcher* DrawBatcher::s_batcher = nullptr;

DrawBatcher::DrawBatcher() : m_vertices()
{
m_vertices.reserve(0x2000);
m_vertices.reserve(0x4000);
}

void DrawBatcher::DrawTriangles(DRAWVERTEX* verts, int numtris) noexcept
Expand Down
16 changes: 16 additions & 0 deletions tests/TestMath.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <catch_amalgamated.hpp>
#include <math.h>

#include "cdc/math/Vector.h"

TEST_CASE("vector3")
{
SECTION("can calculate distance")
{
auto first = cdc::Vector3{ 100.f, 11.f, 300.f };
auto second = cdc::Vector3{ 400.f, 10.f, 230.f };
auto distance = first - &second;

REQUIRE(floor(distance) == 308);
}
}

0 comments on commit 2eaa6cd

Please sign in to comment.