Skip to content

Commit

Permalink
refactor(collision): remove trackNewEntities
Browse files Browse the repository at this point in the history
  • Loading branch information
luishfonseca committed Oct 4, 2023
1 parent 4f7e5a2 commit e10a607
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 34 deletions.
5 changes: 3 additions & 2 deletions engine/include/cubos/engine/collisions/collider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion engine/samples/collisions/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ static void init(Commands commands, Write<Input> input, Write<ActiveCameras> cam
static void addColliders(Write<State> state, Commands commands)
{
state->a = commands.create()
.add(Collider{})
.add(BoxCollisionShape{})
.add(LocalToWorld{})
.add(Position{glm::vec3{0.0F, 0.0F, -2.0F}})
Expand All @@ -69,6 +70,7 @@ static void addColliders(Write<State> 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}})
Expand Down Expand Up @@ -114,7 +116,6 @@ static void updateCollided(Query<Read<Collider>> query, Write<State> state, Read
{
if (collider1 == entity || collider2 == entity)
{
CUBOS_WARN("Collision detected!");
state->collided = true;
break;
}
Expand Down
10 changes: 8 additions & 2 deletions engine/src/cubos/engine/collisions/broad_phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@ using cubos::engine::ColliderAABB;

using CollisionType = BroadPhaseCollisions::CollisionType;

void setupBoxAABBs(Query<Read<BoxCollisionShape>, Write<Collider>> query)
void setupNewBoxes(Query<Read<BoxCollisionShape>, Write<Collider>> query, Write<BroadPhaseCollisions> 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<Read<CapsuleCollisionShape>, Write<Collider>> query)
void setupNewCapsules(Query<Read<CapsuleCollisionShape>, Write<Collider>> query, Write<BroadPhaseCollisions> collisions)
{
(void)query;
(void)collisions;
}

void updateAABBs(Query<Read<LocalToWorld>, Write<Collider>> query)
Expand Down
28 changes: 6 additions & 22 deletions engine/src/cubos/engine/collisions/broad_phase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename S>
void trackNewEntities(Query<Read<S>, OptRead<Collider>> query, Write<BroadPhaseCollisions> 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<Read<BoxCollisionShape>, Write<Collider>> query, Write<BroadPhaseCollisions> 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<Read<BoxCollisionShape>, Write<Collider>> query);

/// @brief Setups the AABBs of all capsule colliders.
void setupCapsuleAABBs(Query<Read<CapsuleCollisionShape>, Write<Collider>> query);
/// @brief Setups new capsule colliders.
void setupNewCapsules(Query<Read<CapsuleCollisionShape>, Write<Collider>> query,
Write<BroadPhaseCollisions> collisions);

/// @brief Updates the AABBs of all colliders.
void updateAABBs(Query<Read<LocalToWorld>, Write<Collider>> query);
Expand All @@ -61,4 +45,4 @@ void sweep(Write<BroadPhaseCollisions> 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<BoxCollisionShape>, OptRead<CapsuleCollisionShape>, Read<Collider>> query,
Write<BroadPhaseCollisions> collisions);
Write<BroadPhaseCollisions> collisions);
10 changes: 3 additions & 7 deletions engine/src/cubos/engine/collisions/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ void cubos::engine::collisionsPlugin(Cubos& cubos)
cubos.addComponent<BoxCollisionShape>();
cubos.addComponent<CapsuleCollisionShape>();

cubos.system(trackNewEntities<BoxCollisionShape>).tagged("cubos.collisions.missing");
cubos.system(trackNewEntities<CapsuleCollisionShape>).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");

Expand Down

0 comments on commit e10a607

Please sign in to comment.