From c2dcd3aac39843c99ee8988704f10b67b6dbc2ef Mon Sep 17 00:00:00 2001 From: Panos Karabelas Date: Tue, 7 Jan 2025 01:24:11 +0000 Subject: [PATCH] [physicsbody] reduced the number of rigid bodies that would be created when initializing a physics body (due to property changes requiring rigid body recreation) --- runtime/Game/Game.cpp | 19 +++++++++-------- runtime/World/Components/PhysicsBody.cpp | 26 +++++------------------- runtime/World/Components/PhysicsBody.h | 5 +++-- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/runtime/Game/Game.cpp b/runtime/Game/Game.cpp index fc0ee5fde..7cf7d8c35 100644 --- a/runtime/Game/Game.cpp +++ b/runtime/Game/Game.cpp @@ -99,9 +99,9 @@ namespace Spartan renderable->GetMaterial()->SetProperty(MaterialProperty::TextureTilingY, entity->GetScale().z); // add physics components - shared_ptr rigid_body = entity->AddComponent(); - rigid_body->SetMass(0.0f); // static - rigid_body->SetShapeType(PhysicsShape::StaticPlane); + shared_ptr physics_body = entity->AddComponent(); + physics_body->SetShapeType(PhysicsShape::StaticPlane); + physics_body->SetMass(0.0f); } void create_camera(const Vector3& camera_position = Vector3(0.0f, 2.0f, -10.0f), const Vector3& camera_rotation = Vector3(0.0f, 0.0f, 0.0f)) @@ -414,9 +414,9 @@ namespace Spartan renderable->SetMaterial(material); // add physics components - shared_ptr rigid_body = entity->AddComponent(); - rigid_body->SetMass(15.0f); - rigid_body->SetShapeType(PhysicsShape::Box); + shared_ptr physics_body = entity->AddComponent(); + physics_body->SetShapeType(PhysicsShape::Box); + physics_body->SetMass(15.0f); } // flight helmet @@ -437,6 +437,7 @@ namespace Spartan entity->SetScale(Vector3(0.3f, 0.3f, 0.3f)); PhysicsBody* physics_body = entity->AddComponent().get(); + physics_body->SetShapeType(PhysicsShape::MeshConvexHull); physics_body->SetMass(8.0f); } @@ -451,6 +452,7 @@ namespace Spartan if (auto mesh_entity = entity->GetDescendantByName("Object_2")) { PhysicsBody* physics_body = mesh_entity->AddComponent().get(); + physics_body->SetShapeType(PhysicsShape::MeshConvexHull); physics_body->SetMass(8.0f); } } @@ -562,8 +564,9 @@ namespace Spartan // add water and vegetation { // add physics so we can walk on it - PhysicsBody* rigid_body = m_default_terrain->AddComponent().get(); - rigid_body->SetMass(0.0f); + PhysicsBody* physics_body = m_default_terrain->AddComponent().get(); + physics_body->SetShapeType(PhysicsShape::Terrain); + physics_body->SetMass(0.0f); // water { diff --git a/runtime/World/Components/PhysicsBody.cpp b/runtime/World/Components/PhysicsBody.cpp index 8d7cce4f1..2e7a08ac7 100644 --- a/runtime/World/Components/PhysicsBody.cpp +++ b/runtime/World/Components/PhysicsBody.cpp @@ -179,16 +179,6 @@ namespace Spartan SP_REGISTER_ATTRIBUTE_VALUE_VALUE(m_center_of_mass, Vector3); SP_REGISTER_ATTRIBUTE_VALUE_VALUE(m_size, Vector3); SP_REGISTER_ATTRIBUTE_VALUE_SET(m_shape_type, SetShapeType, PhysicsShape); - - if (GetEntity()->GetComponent()) - { - m_shape_type = PhysicsShape::MeshConvexHull; - } - - if (GetEntity()->GetComponent()) - { - m_shape_type = PhysicsShape::Terrain; - } } PhysicsBody::~PhysicsBody() @@ -199,7 +189,6 @@ namespace Spartan void PhysicsBody::OnInitialize() { Component::OnInitialize(); - UpdateShape(); } void PhysicsBody::OnRemove() @@ -582,7 +571,11 @@ namespace Spartan void PhysicsBody::AddBodyToWorld() { - SP_ASSERT(shape != nullptr); + if (!shape) + { + SP_LOG_WARNING("To modify the physics body of \"%s\", you need to first call SetShapeType()", GetEntity()->GetObjectName().c_str()); + return; + } // compute local inertia so that we can transfer it to the new body btVector3 inertia = btVector3(0, 0, 0); @@ -728,15 +721,6 @@ namespace Spartan if (m_shape_type == type) return; - if (type == PhysicsShape::Terrain) - { - if (!m_entity_ptr->GetComponent()) - { - SP_LOG_WARNING("Can't set terrain shape as there is no terrain component"); - return; - } - } - m_shape_type = type; UpdateShape(); } diff --git a/runtime/World/Components/PhysicsBody.h b/runtime/World/Components/PhysicsBody.h index 726d7d74a..12c39f8fc 100644 --- a/runtime/World/Components/PhysicsBody.h +++ b/runtime/World/Components/PhysicsBody.h @@ -57,7 +57,8 @@ namespace Spartan Cone, Terrain, MeshConvexHull, - Mesh + Mesh, + Max }; class PhysicsBody : public Component @@ -177,7 +178,7 @@ namespace Spartan Math::Vector3 m_rotation_lock = Math::Vector3::Zero; Math::Vector3 m_center_of_mass = Math::Vector3::Zero; Math::Vector3 m_size = Math::Vector3::One; - PhysicsShape m_shape_type = PhysicsShape::Box; + PhysicsShape m_shape_type = PhysicsShape::Max; PhysicsBodyType m_body_type = PhysicsBodyType::RigidBody; uint32_t terrain_width = 0; uint32_t terrain_length = 0;