From d79f96e83d5020935e3c1284ed9e059310125089 Mon Sep 17 00:00:00 2001 From: Panos Karabelas Date: Wed, 15 Jan 2025 02:24:24 +0000 Subject: [PATCH] [physicsbody] walking down the hierarchy to create compound shape can now be toggled when setting the shape typ --- runtime/Game/Game.cpp | 47 ++++++++++-------------- runtime/World/Components/PhysicsBody.cpp | 21 +++++++---- runtime/World/Components/PhysicsBody.h | 3 +- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/runtime/Game/Game.cpp b/runtime/Game/Game.cpp index 23a6c9ecb..26efedbd4 100644 --- a/runtime/Game/Game.cpp +++ b/runtime/Game/Game.cpp @@ -429,7 +429,7 @@ namespace spartan PhysicsBody* physics_body = entity->AddComponent().get(); physics_body->SetMass(8.0f); - physics_body->SetShapeType(PhysicsShape::Mesh); + physics_body->SetShapeType(PhysicsShape::Mesh, true); } // damaged helmet @@ -815,7 +815,7 @@ namespace spartan entity->SetScale(Vector3(0.1f, 0.1f, 0.1f)); PhysicsBody* physics_body = entity->AddComponent().get(); - physics_body->SetShapeType(PhysicsShape::Mesh); + physics_body->SetShapeType(PhysicsShape::Mesh, true); } } @@ -904,17 +904,8 @@ namespace spartan entity->SetPosition(Vector3(0.0f, 0.0f, 0.0f)); entity->SetScale(Vector3(100.0f, 100.0f, 100.0f)); - // enable physics for all meshes - vector entities; - entity->GetDescendants(&entities); - for (Entity* entity : entities) - { - if (entity->GetComponent() != nullptr) - { - PhysicsBody* physics_body = entity->AddComponent().get(); - physics_body->SetShapeType(PhysicsShape::Mesh); - } - } + PhysicsBody* physics_body = entity->AddComponent().get(); + physics_body->SetShapeType(PhysicsShape::Mesh, true); } } @@ -931,18 +922,6 @@ namespace spartan entity->SetPosition(Vector3(0.0f, 0.03f, 0.0f)); entity->SetScale(Vector3(2.5f, 2.5f, 2.5f)); - // enable physics for all meshes - vector entities; - entity->GetDescendants(&entities); - for (Entity* entity : entities) - { - if (entity->GetComponent() != nullptr) - { - PhysicsBody* physics_body = entity->AddComponent().get(); - physics_body->SetShapeType(PhysicsShape::Mesh); - } - } - // make the radiator metallic if (shared_ptr renderable = entity->GetDescendantByName("Mesh_93")->GetComponent()) { @@ -981,9 +960,9 @@ namespace spartan } // disable window blinds - entity->GetDescendantByName("Default_1")->SetActive(false); - entity->GetDescendantByName("Default_2")->SetActive(false); - entity->GetDescendantByName("Default_3")->SetActive(false); + entity->GetDescendantByName("Default_1")->SetActive(false); + entity->GetDescendantByName("Default_2")->SetActive(false); + entity->GetDescendantByName("Default_3")->SetActive(false); // make the same come in through the window m_default_light_directional->SetRotation(Quaternion::FromEulerAngles(30.0f, 180.0f, 0.0f)); @@ -1041,6 +1020,18 @@ namespace spartan } } } + + // enable physics for all meshes + vector entities; + entity->GetDescendants(&entities); + for (Entity* entity : entities) + { + if (entity->GetComponent() != nullptr) + { + PhysicsBody* physics_body = entity->AddComponent().get(); + physics_body->SetShapeType(PhysicsShape::Mesh); + } + } } } diff --git a/runtime/World/Components/PhysicsBody.cpp b/runtime/World/Components/PhysicsBody.cpp index 78a882eae..19552ee59 100644 --- a/runtime/World/Components/PhysicsBody.cpp +++ b/runtime/World/Components/PhysicsBody.cpp @@ -694,12 +694,14 @@ namespace spartan m_size.z = helper::Clamp(m_size.z, helper::SMALL_FLOAT, INFINITY); } - void PhysicsBody::SetShapeType(PhysicsShape type) + void PhysicsBody::SetShapeType(PhysicsShape type, const bool replicate_hierarchy) { if (m_shape_type == type) return; - m_shape_type = type; + m_shape_type = type; + m_replicate_hierarchy = replicate_hierarchy; + UpdateShape(); } @@ -871,7 +873,7 @@ namespace spartan case PhysicsShape::Mesh: { - function recursive_renderable_to_shape = [&](Entity* entity, btCompoundShape* shape_compount, const bool is_root_entity) + function recursive_renderable_to_shape = [&](Entity* entity, btCompoundShape* shape_compount, const bool is_root_entity, const bool replicate_hierarchy) { // get renderable shared_ptr renderable = entity->GetComponent(); @@ -946,16 +948,19 @@ namespace spartan } // recursively process all children - vector children = entity->GetChildren(); - for (Entity* child : children) - { - recursive_renderable_to_shape(child, shape_compount, false); + if (replicate_hierarchy) + { + vector children = entity->GetChildren(); + for (Entity* child : children) + { + recursive_renderable_to_shape(child, shape_compount, false, replicate_hierarchy); + } } }; // recursively create a compound shape that contains the entity's hierarchy btCompoundShape* shape_compound = new btCompoundShape(); - recursive_renderable_to_shape(GetEntity(), shape_compound, true); + recursive_renderable_to_shape(GetEntity(), shape_compound, true, m_replicate_hierarchy); m_shape = shape_compound; diff --git a/runtime/World/Components/PhysicsBody.h b/runtime/World/Components/PhysicsBody.h index e3a5bae20..f27783a06 100644 --- a/runtime/World/Components/PhysicsBody.h +++ b/runtime/World/Components/PhysicsBody.h @@ -146,7 +146,7 @@ namespace spartan // shape type PhysicsShape GetShapeType() const { return m_shape_type; } - void SetShapeType(PhysicsShape type); + void SetShapeType(PhysicsShape type, const bool replicate_hierarchy = false); // body type PhysicsBodyType GetBodyType() const { return m_body_type; } @@ -190,6 +190,7 @@ namespace spartan void* m_shape = nullptr; void* m_rigid_body = nullptr; std::shared_ptr m_car = nullptr; + bool m_replicate_hierarchy = false; std::vector m_mesh_data; std::vector m_constraints; };