Skip to content

Commit

Permalink
[physicsbody] walking down the hierarchy to create compound shape can…
Browse files Browse the repository at this point in the history
… now be toggled when setting the shape typ
  • Loading branch information
PanosK92 committed Jan 15, 2025
1 parent 56adfd9 commit d79f96e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
47 changes: 19 additions & 28 deletions runtime/Game/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ namespace spartan

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

// damaged helmet
Expand Down Expand Up @@ -815,7 +815,7 @@ namespace spartan
entity->SetScale(Vector3(0.1f, 0.1f, 0.1f));

PhysicsBody* physics_body = entity->AddComponent<PhysicsBody>().get();
physics_body->SetShapeType(PhysicsShape::Mesh);
physics_body->SetShapeType(PhysicsShape::Mesh, true);
}
}

Expand Down Expand Up @@ -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<Entity*> entities;
entity->GetDescendants(&entities);
for (Entity* entity : entities)
{
if (entity->GetComponent<Renderable>() != nullptr)
{
PhysicsBody* physics_body = entity->AddComponent<PhysicsBody>().get();
physics_body->SetShapeType(PhysicsShape::Mesh);
}
}
PhysicsBody* physics_body = entity->AddComponent<PhysicsBody>().get();
physics_body->SetShapeType(PhysicsShape::Mesh, true);
}
}

Expand All @@ -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<Entity*> entities;
entity->GetDescendants(&entities);
for (Entity* entity : entities)
{
if (entity->GetComponent<Renderable>() != nullptr)
{
PhysicsBody* physics_body = entity->AddComponent<PhysicsBody>().get();
physics_body->SetShapeType(PhysicsShape::Mesh);
}
}

// make the radiator metallic
if (shared_ptr<Renderable> renderable = entity->GetDescendantByName("Mesh_93")->GetComponent<Renderable>())
{
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -1041,6 +1020,18 @@ namespace spartan
}
}
}

// enable physics for all meshes
vector<Entity*> entities;
entity->GetDescendants(&entities);
for (Entity* entity : entities)
{
if (entity->GetComponent<Renderable>() != nullptr)
{
PhysicsBody* physics_body = entity->AddComponent<PhysicsBody>().get();
physics_body->SetShapeType(PhysicsShape::Mesh);
}
}
}
}

Expand Down
21 changes: 13 additions & 8 deletions runtime/World/Components/PhysicsBody.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -871,7 +873,7 @@ namespace spartan

case PhysicsShape::Mesh:
{
function<void(Entity*, btCompoundShape*, bool)> recursive_renderable_to_shape = [&](Entity* entity, btCompoundShape* shape_compount, const bool is_root_entity)
function<void(Entity*, btCompoundShape*, bool, bool)> recursive_renderable_to_shape = [&](Entity* entity, btCompoundShape* shape_compount, const bool is_root_entity, const bool replicate_hierarchy)
{
// get renderable
shared_ptr<Renderable> renderable = entity->GetComponent<Renderable>();
Expand Down Expand Up @@ -946,16 +948,19 @@ namespace spartan
}

// recursively process all children
vector<Entity*> children = entity->GetChildren();
for (Entity* child : children)
{
recursive_renderable_to_shape(child, shape_compount, false);
if (replicate_hierarchy)
{
vector<Entity*> 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;

Expand Down
3 changes: 2 additions & 1 deletion runtime/World/Components/PhysicsBody.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -190,6 +190,7 @@ namespace spartan
void* m_shape = nullptr;
void* m_rigid_body = nullptr;
std::shared_ptr<Car> m_car = nullptr;
bool m_replicate_hierarchy = false;
std::vector<PhysicsBodyMeshData> m_mesh_data;
std::vector<Constraint*> m_constraints;
};
Expand Down

0 comments on commit d79f96e

Please sign in to comment.