From 7a79cda7298a8965892e5f87de58804993ccb4c6 Mon Sep 17 00:00:00 2001 From: Diren D Bharwani Date: Fri, 3 Feb 2023 23:22:21 +0800 Subject: [PATCH] Reworked Collision Tag Interface and Logic --- Assets/CollisionTags.SHConfig | 18 ++++++- Assets/Editor/Layouts/UserLayout.ini | 10 ++-- .../ColliderTagPanel/SHColliderTagPanel.cpp | 54 ++++++++++--------- .../CollisionTags/SHCollisionTagMatrix.cpp | 23 ++++++-- .../CollisionTags/SHCollisionTagMatrix.h | 4 +- .../Collision/Shapes/SHCollisionShape.cpp | 9 +++- .../src/Physics/System/SHPhysicsSystem.cpp | 4 +- 7 files changed, 79 insertions(+), 43 deletions(-) diff --git a/Assets/CollisionTags.SHConfig b/Assets/CollisionTags.SHConfig index 44cfb2dc..7645bbd0 100644 --- a/Assets/CollisionTags.SHConfig +++ b/Assets/CollisionTags.SHConfig @@ -1,2 +1,16 @@ -0 1 65535 -1 2 65415 +0 1 0000000000000000 +1 2 0000000000000000 +2 3 0000000000000000 +3 4 0000000000000000 +4 5 0000000000000000 +5 6 0000000000000000 +6 7 0000000000000000 +7 8 0000000000000000 +8 9 0000000000000000 +9 10 0000000000000000 +10 11 0000000000000000 +11 12 0000000000000000 +12 13 0000000000000000 +13 14 0000000000000000 +14 15 0000000000000000 +15 16 0000000000000000 diff --git a/Assets/Editor/Layouts/UserLayout.ini b/Assets/Editor/Layouts/UserLayout.ini index 2c3c4806..d59a3d01 100644 --- a/Assets/Editor/Layouts/UserLayout.ini +++ b/Assets/Editor/Layouts/UserLayout.ini @@ -132,8 +132,8 @@ Size=165,120 Collapsed=0 [Table][0x9D40AE32,17] -Column 0 Weight=0.9945 -Column 1 Weight=0.9945 +Column 0 Weight=1.0696 +Column 1 Weight=0.9194 Column 2 Weight=0.9945 Column 3 Weight=1.0320 Column 4 Weight=0.9570 @@ -147,8 +147,8 @@ Column 11 Weight=1.0133 Column 12 Weight=0.9945 Column 13 Weight=0.8444 Column 14 Weight=0.9945 -Column 15 Weight=1.2009 -Column 16 Weight=1.0133 +Column 15 Weight=0.8632 +Column 16 Weight=1.3510 [Docking][Data] DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=0,71 Size=1920,941 Split=X @@ -159,7 +159,7 @@ DockSpace ID=0xC5C9B8AB Window=0xBE4044E9 Pos=0,71 Size=1920,941 Split DockNode ID=0x00000002 Parent=0x00000005 SizeRef=1130,1036 Split=Y Selected=0xB41284E7 DockNode ID=0x00000007 Parent=0x00000002 SizeRef=1501,672 Split=Y Selected=0xB41284E7 DockNode ID=0x00000009 Parent=0x00000007 SizeRef=1501,700 Split=Y Selected=0xB41284E7 - DockNode ID=0x0000000B Parent=0x00000009 SizeRef=1501,705 CentralNode=1 Selected=0xB41284E7 + DockNode ID=0x0000000B Parent=0x00000009 SizeRef=1501,705 CentralNode=1 Selected=0x69428B24 DockNode ID=0x0000000C Parent=0x00000009 SizeRef=1501,234 Selected=0xB128252A DockNode ID=0x0000000A Parent=0x00000007 SizeRef=1501,310 Selected=0xD446F7B6 DockNode ID=0x00000008 Parent=0x00000002 SizeRef=1501,338 Selected=0xD9F31532 diff --git a/SHADE_Engine/src/Editor/EditorWindow/ColliderTagPanel/SHColliderTagPanel.cpp b/SHADE_Engine/src/Editor/EditorWindow/ColliderTagPanel/SHColliderTagPanel.cpp index e40bb643..97d9a34b 100644 --- a/SHADE_Engine/src/Editor/EditorWindow/ColliderTagPanel/SHColliderTagPanel.cpp +++ b/SHADE_Engine/src/Editor/EditorWindow/ColliderTagPanel/SHColliderTagPanel.cpp @@ -16,48 +16,52 @@ namespace SHADE ImGui::TableNextRow(); ImGui::PushID("CollisionTagNames"); - for (int i = SHCollisionTag::NUM_LAYERS; i >= 1; --i) + // Draw Layers + // We have 16 layers for each tag + ImGui::TableNextColumn(); + + for (int i = 1; i <= SHCollisionTag::NUM_LAYERS; ++i) { ImGui::TableNextColumn(); - if(i == SHCollisionTag::NUM_LAYERS) continue; + std::string const& tagName = SHCollisionTagMatrix::GetTagName(i); auto tag = SHCollisionTagMatrix::GetTag(i); if (!tag) continue; - //ImGui::Text(tagName.data()); - ImGui::PushID(i); - SHEditorWidgets::InputText("##", [i]{return SHCollisionTagMatrix::GetTagName(i);}, [i](std::string const& value){SHCollisionTagMatrix::GetTag(i)->SetName(value);}, tagName.data(), ImGuiInputTextFlags_EnterReturnsTrue); - ImGui::PopID(); + + // Show the layer number + ImGui::Text(std::to_string(i).c_str()); + } ImGui::PopID(); + + // Draw the table + // Convenient how we have 16 layers & 16 tags huh? + for (int i = 0; i < SHCollisionTag::NUM_LAYERS - 1; ++i) { std::string tagName = SHCollisionTagMatrix::GetTagName(i); auto tag = SHCollisionTagMatrix::GetTag(i); + if(!tag) continue; + ImGui::TableNextRow(); ImGui::TableNextColumn(); - ImGui::Text(tagName.data()); - for (int j = (SHCollisionTag::NUM_LAYERS) - 1; j >= i; --j) + + ImGui::PushID(i); + SHEditorWidgets::InputText("##", [i]{return SHCollisionTagMatrix::GetTagName(i);}, [i](std::string const& value){SHCollisionTagMatrix::GetTag(i)->SetName(value);}, tagName.data(), ImGuiInputTextFlags_EnterReturnsTrue); + ImGui::PopID(); + + // The checkboxes are just to set the layers of each tag + // Nothing else should be altered. + + for (int j = 0; j < SHCollisionTag::NUM_LAYERS; ++j) { - int idx = j; - std::string tagName2 = SHCollisionTagMatrix::GetTagName(idx); - auto tag2 = SHCollisionTagMatrix::GetTag(idx); - - if(!tag2) - continue; - - if(tagName.empty()) - tagName = std::to_string(i); - if(tagName2.empty()) - tagName2 = std::to_string(idx); - ImGui::TableNextColumn(); - if(i == idx) - continue; - std::string label = std::format("##{} vs {}", tagName, tagName2); - if (SHEditorWidgets::CheckBox(label, [tag, &idx] {return tag->GetLayerState(idx); }, [tag, i, idx](bool const& value) {tag->SetLayerState(idx, value); SHCollisionTagMatrix::GetTag(idx)->SetLayerState(i, value); }, label.substr(2))) + + std::string label = std::format("##{}, Layer {}", tagName, j); + if (SHEditorWidgets::CheckBox(label, [tag, &j] {return tag->GetLayerState(j); }, [tag, j](bool const& value) {tag->SetLayerState(j, value); }, label.substr(2))) { isDirty = true; } @@ -77,7 +81,7 @@ namespace SHADE { std::filesystem::path defaultCollisionTagNameFilePath{ ASSET_ROOT }; defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig"); - SHCollisionTagMatrix::Exit(defaultCollisionTagNameFilePath); + SHCollisionTagMatrix::SaveToFile(defaultCollisionTagNameFilePath); isDirty = false; } ImGui::EndDisabled(); diff --git a/SHADE_Engine/src/Physics/Collision/CollisionTags/SHCollisionTagMatrix.cpp b/SHADE_Engine/src/Physics/Collision/CollisionTags/SHCollisionTagMatrix.cpp index 9b2effbd..ac34a449 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionTags/SHCollisionTagMatrix.cpp +++ b/SHADE_Engine/src/Physics/Collision/CollisionTags/SHCollisionTagMatrix.cpp @@ -166,7 +166,7 @@ namespace SHADE /* Public Function Member Definitions */ /*-----------------------------------------------------------------------------------*/ - void SHCollisionTagMatrix::Init(const std::filesystem::path& tagNameFilePath) noexcept + void SHCollisionTagMatrix::LoadFromFile(const std::filesystem::path& tagNameFilePath) noexcept { /** * I HATE FILE IO @@ -240,21 +240,26 @@ namespace SHADE , linesRead + 1 , tagIndex ) + + collisionTags[tagIndex].SetMask(mask); } else { mask = static_cast(std::stoi(maskString)); + for (int i = 0; i < SHCollisionTag::NUM_LAYERS; ++i) + { + const bool LAYER_STATE = mask & 1U << i; + collisionTags[tagIndex].SetLayerState(i, LAYER_STATE); + } } - collisionTags[tagIndex].SetMask(mask); - ss.clear(); } collisionTagNamesFile.close(); } - void SHCollisionTagMatrix::Exit(const std::filesystem::path& tagNameFilePath) noexcept + void SHCollisionTagMatrix::SaveToFile(const std::filesystem::path& tagNameFilePath) noexcept { std::ofstream collisionTagNamesFile { tagNameFilePath }; @@ -266,7 +271,15 @@ namespace SHADE // Index Name Mask for (int i = 0; i < SHCollisionTag::NUM_LAYERS; ++i) - collisionTagNamesFile << i << " " << collisionTags[i].GetName() << " " << collisionTags[i].GetMask() << std::endl; + { + collisionTagNamesFile << i << " " << collisionTags[i].GetName() << " "; + + // Write 1 or 0 for each layer for improved clarity + for (int j = 0; j < SHCollisionTag::NUM_LAYERS; ++j) + collisionTagNamesFile << (collisionTags[i].GetMask() & 1U << j ? 1 : 0); + + collisionTagNamesFile << std::endl; + } collisionTagNamesFile.close(); } diff --git a/SHADE_Engine/src/Physics/Collision/CollisionTags/SHCollisionTagMatrix.h b/SHADE_Engine/src/Physics/Collision/CollisionTags/SHCollisionTagMatrix.h index 6ece83ac..726ba8f6 100644 --- a/SHADE_Engine/src/Physics/Collision/CollisionTags/SHCollisionTagMatrix.h +++ b/SHADE_Engine/src/Physics/Collision/CollisionTags/SHCollisionTagMatrix.h @@ -55,8 +55,8 @@ namespace SHADE /* Function Members */ /*---------------------------------------------------------------------------------*/ - static void Init (const std::filesystem::path& tagNameFilePath) noexcept; - static void Exit (const std::filesystem::path& tagNameFilePath) noexcept; + static void LoadFromFile (const std::filesystem::path& tagNameFilePath) noexcept; + static void SaveToFile (const std::filesystem::path& tagNameFilePath) noexcept; /** * @brief diff --git a/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.cpp b/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.cpp index 219c3e47..bd67d42f 100644 --- a/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.cpp +++ b/SHADE_Engine/src/Physics/Collision/Shapes/SHCollisionShape.cpp @@ -120,7 +120,10 @@ namespace SHADE collisionTag = newCollisionTag; if (rp3dCollider) + { + rp3dCollider->setCollisionCategoryBits(collisionTag->GetMask()); rp3dCollider->setCollideWithMaskBits(collisionTag->GetMask()); + } } void SHCollisionShape::SetFriction(float friction) noexcept @@ -150,7 +153,6 @@ namespace SHADE // Recompute the bodies' mass dynamic_cast(collider->collisionBody)->updateMassPropertiesFromColliders(); } - } void SHCollisionShape::SetMaterial(const SHPhysicsMaterial& newMaterial) noexcept @@ -204,8 +206,11 @@ namespace SHADE void SHCollisionShape::UpdateCollisionTags() noexcept { - if (collisionTag->IsDirty()) + if (rp3dCollider && collisionTag->IsDirty()) + { + rp3dCollider->setCollisionCategoryBits(collisionTag->GetMask()); rp3dCollider->setCollideWithMaskBits(collisionTag->GetMask()); + } } diff --git a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp index ede0ede2..f4529a40 100644 --- a/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp +++ b/SHADE_Engine/src/Physics/System/SHPhysicsSystem.cpp @@ -122,7 +122,7 @@ namespace SHADE // Initialise collision tags std::filesystem::path defaultCollisionTagNameFilePath { ASSET_ROOT }; defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig"); - SHCollisionTagMatrix::Init(defaultCollisionTagNameFilePath); + SHCollisionTagMatrix::LoadFromFile(defaultCollisionTagNameFilePath); // Register Events for (int i = 0; i < NUM_EVENT_FUNCTIONS; ++i) @@ -145,7 +145,7 @@ namespace SHADE // Write collision tag names to file std::filesystem::path defaultCollisionTagNameFilePath { ASSET_ROOT }; defaultCollisionTagNameFilePath.append("CollisionTags.SHConfig"); - SHCollisionTagMatrix::Exit(defaultCollisionTagNameFilePath); + SHCollisionTagMatrix::SaveToFile(defaultCollisionTagNameFilePath); } const std::vector& SHPhysicsSystem::Raycast(const SHRaycaster::RaycastInfo& info) noexcept