Skip to content

Commit

Permalink
feat(collisions): compute capsule aabbs
Browse files Browse the repository at this point in the history
  • Loading branch information
luishfonseca committed Oct 5, 2023
1 parent c547dce commit d05c425
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
12 changes: 7 additions & 5 deletions engine/samples/collisions/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cubos/engine/collisions/collider.hpp>
#include <cubos/engine/collisions/plugin.hpp>
#include <cubos/engine/collisions/shapes/box.hpp>
#include <cubos/engine/collisions/shapes/capsule.hpp>
#include <cubos/engine/input/plugin.hpp>
#include <cubos/engine/renderer/plugin.hpp>
#include <cubos/engine/settings/settings.hpp>
Expand Down Expand Up @@ -61,7 +62,7 @@ static void addColliders(Write<State> state, Commands commands)
{
state->a = commands.create()
.add(Collider{})
.add(BoxCollisionShape{})
.add(CapsuleCollisionShape{{0.5F, 1.0F}})
.add(LocalToWorld{})
.add(Position{glm::vec3{0.0F, 0.0F, -2.0F}})
.add(Rotation{})
Expand Down Expand Up @@ -111,7 +112,8 @@ static void updateCollided(Query<Read<Collider>> query, Write<State> state, Read
{
for (auto [entity, collider] : query)
{
for (const auto& [collider1, collider2] : collisions->candidates(BroadPhaseCollisions::CollisionType::BoxBox))
for (const auto& [collider1, collider2] :
collisions->candidates(BroadPhaseCollisions::CollisionType::BoxCapsule))
{
if (collider1 == entity || collider2 == entity)
{
Expand All @@ -122,11 +124,11 @@ static void updateCollided(Query<Read<Collider>> query, Write<State> state, Read
}
}

static void render(Query<Read<LocalToWorld>, Read<Collider>, Read<BoxCollisionShape>> query)
static void render(Query<Read<LocalToWorld>, Read<Collider>> query)
{
for (auto [entity, localToWorld, collider, shape] : query)
for (auto [entity, localToWorld, collider] : query)
{
cubos::core::gl::Debug::drawWireBox(shape->box, localToWorld->mat * collider->transform);
cubos::core::gl::Debug::drawWireBox(collider->localAABB.box(), localToWorld->mat * collider->transform);
cubos::core::gl::Debug::drawWireBox(collider->worldAABB.box(),
glm::translate(glm::mat4{1.0}, collider->worldAABB.center()),
glm::vec3{1.0, 0.0, 0.0});
Expand Down
15 changes: 13 additions & 2 deletions engine/src/cubos/engine/collisions/broad_phase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,19 @@ void setupNewBoxes(Query<Read<BoxCollisionShape>, Write<Collider>> query, Write<

void setupNewCapsules(Query<Read<CapsuleCollisionShape>, Write<Collider>> query, Write<BroadPhaseCollisions> collisions)
{
(void)query;
(void)collisions;
for (auto [entity, shape, collider] : query)
{
if (collider->fresh)
{
collisions->addEntity(entity);

collider->localAABB = shape->capsule.aabb();

collider->margin = 0.0F;

collider->fresh = false;
}
}
}

void updateAABBs(Query<Read<LocalToWorld>, Write<Collider>> query)
Expand Down

0 comments on commit d05c425

Please sign in to comment.