]> git.tdb.fi Git - libs/game.git/commitdiff
Set physics bodies on different layers based on whether they move
authorMikko Rasa <tdb@tdb.fi>
Sun, 30 Mar 2025 12:07:16 +0000 (15:07 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 30 Mar 2025 12:07:16 +0000 (15:07 +0300)
This should improve performance when there's a lot of static bodies
compared to moving ones.

source/game/physicssystem.cpp

index 9d5fc4705a345f7892ab6f83921e2057b30d9d39..621b9d1b74c6fbeff269076ef934ede4500ad7d1 100644 (file)
@@ -10,6 +10,17 @@ using namespace std;
 
 namespace Msp::Game {
 
+namespace CollisionLayer {
+
+enum
+{
+       STATIC,
+       MOVING
+};
+
+} // namespace CollisionLayer
+
+
 PhysicsSystem::PhysicsSystem(Stage &s):
        System(s),
        event_observer(stage.get_event_bus()),
@@ -45,6 +56,9 @@ void PhysicsSystem::early_tick()
                        Handle<Transform> transform = i->entity->get_transform();
                        Physics::MotionType motion = (i->motion ? Physics::DYNAMIC : Physics::STATIC);
                        i->physics_body = make_unique<Physics::RigidBody>(world, i->shape->get_shape(), transform->get_position(), transform->get_rotation(), motion);
+                       unsigned layer = (motion==Physics::STATIC ? CollisionLayer::STATIC : CollisionLayer::MOVING);
+                       uint32_t collision_mask = (1<<CollisionLayer::STATIC) | (1<<CollisionLayer::MOVING);
+                       i->physics_body->set_collisions(layer, collision_mask);
                }
        }
        pending.clear();