Skip to content

Commit

Permalink
global enable, group add/remove for physics
Browse files Browse the repository at this point in the history
  • Loading branch information
crocdialer committed Nov 15, 2024
1 parent 87783dc commit 942306e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
2 changes: 2 additions & 0 deletions include/vierkant/Object3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class Object3D : public std::enable_shared_from_this<Object3D>

void set_global_transform(const vierkant::transform_t &t);

bool global_enable() const;

/**
* @return the axis-aligned boundingbox (AABB) in object coords.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Object3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ void Object3D::set_global_transform(const vierkant::transform_t &t)
transform = parent_inverse * t;
}

bool Object3D::global_enable() const
{
if(!enabled) { return false; }
Object3DPtr ancestor = parent();
while(ancestor)
{
if(!ancestor->enabled) { return false; }
ancestor = ancestor->parent();
}
return true;
}

void Object3D::set_parent(const Object3DPtr &parent_object)
{
// detach object from former parent
Expand Down
34 changes: 23 additions & 11 deletions src/physics_context.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <unordered_set>

#include <crocore/ThreadPool.hpp>
#include <vierkant/Visitor.hpp>
#include <vierkant/physics_context.hpp>

// The Jolt headers don't include Jolt.h. Always include Jolt.h before including any other Jolt header.
Expand Down Expand Up @@ -925,10 +926,7 @@ CollisionShapeId PhysicsContext::create_collision_shape(const vierkant::collisio
}
}
}
if(new_id)
{
m_engine->jolt.shape_ids[s] = {new_id, 1};
}
if(new_id) { m_engine->jolt.shape_ids[s] = {new_id, 1}; }
return new_id;
},
shape);
Expand Down Expand Up @@ -959,17 +957,29 @@ void PhysicsScene::add_object(const Object3DPtr &object)

if(object)
{
auto phy_cmp_ptr = object->get_component_ptr<vierkant::physics_component_t>();
if(phy_cmp_ptr) { m_context.add_object(object->id(), object->transform, *phy_cmp_ptr); }
vierkant::LambdaVisitor visitor;
visitor.traverse(*object, [this](const auto &obj) -> bool {
if(auto phy_cmp_ptr = obj.template get_component_ptr<vierkant::physics_component_t>())
{
m_context.add_object(obj.id(), obj.global_transform(), *phy_cmp_ptr);
}
return true;
});
}
}

void PhysicsScene::remove_object(const Object3DPtr &object)
{
if(object)
{
auto phy_cmp_ptr = object->get_component_ptr<vierkant::physics_component_t>();
if(phy_cmp_ptr) { m_context.remove_object(object->id(), *phy_cmp_ptr); }
vierkant::LambdaVisitor visitor;
visitor.traverse(*object, [this](const auto &obj) -> bool {
if(auto phy_cmp_ptr = obj.template get_component_ptr<vierkant::physics_component_t>())
{
m_context.remove_object(obj.id(), *phy_cmp_ptr);
}
return true;
});
}
vierkant::Scene::remove_object(object);
}
Expand All @@ -987,6 +997,8 @@ void PhysicsScene::update(double time_delta)
for(const auto &[entity, cmp]: view.each())
{
auto obj = object_by_id(static_cast<uint32_t>(entity));
bool obj_enabled = obj->global_enable();

if(cmp.mode == physics_component_t::UPDATE)
{
if(auto mesh_shape = std::get_if<collision::mesh_t>(&cmp.shape))
Expand All @@ -999,12 +1011,12 @@ void PhysicsScene::update(double time_delta)
m_context.add_object(obj->id(), obj->transform, cmp);
cmp.mode = physics_component_t::ACTIVE;
}
else if(obj->enabled && cmp.mode == physics_component_t::INACTIVE)
else if(obj_enabled && cmp.mode == physics_component_t::INACTIVE)
{
cmp.mode = physics_component_t::ACTIVE;
m_context.add_object(obj->id(), obj->transform, cmp);
}
else if(!obj->enabled)
else if(!obj_enabled)
{
cmp.mode = physics_component_t::INACTIVE;
m_context.remove_object(obj->id(), cmp);
Expand All @@ -1016,7 +1028,7 @@ void PhysicsScene::update(double time_delta)
continue;
}

if(cmp.kinematic)
if(cmp.kinematic || cmp.mass == 0.f)
{
// object -> physics
m_context.body_interface().set_transform(static_cast<uint32_t>(entity), obj->transform);
Expand Down

0 comments on commit 942306e

Please sign in to comment.