From e10a607f8d3cd4df0c0e46f475e31f95fa577ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Fonseca?= Date: Wed, 4 Oct 2023 22:51:41 +0100 Subject: [PATCH] refactor(collision): remove trackNewEntities --- .../cubos/engine/collisions/collider.hpp | 5 ++-- engine/samples/collisions/main.cpp | 3 +- .../cubos/engine/collisions/broad_phase.cpp | 10 +++++-- .../cubos/engine/collisions/broad_phase.hpp | 28 ++++--------------- engine/src/cubos/engine/collisions/plugin.cpp | 10 ++----- 5 files changed, 22 insertions(+), 34 deletions(-) diff --git a/engine/include/cubos/engine/collisions/collider.hpp b/engine/include/cubos/engine/collisions/collider.hpp index 8d3d7f30c8..c16ad9e6f2 100644 --- a/engine/include/cubos/engine/collisions/collider.hpp +++ b/engine/include/cubos/engine/collisions/collider.hpp @@ -16,13 +16,14 @@ namespace cubos::engine { glm::mat4 transform{1.0F}; ///< Transform of the collider. - bool fresh = true; ///< Whether the collider is fresh. This is an hack and should be done in ECS. ColliderAABB localAABB; ///< Local space AABB of the collider. ColliderAABB worldAABB; ///< World space AABB of the collider. /// @brief Margin of the collider. /// /// When the collider shape has sharp edges, a margin is needed. - float margin = 0.0F; + float margin; + + bool fresh = true; ///< Whether the collider is fresh. This is an hack and should be done in ECS. }; } // namespace cubos::engine diff --git a/engine/samples/collisions/main.cpp b/engine/samples/collisions/main.cpp index 94c07e02b1..459442276b 100644 --- a/engine/samples/collisions/main.cpp +++ b/engine/samples/collisions/main.cpp @@ -61,6 +61,7 @@ static void init(Commands commands, Write input, Write cam static void addColliders(Write state, Commands commands) { state->a = commands.create() + .add(Collider{}) .add(BoxCollisionShape{}) .add(LocalToWorld{}) .add(Position{glm::vec3{0.0F, 0.0F, -2.0F}}) @@ -69,6 +70,7 @@ static void addColliders(Write state, Commands commands) state->aRotationAxis = glm::sphericalRand(1.0F); state->b = commands.create() + .add(Collider{}) .add(BoxCollisionShape{}) .add(LocalToWorld{}) .add(Position{glm::vec3{0.0F, 0.0F, 2.0F}}) @@ -114,7 +116,6 @@ static void updateCollided(Query> query, Write state, Read { if (collider1 == entity || collider2 == entity) { - CUBOS_WARN("Collision detected!"); state->collided = true; break; } diff --git a/engine/src/cubos/engine/collisions/broad_phase.cpp b/engine/src/cubos/engine/collisions/broad_phase.cpp index 811fa6b509..e8d959fe47 100644 --- a/engine/src/cubos/engine/collisions/broad_phase.cpp +++ b/engine/src/cubos/engine/collisions/broad_phase.cpp @@ -4,23 +4,29 @@ using cubos::engine::ColliderAABB; using CollisionType = BroadPhaseCollisions::CollisionType; -void setupBoxAABBs(Query, Write> query) +void setupNewBoxes(Query, Write> query, Write collisions) { for (auto [entity, shape, collider] : query) { if (collider->fresh) { + collisions->addEntity(entity); + glm::vec3 diag[2]; shape->shape.diag(diag); collider->localAABB = ColliderAABB{diag[0], diag[1]}; + + collider->margin = 0.04F; + collider->fresh = false; } } } -void setupCapsuleAABBs(Query, Write> query) +void setupNewCapsules(Query, Write> query, Write collisions) { (void)query; + (void)collisions; } void updateAABBs(Query, Write> query) diff --git a/engine/src/cubos/engine/collisions/broad_phase.hpp b/engine/src/cubos/engine/collisions/broad_phase.hpp index 6596b6d1c8..51b8789b65 100644 --- a/engine/src/cubos/engine/collisions/broad_phase.hpp +++ b/engine/src/cubos/engine/collisions/broad_phase.hpp @@ -23,28 +23,12 @@ using cubos::engine::CapsuleCollisionShape; using cubos::engine::Collider; using cubos::engine::LocalToWorld; -/// @brief Adds collider to all new entities with collision shape. -template -void trackNewEntities(Query, OptRead> query, Write collisions, - Commands commands) -{ - // TODO: This is a bit of a hack. We should detect newly added colliders instead of using them as a marker. +/// @brief Setups new box colliders. +void setupNewBoxes(Query, Write> query, Write collisions); - for (auto [entity, shape, collider] : query) - { - if (!collider) - { - commands.add(entity, Collider{}); - collisions->addEntity(entity); - } - } -} - -/// @brief Setups the AABBs of all box colliders. -void setupBoxAABBs(Query, Write> query); - -/// @brief Setups the AABBs of all capsule colliders. -void setupCapsuleAABBs(Query, Write> query); +/// @brief Setups new capsule colliders. +void setupNewCapsules(Query, Write> query, + Write collisions); /// @brief Updates the AABBs of all colliders. void updateAABBs(Query, Write> query); @@ -61,4 +45,4 @@ void sweep(Write collisions); /// TODO: This query is disgusting. We need a way to find if a component is present without reading it. /// Maybe something like Commands but for reads? void findPairs(Query, OptRead, Read> query, - Write collisions); \ No newline at end of file + Write collisions); diff --git a/engine/src/cubos/engine/collisions/plugin.cpp b/engine/src/cubos/engine/collisions/plugin.cpp index d483bf409b..2fda8b64c0 100644 --- a/engine/src/cubos/engine/collisions/plugin.cpp +++ b/engine/src/cubos/engine/collisions/plugin.cpp @@ -13,14 +13,10 @@ void cubos::engine::collisionsPlugin(Cubos& cubos) cubos.addComponent(); cubos.addComponent(); - cubos.system(trackNewEntities).tagged("cubos.collisions.missing"); - cubos.system(trackNewEntities).tagged("cubos.collisions.missing"); - cubos.tag("cubos.collisions.missing").before("cubos.collisions.aabb.setup"); - - cubos.system(setupBoxAABBs).tagged("cubos.collisions.aabb.setup"); - cubos.system(setupCapsuleAABBs).tagged("cubos.collisions.aabb.setup"); + cubos.system(setupNewBoxes).tagged("cubos.collisions.setup"); + cubos.system(setupNewCapsules).tagged("cubos.collisions.setup"); cubos.system(updateAABBs) - .after("cubos.collisions.aabb.setup") + .after("cubos.collisions.setup") .after("cubos.transform.update") .before("cubos.collisions.broad.markers");