]> git.tdb.fi Git - libs/game.git/commitdiff
Refactor handling of pending physics bodies
authorMikko Rasa <tdb@tdb.fi>
Sun, 13 Apr 2025 10:43:50 +0000 (13:43 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 14 Apr 2025 10:17:42 +0000 (13:17 +0300)
This avoids the need to keep the list of pending bodies sorted.

source/game/physicssystem.cpp
source/game/physicssystem.h

index 554e54d9c65aae59ff96025f5f4935aaac7ccc6e..cea1440ccf510d16ae636df52e564229521d35c2 100644 (file)
@@ -49,13 +49,12 @@ RaycastHit PhysicsSystem::cast_ray(const Geometry::Ray<float, 3> &ray)
 
 void PhysicsSystem::simulated_rigid_body_changed(SimulatedRigidBody &body)
 {
-       if(!body.physics_body)
+       if(body.body && body.shape && !body.physics_body && !body.pending_create)
        {
-               auto i = lower_bound(pending, body.entity);
-               if(i==pending.end() || *i!=body.entity)
-                       pending.insert(i, body.entity);
+               pending.push_back(body.entity);
+               body.pending_create = true;
        }
-       else if(!body.body)
+       else if(body.physics_body && !body.body)
        {
                auto i = lower_bound_member(collider_lookup, body.physics_body.get(), &Collider::physics_body);
                if(i!=collider_lookup.end() && i->physics_body==body.physics_body.get())
@@ -68,6 +67,7 @@ void PhysicsSystem::early_tick()
 {
        for(Handle<Entity> e: pending)
                if(SimulatedRigidBody *srb = lookup_member(rigid_bodies, e, &SimulatedRigidBody::entity))
+               {
                        if(srb->body && srb->shape && !srb->physics_body)
                        {
                                Handle<Transform> transform = srb->entity->get_transform();
@@ -83,6 +83,9 @@ void PhysicsSystem::early_tick()
                                auto j = lower_bound_member(collider_lookup, srb->physics_body.get(), &Collider::physics_body);
                                collider_lookup.emplace(j, srb->physics_body.get(), srb->shape);
                        }
+
+                       srb->pending_create = false;
+               }
        pending.clear();
 }
 
index e56b3541ffcd7d1dfa5674834ba97074dbbaeb6c..2365eaa872b499cf54e2639cb46d36924561569a 100644 (file)
@@ -36,6 +36,7 @@ private:
                uint8_t transform_generation = 0;
                uint8_t motion_generation = 0;
                uint8_t body_generation = 0;
+               bool pending_create = false;
 
                using Components = ArchetypeComponents<&SimulatedRigidBody::body, &SimulatedRigidBody::shape, &SimulatedRigidBody::motion>;