diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index f920e8db..32a72a8b 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -40,6 +40,11 @@ namespace BBM : WAL::Component(entity), onCollide(), _bound({boundSize, boundSize, boundSize}) { } + Vector3f CollisionComponent::getBounds(void) const + { + return _bound; + } + float CollisionComponent::getBoundX(void) const { return _bound.x; diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 54f7bfa4..1e4156e4 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -17,6 +17,8 @@ namespace BBM //! @brief Bound size on all axis Vector3f _bound; public: + //! @brief get vector of bounds + Vector3f getBounds(void) const; //! @brief get bound size on the X axis float getBoundX(void) const; //! @brief get bound size on the Y axis diff --git a/sources/Component/Movable/MovableComponent.hpp b/sources/Component/Movable/MovableComponent.hpp index 7656a960..ba04f804 100644 --- a/sources/Component/Movable/MovableComponent.hpp +++ b/sources/Component/Movable/MovableComponent.hpp @@ -35,5 +35,6 @@ namespace BBM MovableComponent &operator=(const MovableComponent &) = delete; friend class MovableSystem; + friend class CollisionSystem; }; } // namespace WAL \ No newline at end of file diff --git a/sources/Models/Vector3.hpp b/sources/Models/Vector3.hpp index 5579b382..5d77dbe4 100644 --- a/sources/Models/Vector3.hpp +++ b/sources/Models/Vector3.hpp @@ -158,6 +158,22 @@ namespace BBM { return RAY::Vector3(this->x, this->y, this->z); } + + static Vector3 min(Vector3 a, Vector3 b) + { + Vector3 min = { std::min(a.x, b.x), + std::min(a.y, b.y), + std::min(a.z, b.z)}; + return min; + } + + static Vector3 max(Vector3 a, Vector3 b) + { + Vector3 max = { std::max(a.x, b.x), + std::max(a.y, b.y), + std::max(a.z, b.z)}; + return max; + } }; typedef Vector3 Vector3f; diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 7c85195a..6c644555 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -2,6 +2,7 @@ // Created by Louis Auzuret on 5/20/21 // +#include "Component/Movable/MovableComponent.hpp" #include "Component/Position/PositionComponent.hpp" #include "Component/Collision/CollisionComponent.hpp" #include "System/Collision/CollisionSystem.hpp" @@ -15,33 +16,29 @@ namespace BBM } void CollisionSystem::onFixedUpdate(WAL::Entity &entity) { + auto &posA = entity.getComponent(); + auto &col = entity.getComponent(); + Vector3f position = posA.position; try { - auto &posA = entity.getComponent(); - auto &col = entity.getComponent(); - Vector3f minA = { std::min(posA.getX(), posA.getX() + col.getBoundX()), - std::min(posA.getY(), posA.getY() + col.getBoundY()), - std::min(posA.getZ(), posA.getZ() + col.getBoundZ())}; - Vector3f maxA = { std::max(posA.getX(), posA.getX() + col.getBoundX()), - std::max(posA.getY(), posA.getY() + col.getBoundY()), - std::max(posA.getZ(), posA.getZ() + col.getBoundZ())}; - for (auto &other : _wal.scene->getEntities()) { - if (&other == &entity) - continue; - auto &colB = entity.getComponent(); - auto &posB = other.getComponent(); - Vector3f minB = { std::min(posB.getX(), posB.getX() + colB.getBoundX()), - std::min(posB.getY(), posB.getY() + colB.getBoundY()), - std::min(posB.getZ(), posB.getZ() + colB.getBoundZ())}; - Vector3f maxB = { std::max(posB.getX(), posB.getX() + colB.getBoundX()), - std::max(posB.getY(), posB.getY() + colB.getBoundY()), - std::max(posB.getZ(), posB.getZ() + colB.getBoundZ())}; - if ((minA.x <= maxB.x && maxA.x >= minB.x) && - (minA.y <= maxB.y && maxA.y >= minB.y) && - (minA.z <= maxB.z && maxA.z >= minB.z)) - col.onCollide(entity, other); - } - } catch (std::exception &e) { - return; + auto &movable = entity.getComponent(); + position += movable._velocity; + } catch (std::exception &e) { }; + Vector3f minA = Vector3f::min(position, position + col.getBounds()); + Vector3f maxA = Vector3f::max(position, position + col.getBounds()); + for (auto &other : _wal.scene->getEntities()) { + if (&other == &entity) + continue; + if (!other.hasComponent(typeid(CollisionComponent)) || + !other.hasComponent(typeid(PositionComponent))) + continue; + auto colB = entity.getComponent().getBounds(); + auto posB = other.getComponent().position; + Vector3f minB = Vector3f::min(posB, posB + colB); + Vector3f maxB = Vector3f::max(posB, posB + colB); + if ((minA.x <= maxB.x && maxA.x >= minB.x) && + (minA.y <= maxB.y && maxA.y >= minB.y) && + (minA.z <= maxB.z && maxA.z >= minB.z)) + col.onCollide(entity, other); } } } \ No newline at end of file