修改场景实体查询为unordered_set存储,提升查询效率, 修改Physics的SyncScene也为unordered_set
This commit is contained in:
@@ -343,7 +343,7 @@ namespace Engine {
|
||||
void PhysicsWorld::SyncScene() {
|
||||
auto ® = m_Scene.GetRegistry();
|
||||
auto &bi = m_System->GetBodyInterface();
|
||||
std::vector<u32> invalidated; // 本帧销毁/重建 body → 约束失效传染
|
||||
std::unordered_set<u32> invalidated; // 本帧销毁/重建 body → 约束失效传染
|
||||
|
||||
// 1. 孤儿清理:实体销毁 / 组件被移除 → 移除 body
|
||||
for (auto it = m_Bodies.begin(); it != m_Bodies.end();) {
|
||||
@@ -353,7 +353,7 @@ namespace Engine {
|
||||
bi.RemoveBody(it->second.id);
|
||||
bi.DestroyBody(it->second.id);
|
||||
m_BodyToEntity.erase(it->second.id.GetIndexAndSequenceNumber());
|
||||
invalidated.push_back(it->first);
|
||||
invalidated.insert(it->first);
|
||||
it = m_Bodies.erase(it);
|
||||
} else {
|
||||
++it;
|
||||
@@ -373,7 +373,7 @@ namespace Engine {
|
||||
bi.DestroyBody(found->second.id);
|
||||
m_BodyToEntity.erase(found->second.id.GetIndexAndSequenceNumber());
|
||||
m_Bodies.erase(found);
|
||||
invalidated.push_back(bits);
|
||||
invalidated.insert(bits);
|
||||
}
|
||||
rb.bodyValid = false;
|
||||
continue;
|
||||
@@ -385,7 +385,7 @@ namespace Engine {
|
||||
bi.DestroyBody(found->second.id);
|
||||
m_BodyToEntity.erase(found->second.id.GetIndexAndSequenceNumber());
|
||||
m_Bodies.erase(found);
|
||||
invalidated.push_back(bits);
|
||||
invalidated.insert(bits);
|
||||
}
|
||||
CreateBody(bits, rb, tr);
|
||||
continue;
|
||||
@@ -407,7 +407,7 @@ namespace Engine {
|
||||
bi.DestroyBody(rec.id);
|
||||
m_BodyToEntity.erase(found->second.id.GetIndexAndSequenceNumber());
|
||||
m_Bodies.erase(found);
|
||||
invalidated.push_back(bits);
|
||||
invalidated.insert(bits);
|
||||
rb.bodyValid = false;
|
||||
CreateBody(bits, rb, tr);
|
||||
continue;
|
||||
@@ -456,11 +456,10 @@ namespace Engine {
|
||||
|
||||
// ---- 约束对账 ----
|
||||
|
||||
void PhysicsWorld::SyncConstraints(const std::vector<u32> &invalidated) {
|
||||
void PhysicsWorld::SyncConstraints(const std::unordered_set<u32> &invalidated) {
|
||||
auto ® = m_Scene.GetRegistry();
|
||||
const auto tainted = [&invalidated](u32 bits) {
|
||||
return std::find(invalidated.begin(), invalidated.end(), bits)
|
||||
!= invalidated.end();
|
||||
const auto tainted = [&invalidated](const u32 bits) {
|
||||
return invalidated.count(bits);
|
||||
};
|
||||
|
||||
// 1. owner 实体/组件没了(或被禁用),或任一端 body 本帧被销毁/重建 → 移除
|
||||
@@ -901,6 +900,7 @@ namespace Engine {
|
||||
rb.bodyValid = true;
|
||||
}
|
||||
|
||||
|
||||
void PhysicsWorld::RemoveBody(const u32 entityBits) {
|
||||
const auto found = m_Bodies.find(entityBits);
|
||||
if (found == m_Bodies.end()) return;
|
||||
|
||||
Reference in New Issue
Block a user