Skip to content

Commit

Permalink
[physicsbody] reduced the number of rigid bodies that would be create…
Browse files Browse the repository at this point in the history
…d when initializing a physics body (due to property changes requiring rigid body recreation)
  • Loading branch information
PanosK92 committed Jan 7, 2025
1 parent 079848c commit c2dcd3a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 31 deletions.
19 changes: 11 additions & 8 deletions runtime/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ namespace Spartan
renderable->GetMaterial()->SetProperty(MaterialProperty::TextureTilingY, entity->GetScale().z);

// add physics components
shared_ptr<PhysicsBody> rigid_body = entity->AddComponent<PhysicsBody>();
rigid_body->SetMass(0.0f); // static
rigid_body->SetShapeType(PhysicsShape::StaticPlane);
shared_ptr<PhysicsBody> physics_body = entity->AddComponent<PhysicsBody>();
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))
Expand Down Expand Up @@ -414,9 +414,9 @@ namespace Spartan
renderable->SetMaterial(material);

// add physics components
shared_ptr<PhysicsBody> rigid_body = entity->AddComponent<PhysicsBody>();
rigid_body->SetMass(15.0f);
rigid_body->SetShapeType(PhysicsShape::Box);
shared_ptr<PhysicsBody> physics_body = entity->AddComponent<PhysicsBody>();
physics_body->SetShapeType(PhysicsShape::Box);
physics_body->SetMass(15.0f);
}

// flight helmet
Expand All @@ -437,6 +437,7 @@ namespace Spartan
entity->SetScale(Vector3(0.3f, 0.3f, 0.3f));

PhysicsBody* physics_body = entity->AddComponent<PhysicsBody>().get();
physics_body->SetShapeType(PhysicsShape::MeshConvexHull);
physics_body->SetMass(8.0f);
}

Expand All @@ -451,6 +452,7 @@ namespace Spartan
if (auto mesh_entity = entity->GetDescendantByName("Object_2"))
{
PhysicsBody* physics_body = mesh_entity->AddComponent<PhysicsBody>().get();
physics_body->SetShapeType(PhysicsShape::MeshConvexHull);
physics_body->SetMass(8.0f);
}
}
Expand Down Expand Up @@ -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<PhysicsBody>().get();
rigid_body->SetMass(0.0f);
PhysicsBody* physics_body = m_default_terrain->AddComponent<PhysicsBody>().get();
physics_body->SetShapeType(PhysicsShape::Terrain);
physics_body->SetMass(0.0f);

// water
{
Expand Down
26 changes: 5 additions & 21 deletions runtime/World/Components/PhysicsBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Renderable>())
{
m_shape_type = PhysicsShape::MeshConvexHull;
}

if (GetEntity()->GetComponent<Terrain>())
{
m_shape_type = PhysicsShape::Terrain;
}
}

PhysicsBody::~PhysicsBody()
Expand All @@ -199,7 +189,6 @@ namespace Spartan
void PhysicsBody::OnInitialize()
{
Component::OnInitialize();
UpdateShape();
}

void PhysicsBody::OnRemove()
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -728,15 +721,6 @@ namespace Spartan
if (m_shape_type == type)
return;

if (type == PhysicsShape::Terrain)
{
if (!m_entity_ptr->GetComponent<Terrain>())
{
SP_LOG_WARNING("Can't set terrain shape as there is no terrain component");
return;
}
}

m_shape_type = type;
UpdateShape();
}
Expand Down
5 changes: 3 additions & 2 deletions runtime/World/Components/PhysicsBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ namespace Spartan
Cone,
Terrain,
MeshConvexHull,
Mesh
Mesh,
Max
};

class PhysicsBody : public Component
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit c2dcd3a

Please sign in to comment.