diff --git a/dGame/dComponents/ProximityMonitorComponent.cpp b/dGame/dComponents/ProximityMonitorComponent.cpp index fd3f1b5d..14e440d5 100644 --- a/dGame/dComponents/ProximityMonitorComponent.cpp +++ b/dGame/dComponents/ProximityMonitorComponent.cpp @@ -4,6 +4,8 @@ #include "ControllablePhysicsComponent.h" #include "EntityManager.h" #include "SimplePhysicsComponent.h" +#include "Amf3.h" +#include "dpShapeSphere.h" const std::unordered_set ProximityMonitorComponent::m_EmptyObjectSet = {}; @@ -12,6 +14,7 @@ ProximityMonitorComponent::ProximityMonitorComponent(Entity* parent, const int32 SetProximityRadius(radiusSmall, "rocketSmall"); SetProximityRadius(radiusLarge, "rocketLarge"); } + RegisterMsg(&ProximityMonitorComponent::OnGetObjectReportInfo); } ProximityMonitorComponent::~ProximityMonitorComponent() { @@ -60,6 +63,31 @@ bool ProximityMonitorComponent::IsInProximity(const std::string& name, LWOOBJID return collisions.contains(objectID); } +bool ProximityMonitorComponent::OnGetObjectReportInfo(GameMessages::GetObjectReportInfo& reportInfo) { + auto& proxInfo = reportInfo.info->PushDebug("Proximity Monitor"); + for (const auto& [name, entity] : m_ProximitiesData) { + if (!entity) continue; + auto& proxAmf = proxInfo.PushDebug(name); + const auto* const shape = entity->GetShape(); + if (shape && shape->GetShapeType() == dpShapeType::Sphere) { + const auto* const sphere = static_cast(shape); + proxAmf.PushDebug("Radius") = sphere->GetRadius(); + } + proxAmf.PushDebug("Sleeping") = entity->GetSleeping(); + proxAmf.PushDebug("Scale") = entity->GetScale(); + proxAmf.PushDebug("Gargantuan") = entity->GetIsGargantuan(); + proxAmf.PushDebug("Static") = entity->GetIsStatic(); + proxAmf.PushDebug("Position").PushDebug(entity->GetPosition()); + proxAmf.PushDebug("Rotation").PushDebug(entity->GetRotation()); + auto& collidingAmf = proxAmf.PushDebug("Colliding Objects"); + for (const auto& colliding : entity->GetCurrentlyCollidingObjects()) { + collidingAmf.PushDebug(std::to_string(colliding)); + } + } + + return true; +} + void ProximityMonitorComponent::Update(float deltaTime) { for (const auto& prox : m_ProximitiesData) { if (!prox.second) continue; diff --git a/dGame/dComponents/ProximityMonitorComponent.h b/dGame/dComponents/ProximityMonitorComponent.h index b83c0df0..b4aa1f1a 100644 --- a/dGame/dComponents/ProximityMonitorComponent.h +++ b/dGame/dComponents/ProximityMonitorComponent.h @@ -64,6 +64,7 @@ public: private: + bool OnGetObjectReportInfo(GameMessages::GetObjectReportInfo& reportInfo); /** * All the proximity sensors for this component, indexed by name */ diff --git a/dPhysics/dpGrid.cpp b/dPhysics/dpGrid.cpp index 7a0db1f8..c338efe3 100644 --- a/dPhysics/dpGrid.cpp +++ b/dPhysics/dpGrid.cpp @@ -2,6 +2,7 @@ #include "dpEntity.h" #include +#include dpGrid::dpGrid(int numCells, int cellSize) { NUM_CELLS = numCells; @@ -122,38 +123,35 @@ void dpGrid::HandleEntity(dpEntity* entity, dpEntity* other) { void dpGrid::HandleCell(int x, int z, float deltaTime) { auto& entities = m_Cells[x][z]; //vector of entities contained within this cell. - for (auto en : entities) { + for (auto* en : entities) { if (!en) continue; if (en->GetIsStatic() || en->GetSleeping()) continue; //Check against all entities that are in the same cell as us - for (auto other : entities) - HandleEntity(en, other); + for (auto other : entities) HandleEntity(en, other); - //To try neighbouring cells as well: (can be disabled if needed) - //we only check 4 of the 8 neighbouring cells, otherwise we'd get duplicates and cpu cycles wasted... - - if (x > 0 && z > 0) { - for (auto other : m_Cells[x - 1][z - 1]) - HandleEntity(en, other); + // All 8 neighbours in one pass. + // staticOnly=false — canonical 4: covers each dynamic-vs-dynamic pair exactly once, + // since the higher-index cell checks back to the lower-index cell. + // staticOnly=true — skipped 4: dynamic entities there are handled when those cells + // process their own en loop; static ones never drive a loop, so + // we handle them here explicitly to avoid missing exits. + struct NeighbourCheck { int dx, dz; bool staticOnly; }; + constexpr NeighbourCheck kNeighbours[8] = { + { -1, -1, false }, { -1, 0, false }, { 0, -1, false }, { -1, 1, false }, + { 1, -1, true }, { 1, 0, true }, { 0, 1, true }, { 1, 1, true }, + }; + for (auto [dx, dz, staticOnly] : kNeighbours) { + const int nx = x + dx; + const int nz = z + dz; + // Ensure the cell we're checking is within the valid range + if (nx < 0 || nx >= NUM_CELLS || nz < 0 || nz >= NUM_CELLS) continue; + for (auto* other : m_Cells[nx][nz]) { + if (!staticOnly || (other && other->GetIsStatic())) + HandleEntity(en, other); + } } - if (x > 0) { - for (auto other : m_Cells[x - 1][z]) - HandleEntity(en, other); - } - - if (z > 0) { - for (auto other : m_Cells[x][z - 1]) - HandleEntity(en, other); - } - - if (x > 0 && z < NUM_CELLS - 1) { - for (auto other : m_Cells[x - 1][z + 1]) - HandleEntity(en, other); - } - - for (auto& [id, entity] : m_GargantuanObjects) - HandleEntity(en, entity); + for (auto* entity : m_GargantuanObjects | std::views::values) HandleEntity(en, entity); } }