chore: Remove dpEntity pointers from collision checking (#1529)

* chore: Remove dpEntity pointers from collision checking

* Update fn documentation in ProximityMonitorComponent.h

* use more idiomatic method to calculate vector index

* feedback

* missed a ranges::find replacement

* adjust for feedback. last changes tonight.

* okay, also remove unneeded include. then sleep.

* for real tho

* update to use unordered_set instead of set
This commit is contained in:
jadebenn 2024-04-05 00:52:26 -05:00 committed by GitHub
parent b340d7c8f9
commit 06e7d57e0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 51 additions and 51 deletions

View File

@ -150,13 +150,13 @@ void BaseCombatAIComponent::Update(const float deltaTime) {
m_dpEntityEnemy->SetPosition(m_Parent->GetPosition()); m_dpEntityEnemy->SetPosition(m_Parent->GetPosition());
//Process enter events //Process enter events
for (auto en : m_dpEntity->GetNewObjects()) { for (const auto id : m_dpEntity->GetNewObjects()) {
m_Parent->OnCollisionPhantom(en->GetObjectID()); m_Parent->OnCollisionPhantom(id);
} }
//Process exit events //Process exit events
for (auto en : m_dpEntity->GetRemovedObjects()) { for (const auto id : m_dpEntity->GetRemovedObjects()) {
m_Parent->OnCollisionLeavePhantom(en->GetObjectID()); m_Parent->OnCollisionLeavePhantom(id);
} }
// Check if we should stop the tether effect // Check if we should stop the tether effect

View File

@ -187,7 +187,7 @@ PhantomPhysicsComponent::PhantomPhysicsComponent(Entity* parent) : PhysicsCompon
//add fallback cube: //add fallback cube:
m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 2.0f, 2.0f, 2.0f); m_dpEntity = new dpEntity(m_Parent->GetObjectID(), 2.0f, 2.0f, 2.0f);
} }
m_dpEntity->SetScale(m_Scale); m_dpEntity->SetScale(m_Scale);
m_dpEntity->SetRotation(m_Rotation); m_dpEntity->SetRotation(m_Rotation);
m_dpEntity->SetPosition(m_Position); m_dpEntity->SetPosition(m_Position);
@ -323,14 +323,13 @@ void PhantomPhysicsComponent::Update(float deltaTime) {
if (!m_dpEntity) return; if (!m_dpEntity) return;
//Process enter events //Process enter events
for (auto en : m_dpEntity->GetNewObjects()) { for (const auto id : m_dpEntity->GetNewObjects()) {
if (!en) continue; ApplyCollisionEffect(id, m_EffectType, m_DirectionalMultiplier);
ApplyCollisionEffect(en->GetObjectID(), m_EffectType, m_DirectionalMultiplier); m_Parent->OnCollisionPhantom(id);
m_Parent->OnCollisionPhantom(en->GetObjectID());
//If we are a respawn volume, inform the client: //If we are a respawn volume, inform the client:
if (m_IsRespawnVolume) { if (m_IsRespawnVolume) {
auto entity = Game::entityManager->GetEntity(en->GetObjectID()); auto* const entity = Game::entityManager->GetEntity(id);
if (entity) { if (entity) {
GameMessages::SendPlayerReachedRespawnCheckpoint(entity, m_RespawnPos, m_RespawnRot); GameMessages::SendPlayerReachedRespawnCheckpoint(entity, m_RespawnPos, m_RespawnRot);
@ -341,10 +340,9 @@ void PhantomPhysicsComponent::Update(float deltaTime) {
} }
//Process exit events //Process exit events
for (auto en : m_dpEntity->GetRemovedObjects()) { for (const auto id : m_dpEntity->GetRemovedObjects()) {
if (!en) continue; ApplyCollisionEffect(id, m_EffectType, 1.0f);
ApplyCollisionEffect(en->GetObjectID(), m_EffectType, 1.0f); m_Parent->OnCollisionLeavePhantom(id);
m_Parent->OnCollisionLeavePhantom(en->GetObjectID());
} }
} }

View File

@ -5,7 +5,7 @@
#include "EntityManager.h" #include "EntityManager.h"
#include "SimplePhysicsComponent.h" #include "SimplePhysicsComponent.h"
const std::map<LWOOBJID, dpEntity*> ProximityMonitorComponent::m_EmptyObjectMap = {}; const std::unordered_set<LWOOBJID> ProximityMonitorComponent::m_EmptyObjectSet = {};
ProximityMonitorComponent::ProximityMonitorComponent(Entity* parent, int radiusSmall, int radiusLarge) : Component(parent) { ProximityMonitorComponent::ProximityMonitorComponent(Entity* parent, int radiusSmall, int radiusLarge) : Component(parent) {
if (radiusSmall != -1 && radiusLarge != -1) { if (radiusSmall != -1 && radiusLarge != -1) {
@ -38,26 +38,26 @@ void ProximityMonitorComponent::SetProximityRadius(dpEntity* entity, const std::
m_ProximitiesData.insert(std::make_pair(name, entity)); m_ProximitiesData.insert(std::make_pair(name, entity));
} }
const std::map<LWOOBJID, dpEntity*>& ProximityMonitorComponent::GetProximityObjects(const std::string& name) { const std::unordered_set<LWOOBJID>& ProximityMonitorComponent::GetProximityObjects(const std::string& name) {
const auto& iter = m_ProximitiesData.find(name); const auto iter = m_ProximitiesData.find(name);
if (iter == m_ProximitiesData.end()) { if (iter == m_ProximitiesData.cend()) {
return m_EmptyObjectMap; return m_EmptyObjectSet;
} }
return iter->second->GetCurrentlyCollidingObjects(); return iter->second->GetCurrentlyCollidingObjects();
} }
bool ProximityMonitorComponent::IsInProximity(const std::string& name, LWOOBJID objectID) { bool ProximityMonitorComponent::IsInProximity(const std::string& name, LWOOBJID objectID) {
const auto& iter = m_ProximitiesData.find(name); const auto iter = m_ProximitiesData.find(name);
if (iter == m_ProximitiesData.end()) { if (iter == m_ProximitiesData.cend()) {
return false; return false;
} }
const auto& collitions = iter->second->GetCurrentlyCollidingObjects(); const auto& collisions = iter->second->GetCurrentlyCollidingObjects();
return collitions.find(objectID) != collitions.end(); return collisions.contains(objectID);
} }
void ProximityMonitorComponent::Update(float deltaTime) { void ProximityMonitorComponent::Update(float deltaTime) {
@ -66,13 +66,13 @@ void ProximityMonitorComponent::Update(float deltaTime) {
prox.second->SetPosition(m_Parent->GetPosition()); prox.second->SetPosition(m_Parent->GetPosition());
//Process enter events //Process enter events
for (auto* en : prox.second->GetNewObjects()) { for (const auto id : prox.second->GetNewObjects()) {
m_Parent->OnCollisionProximity(en->GetObjectID(), prox.first, "ENTER"); m_Parent->OnCollisionProximity(id, prox.first, "ENTER");
} }
//Process exit events //Process exit events
for (auto* en : prox.second->GetRemovedObjects()) { for (const auto id : prox.second->GetRemovedObjects()) {
m_Parent->OnCollisionProximity(en->GetObjectID(), prox.first, "LEAVE"); m_Parent->OnCollisionProximity(id, prox.first, "LEAVE");
} }
} }
} }

View File

@ -6,6 +6,8 @@
#ifndef PROXIMITYMONITORCOMPONENT_H #ifndef PROXIMITYMONITORCOMPONENT_H
#define PROXIMITYMONITORCOMPONENT_H #define PROXIMITYMONITORCOMPONENT_H
#include <unordered_set>
#include "BitStream.h" #include "BitStream.h"
#include "Entity.h" #include "Entity.h"
#include "dpWorld.h" #include "dpWorld.h"
@ -42,9 +44,9 @@ public:
/** /**
* Returns the last of entities that are used to check proximity, given a name * Returns the last of entities that are used to check proximity, given a name
* @param name the proximity name to retrieve physics objects for * @param name the proximity name to retrieve physics objects for
* @return a map of physics entities for this name, indexed by object ID * @return a set of physics entity object IDs for this name
*/ */
const std::map<LWOOBJID, dpEntity*>& GetProximityObjects(const std::string& name); const std::unordered_set<LWOOBJID>& GetProximityObjects(const std::string& name);
/** /**
* Checks if the passed object is in proximity of the named proximity sensor * Checks if the passed object is in proximity of the named proximity sensor
@ -70,7 +72,7 @@ private:
/** /**
* Default value for the proximity data * Default value for the proximity data
*/ */
static const std::map<LWOOBJID, dpEntity*> m_EmptyObjectMap; static const std::unordered_set<LWOOBJID> m_EmptyObjectSet;
}; };
#endif // PROXIMITYMONITORCOMPONENT_H #endif // PROXIMITYMONITORCOMPONENT_H

View File

@ -3,8 +3,6 @@
#include "dpShapeBox.h" #include "dpShapeBox.h"
#include "dpGrid.h" #include "dpGrid.h"
#include <iostream>
dpEntity::dpEntity(const LWOOBJID& objectID, dpShapeType shapeType, bool isStatic) { dpEntity::dpEntity(const LWOOBJID& objectID, dpShapeType shapeType, bool isStatic) {
m_ObjectID = objectID; m_ObjectID = objectID;
m_IsStatic = isStatic; m_IsStatic = isStatic;
@ -76,16 +74,17 @@ void dpEntity::CheckCollision(dpEntity* other) {
return; return;
} }
bool wasFound = m_CurrentlyCollidingObjects.contains(other->GetObjectID()); const auto objId = other->GetObjectID();
const auto objItr = m_CurrentlyCollidingObjects.find(objId);
bool isColliding = m_CollisionShape->IsColliding(other->GetShape()); const bool wasFound = objItr != m_CurrentlyCollidingObjects.cend();
const bool isColliding = m_CollisionShape->IsColliding(other->GetShape());
if (isColliding && !wasFound) { if (isColliding && !wasFound) {
m_CurrentlyCollidingObjects.emplace(other->GetObjectID(), other); m_CurrentlyCollidingObjects.emplace(objId);
m_NewObjects.push_back(other); m_NewObjects.push_back(objId);
} else if (!isColliding && wasFound) { } else if (!isColliding && wasFound) {
m_CurrentlyCollidingObjects.erase(other->GetObjectID()); m_CurrentlyCollidingObjects.erase(objItr);
m_RemovedObjects.push_back(other); m_RemovedObjects.push_back(objId);
} }
} }

View File

@ -2,7 +2,8 @@
#include "NiPoint3.h" #include "NiPoint3.h"
#include "NiQuaternion.h" #include "NiQuaternion.h"
#include <vector> #include <vector>
#include <map> #include <unordered_set>
#include <span>
#include "dCommonVars.h" #include "dCommonVars.h"
#include "dpCommon.h" #include "dpCommon.h"
@ -49,9 +50,9 @@ public:
bool GetSleeping() const { return m_Sleeping; } bool GetSleeping() const { return m_Sleeping; }
void SetSleeping(bool value) { m_Sleeping = value; } void SetSleeping(bool value) { m_Sleeping = value; }
const std::vector<dpEntity*>& GetNewObjects() const { return m_NewObjects; } const std::span<const LWOOBJID> GetNewObjects() const { return m_NewObjects; }
const std::vector<dpEntity*>& GetRemovedObjects() const { return m_RemovedObjects; } const std::span<const LWOOBJID> GetRemovedObjects() const { return m_RemovedObjects; }
const std::map<LWOOBJID, dpEntity*>& GetCurrentlyCollidingObjects() const { return m_CurrentlyCollidingObjects; } const std::unordered_set<LWOOBJID>& GetCurrentlyCollidingObjects() const { return m_CurrentlyCollidingObjects; }
void PreUpdate() { m_NewObjects.clear(); m_RemovedObjects.clear(); } void PreUpdate() { m_NewObjects.clear(); m_RemovedObjects.clear(); }
@ -80,7 +81,7 @@ private:
bool m_IsGargantuan = false; bool m_IsGargantuan = false;
std::vector<dpEntity*> m_NewObjects; std::vector<LWOOBJID> m_NewObjects;
std::vector<dpEntity*> m_RemovedObjects; std::vector<LWOOBJID> m_RemovedObjects;
std::map<LWOOBJID, dpEntity*> m_CurrentlyCollidingObjects; std::unordered_set<LWOOBJID> m_CurrentlyCollidingObjects;
}; };

View File

@ -21,7 +21,7 @@ void WanderingVendor::OnProximityUpdate(Entity* self, Entity* entering, std::str
const auto proxObjs = proximityMonitorComponent->GetProximityObjects("playermonitor"); const auto proxObjs = proximityMonitorComponent->GetProximityObjects("playermonitor");
bool foundPlayer = false; bool foundPlayer = false;
for (const auto id : proxObjs | std::views::keys) { for (const auto id : proxObjs) {
auto* entity = Game::entityManager->GetEntity(id); auto* entity = Game::entityManager->GetEntity(id);
if (entity && entity->IsPlayer()) { if (entity && entity->IsPlayer()) {
foundPlayer = true; foundPlayer = true;

View File

@ -23,13 +23,13 @@ void AgBusDoor::OnProximityUpdate(Entity* self, Entity* entering, std::string na
m_Counter = 0; m_Counter = 0;
m_OuterCounter = 0; m_OuterCounter = 0;
for (const auto& pair : proximityMonitorComponent->GetProximityObjects("busDoor")) { for (const auto id : proximityMonitorComponent->GetProximityObjects("busDoor")) {
auto* entity = Game::entityManager->GetEntity(pair.first); const auto* const entity = Game::entityManager->GetEntity(id);
if (entity != nullptr && entity->IsPlayer()) m_Counter++; if (entity != nullptr && entity->IsPlayer()) m_Counter++;
} }
for (const auto& pair : proximityMonitorComponent->GetProximityObjects("busDoorOuter")) { for (const auto id : proximityMonitorComponent->GetProximityObjects("busDoorOuter")) {
auto* entity = Game::entityManager->GetEntity(pair.first); const auto* const entity = Game::entityManager->GetEntity(id);
if (entity != nullptr && entity->IsPlayer()) m_OuterCounter++; if (entity != nullptr && entity->IsPlayer()) m_OuterCounter++;
} }