From 79aa4cc86c522522e6fa2a976117e5ac48c48d4f Mon Sep 17 00:00:00 2001 From: Bluub Date: Mon, 31 May 2021 13:06:03 +0200 Subject: [PATCH 01/12] bounding of box collision in the collisionComponent --- CMakeLists.txt | 4 ++ .../Collision/CollisionComponent.cpp | 64 +++++++++++++++++++ .../Collision/CollisionComponent.hpp | 62 ++++++++++++++++++ sources/System/Collision/CollisionSystem.cpp | 45 +++++++++++++ sources/System/Collision/CollisionSystem.hpp | 32 ++++++++++ 5 files changed, 207 insertions(+) create mode 100644 sources/Component/Collision/CollisionComponent.cpp create mode 100644 sources/Component/Collision/CollisionComponent.hpp create mode 100644 sources/System/Collision/CollisionSystem.cpp create mode 100644 sources/System/Collision/CollisionSystem.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 45d0247d..0c0e8f2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,10 @@ set(SOURCES sources/Component/Renderer/CameraComponent.hpp sources/System/Renderer/Render2DScreenSystem.cpp sources/System/Renderer/Render2DScreenSystem.hpp + sources/Component/Collision/CollisionComponent.cpp + sources/Component/Collision/CollisionComponent.hpp + sources/System/Collision/CollisionSystem.hpp + sources/System/Collision/CollisionSystem.cpp ) add_executable(bomberman diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp new file mode 100644 index 00000000..0f14ca27 --- /dev/null +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -0,0 +1,64 @@ +// +// Created by Louis Auzuret on 2021-05-20. +// + +#include "Component/Collision/CollisionComponent.hpp" + + +namespace BBM +{ + CollisionComponent::CollisionComponent(WAL::Entity &entity) + : WAL::Component(entity) + { } + + WAL::Component *CollisionComponent::clone(WAL::Entity &entity) const + { + return new CollisionComponent(entity); + } + + CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function callback, Vector3f bound) + : WAL::Component(entity), onCollide(callback), _boundX(bound.x), _boundY(bound.y), _boundZ(bound.z) + { } + + CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function callback, float boundSize) + : WAL::Component(entity), onCollide(callback), _boundX(boundSize), _boundY(boundSize), _boundZ(boundSize) + { } + + CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback callback, Vector3f bound) + : WAL::Component(entity), onCollide(callback), _boundX(bound.x), _boundY(bound.y), _boundZ(bound.z) + { } + + CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback callback, float boundSize) + : WAL::Component(entity), onCollide(callback), _boundX(boundSize), _boundY(boundSize), _boundZ(boundSize) + { } + + float CollisionComponent::getBoundX(void) const + { + return _boundX; + } + + float CollisionComponent::getBoundY(void) const + { + return _boundY; + } + + float CollisionComponent::getBoundZ(void) const + { + return _boundZ; + } + + void CollisionComponent::setBoundX(float value) + { + _boundX = value; + } + + void CollisionComponent::setBoundY(float value) + { + _boundY = value; + } + + void CollisionComponent::setBoundZ(float value) + { + _boundZ = value; + } +} \ No newline at end of file diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp new file mode 100644 index 00000000..64ecc243 --- /dev/null +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -0,0 +1,62 @@ +// +// Created by Louis Auzuret on 2021-05-20. +// + +#pragma once + +#include "Models/Callback.hpp" +#include "Models/Vector3.hpp" +#include "Component/Component.hpp" +#include "Entity/Entity.hpp" + +namespace BBM +{ + class CollisionComponent : public WAL::Component + { + private: + float _boundX; + float _boundY; + float _boundZ; + public: + //! @brief get bound size on the X axis + float getBoundX(void) const; + //! @brief get bound size on the Y axis + float getBoundY(void) const; + //! @brief get bound size on the Z axis + float getBoundZ(void) const; + //! @brief set bound size on the X axis + void setBoundX(float); + //! @brief set bound size on the Y axis + void setBoundY(float); + //! @brief set bound size on the Z axis + void setBoundZ(float); + //onCollide functions to be called + WAL::Callback onCollide; + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief A component can't be instantiated, it should be derived. + explicit CollisionComponent(WAL::Entity &entity); + + //! @brief Constructor with a callback function + CollisionComponent(WAL::Entity &entity, std::function callback, Vector3f bound); + + //! @brief Constructor with a callback function + CollisionComponent(WAL::Entity &entity, std::function callback, float boundSize = 0); + + //! @brief Constructor with a WAL::Callback + CollisionComponent(WAL::Entity &entity, WAL::Callback callback, Vector3f bound); + + //! @brief Constructor with a WAL::Callback + CollisionComponent(WAL::Entity &entity, WAL::Callback callback, float boundSize = 0); + + //! @brief A component can't be instantiated, it should be derived. + CollisionComponent(const CollisionComponent &) = default; + + //! @brief default destructor + ~CollisionComponent() override = default; + + //! @brief A component can't be assigned + CollisionComponent &operator=(const CollisionComponent &) = delete; + }; +} \ No newline at end of file diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp new file mode 100644 index 00000000..a13d4648 --- /dev/null +++ b/sources/System/Collision/CollisionSystem.cpp @@ -0,0 +1,45 @@ +// +// Created by Louis Auzuret on 5/20/21 +// + +#include "Component/Position/PositionComponent.hpp" +#include "Component/Collision/CollisionComponent.hpp" +#include "System/Collision/CollisionSystem.hpp" + +namespace BBM +{ + CollisionSystem::CollisionSystem(WAL::Wal &wal) + : WAL::System({typeid(PositionComponent), typeid(CollisionComponent)}), _wal(wal) + { + + } + void CollisionSystem::onFixedUpdate(WAL::Entity &entity) + { + 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()) { + 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; + } + } +} \ No newline at end of file diff --git a/sources/System/Collision/CollisionSystem.hpp b/sources/System/Collision/CollisionSystem.hpp new file mode 100644 index 00000000..41b7d13c --- /dev/null +++ b/sources/System/Collision/CollisionSystem.hpp @@ -0,0 +1,32 @@ + +// +// Created by Louis Auzuret on 5/20/21 +// + +#pragma once + +#include +#include "Wal.hpp" +#include "System/System.hpp" + +namespace BBM +{ + //! @brief A system to handle collisions. + class CollisionSystem : public WAL::System + { + private: + WAL::Wal &_wal; + public: + //! @inherit + void onFixedUpdate(WAL::Entity &entity) override; + + //! @brief A default constructor + CollisionSystem(WAL::Wal &wal); + //! @brief A movable system is copy constructable + CollisionSystem(const CollisionSystem &) = default; + //! @brief A default destructor + ~CollisionSystem() override = default; + //! @brief A movable system is assignable. + CollisionSystem &operator=(const CollisionSystem &) = default; + }; +} \ No newline at end of file From 47aac958012aa133931cbb66ed5b19ac065d5beb Mon Sep 17 00:00:00 2001 From: Bluub Date: Mon, 31 May 2021 14:54:53 +0200 Subject: [PATCH 02/12] adding unit test crashing on addEntity --- CMakeLists.txt | 1 + .../Collision/CollisionComponent.cpp | 20 +++---- .../Collision/CollisionComponent.hpp | 4 +- tests/CollisionTest.cpp | 53 +++++++++++++++++++ 4 files changed, 65 insertions(+), 13 deletions(-) create mode 100644 tests/CollisionTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0c0e8f2f..10f22238 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ add_executable(unit_tests EXCLUDE_FROM_ALL tests/MainTest.cpp tests/EngineTests.cpp tests/CallbackTest.cpp + tests/CollisionTest.cpp ) target_include_directories(unit_tests PUBLIC sources) target_link_libraries(unit_tests PUBLIC wal ray) diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index 0f14ca27..2f5b6c9d 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -17,48 +17,48 @@ namespace BBM } CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function callback, Vector3f bound) - : WAL::Component(entity), onCollide(callback), _boundX(bound.x), _boundY(bound.y), _boundZ(bound.z) + : WAL::Component(entity), onCollide(callback), _bound(bound) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function callback, float boundSize) - : WAL::Component(entity), onCollide(callback), _boundX(boundSize), _boundY(boundSize), _boundZ(boundSize) + : WAL::Component(entity), onCollide(callback), _bound({boundSize, boundSize, boundSize}) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback callback, Vector3f bound) - : WAL::Component(entity), onCollide(callback), _boundX(bound.x), _boundY(bound.y), _boundZ(bound.z) + : WAL::Component(entity), onCollide(callback), _bound(bound) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback callback, float boundSize) - : WAL::Component(entity), onCollide(callback), _boundX(boundSize), _boundY(boundSize), _boundZ(boundSize) + : WAL::Component(entity), onCollide(callback), _bound({boundSize, boundSize, boundSize}) { } float CollisionComponent::getBoundX(void) const { - return _boundX; + return _bound.x; } float CollisionComponent::getBoundY(void) const { - return _boundY; + return _bound.y; } float CollisionComponent::getBoundZ(void) const { - return _boundZ; + return _bound.z; } void CollisionComponent::setBoundX(float value) { - _boundX = value; + _bound.x = value; } void CollisionComponent::setBoundY(float value) { - _boundY = value; + _bound.y = value; } void CollisionComponent::setBoundZ(float value) { - _boundZ = value; + _bound.z = value; } } \ No newline at end of file diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 64ecc243..9d557f64 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -14,9 +14,7 @@ namespace BBM class CollisionComponent : public WAL::Component { private: - float _boundX; - float _boundY; - float _boundZ; + Vector3f _bound; public: //! @brief get bound size on the X axis float getBoundX(void) const; diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp new file mode 100644 index 00000000..72818cef --- /dev/null +++ b/tests/CollisionTest.cpp @@ -0,0 +1,53 @@ +// +// Created by Louis Auzuret on 5/31/21. +// + +#include +#include "Entity/Entity.hpp" +#include "Component/Position/PositionComponent.hpp" +#include "System/Collision/CollisionSystem.hpp" +#include "Wal.hpp" + +#define private public +#include "Component/Collision/CollisionComponent.hpp" + +using namespace WAL; +using namespace BBM; + + +TEST_CASE("Collsion test", "[Component][System]") +{ + Wal wal; + wal.scene->addEntity("player") + .addComponent(); + // .addComponent([](Entity &actual, const Entity &) { + // auto &pos = actual.getComponent(); + // pos.position.x = 1; + // pos.position.y = 1; + // pos.position.z = 1; + // }, 5.0); + //Entity &entity = wal.scene->getEntities()[0]; +// + //REQUIRE(entity.getComponent().position == Vector3f()); +// + //entity.getComponent()._bound.x = 5; + //entity.getComponent()._bound.y = 5; + //entity.getComponent()._bound.z = 5; +// + //CollisionSystem collision(wal); + //collision.onUpdate(entity, std::chrono::nanoseconds(1)); + //collision.onFixedUpdate(entity); + //REQUIRE(entity.getComponent().position.x == 0); + //REQUIRE(entity.getComponent().position.y == 0); + //REQUIRE(entity.getComponent().position.z == 0); +// +// + //wal.scene->addEntity("block") + // .addComponent() + // .addComponent([](Entity &, const Entity &){}, 1); + //collision.onUpdate(entity, std::chrono::nanoseconds(1)); + //collision.onFixedUpdate(entity); + //REQUIRE(entity.getComponent().position.x == 1); + //REQUIRE(entity.getComponent().position.y == 1); + //REQUIRE(entity.getComponent().position.z == 1); +} \ No newline at end of file From 761eaabddb1ea4e3a10145ad1100926b2411b58a Mon Sep 17 00:00:00 2001 From: Bluub Date: Mon, 31 May 2021 15:26:35 +0200 Subject: [PATCH 03/12] adding unit test and constructor for collusion components without callbacks --- .../Collision/CollisionComponent.cpp | 8 +++ .../Collision/CollisionComponent.hpp | 8 ++- sources/System/Collision/CollisionSystem.cpp | 2 + tests/CollisionTest.cpp | 67 ++++++++++--------- 4 files changed, 53 insertions(+), 32 deletions(-) diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index 2f5b6c9d..f920e8db 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -32,6 +32,14 @@ namespace BBM : WAL::Component(entity), onCollide(callback), _bound({boundSize, boundSize, boundSize}) { } + CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound) + : WAL::Component(entity), onCollide(), _bound(bound) + { } + + CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize) + : WAL::Component(entity), onCollide(), _bound({boundSize, boundSize, boundSize}) + { } + float CollisionComponent::getBoundX(void) const { return _bound.x; diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 9d557f64..a4e54265 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -47,7 +47,13 @@ namespace BBM //! @brief Constructor with a WAL::Callback CollisionComponent(WAL::Entity &entity, WAL::Callback callback, float boundSize = 0); - + + //! @brief Constructor of collider with no callback + CollisionComponent(WAL::Entity &entity, Vector3f bound); + + //! @brief Constructor no callback, same boundSize for all axis + CollisionComponent(WAL::Entity &entity, float boundSize); + //! @brief A component can't be instantiated, it should be derived. CollisionComponent(const CollisionComponent &) = default; diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index a13d4648..7c85195a 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -25,6 +25,8 @@ namespace BBM 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()), diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 72818cef..30c5f068 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -18,36 +18,41 @@ using namespace BBM; TEST_CASE("Collsion test", "[Component][System]") { Wal wal; + CollisionSystem collision(wal); + wal.scene = std::shared_ptr(new Scene); wal.scene->addEntity("player") - .addComponent(); - // .addComponent([](Entity &actual, const Entity &) { - // auto &pos = actual.getComponent(); - // pos.position.x = 1; - // pos.position.y = 1; - // pos.position.z = 1; - // }, 5.0); - //Entity &entity = wal.scene->getEntities()[0]; -// - //REQUIRE(entity.getComponent().position == Vector3f()); -// - //entity.getComponent()._bound.x = 5; - //entity.getComponent()._bound.y = 5; - //entity.getComponent()._bound.z = 5; -// - //CollisionSystem collision(wal); - //collision.onUpdate(entity, std::chrono::nanoseconds(1)); - //collision.onFixedUpdate(entity); - //REQUIRE(entity.getComponent().position.x == 0); - //REQUIRE(entity.getComponent().position.y == 0); - //REQUIRE(entity.getComponent().position.z == 0); -// -// - //wal.scene->addEntity("block") - // .addComponent() - // .addComponent([](Entity &, const Entity &){}, 1); - //collision.onUpdate(entity, std::chrono::nanoseconds(1)); - //collision.onFixedUpdate(entity); - //REQUIRE(entity.getComponent().position.x == 1); - //REQUIRE(entity.getComponent().position.y == 1); - //REQUIRE(entity.getComponent().position.z == 1); + .addComponent() + .addComponent([](Entity &actual, const Entity &) { + try { + auto &pos = actual.getComponent(); + pos.position.x = 1; + pos.position.y = 1; + pos.position.z = 1; + } catch (std::exception &e) {}; + }, 5.0); + Entity &entity = wal.scene->getEntities()[0]; + REQUIRE(entity.getComponent().position == Vector3f()); + + entity.getComponent()._bound.x = 5; + entity.getComponent()._bound.y = 5; + entity.getComponent()._bound.z = 5; + + collision.onUpdate(entity, std::chrono::nanoseconds(1)); + collision.onFixedUpdate(entity); + REQUIRE(entity.getComponent().position.x == 0.0); + REQUIRE(entity.getComponent().position.y == 0.0); + REQUIRE(entity.getComponent().position.z == 0.0); + + wal.scene->addEntity("block") + .addComponent(2,2,2) + .addComponent(1); + Entity &player = wal.scene->getEntities()[0]; + collision.onUpdate(entity, std::chrono::nanoseconds(1)); + REQUIRE(player.hasComponent(typeid(PositionComponent))); + collision.onFixedUpdate(player); + REQUIRE(wal.scene->getEntities().size() == 2); + REQUIRE(player.hasComponent(typeid(PositionComponent))); + REQUIRE(player.getComponent().position.x == 1.0); + REQUIRE(player.getComponent().position.y == 1); + REQUIRE(player.getComponent().position.z == 1); } \ No newline at end of file From fcbbfd04d8e5eb8977ff4fec8b4dc590089df1eb Mon Sep 17 00:00:00 2001 From: Bluub Date: Mon, 31 May 2021 15:33:31 +0200 Subject: [PATCH 04/12] fix doc --- sources/Component/Collision/CollisionComponent.hpp | 9 +++++---- sources/System/Collision/CollisionSystem.hpp | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index a4e54265..54f7bfa4 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -14,6 +14,7 @@ namespace BBM class CollisionComponent : public WAL::Component { private: + //! @brief Bound size on all axis Vector3f _bound; public: //! @brief get bound size on the X axis @@ -28,7 +29,7 @@ namespace BBM void setBoundY(float); //! @brief set bound size on the Z axis void setBoundZ(float); - //onCollide functions to be called + //! @brief onCollide functions to be called WAL::Callback onCollide; //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; @@ -39,13 +40,13 @@ namespace BBM //! @brief Constructor with a callback function CollisionComponent(WAL::Entity &entity, std::function callback, Vector3f bound); - //! @brief Constructor with a callback function + //! @brief Constructor with a callback function, same boundSize for all axis CollisionComponent(WAL::Entity &entity, std::function callback, float boundSize = 0); //! @brief Constructor with a WAL::Callback CollisionComponent(WAL::Entity &entity, WAL::Callback callback, Vector3f bound); - //! @brief Constructor with a WAL::Callback + //! @brief Constructor with a WAL::Callback, same boundSize for all axis CollisionComponent(WAL::Entity &entity, WAL::Callback callback, float boundSize = 0); //! @brief Constructor of collider with no callback @@ -54,7 +55,7 @@ namespace BBM //! @brief Constructor no callback, same boundSize for all axis CollisionComponent(WAL::Entity &entity, float boundSize); - //! @brief A component can't be instantiated, it should be derived. + //! @brief Default copy constructor CollisionComponent(const CollisionComponent &) = default; //! @brief default destructor diff --git a/sources/System/Collision/CollisionSystem.hpp b/sources/System/Collision/CollisionSystem.hpp index 41b7d13c..f8748f61 100644 --- a/sources/System/Collision/CollisionSystem.hpp +++ b/sources/System/Collision/CollisionSystem.hpp @@ -15,6 +15,7 @@ namespace BBM class CollisionSystem : public WAL::System { private: + //! @brief reference to the ECS engine to get other entities WAL::Wal &_wal; public: //! @inherit @@ -22,11 +23,11 @@ namespace BBM //! @brief A default constructor CollisionSystem(WAL::Wal &wal); - //! @brief A movable system is copy constructable + //! @brief A Collision system is copy constructable CollisionSystem(const CollisionSystem &) = default; //! @brief A default destructor ~CollisionSystem() override = default; - //! @brief A movable system is assignable. + //! @brief A Collision system is assignable. CollisionSystem &operator=(const CollisionSystem &) = default; }; } \ No newline at end of file From 4d0621a987d6ab7be9c7db4019315e759553836c Mon Sep 17 00:00:00 2001 From: Bluub Date: Mon, 31 May 2021 17:26:54 +0200 Subject: [PATCH 05/12] min max static function on vector3f, bound is public, adding movable into collision calculation --- .../Collision/CollisionComponent.cpp | 5 ++ .../Collision/CollisionComponent.hpp | 2 + .../Component/Movable/MovableComponent.hpp | 1 + sources/Models/Vector3.hpp | 16 ++++++ sources/System/Collision/CollisionSystem.cpp | 49 +++++++++---------- 5 files changed, 47 insertions(+), 26 deletions(-) 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 From e1b1f0a5a406408f03b2f1698ed3283984a4f7b3 Mon Sep 17 00:00:00 2001 From: Bluub Date: Mon, 31 May 2021 17:36:48 +0200 Subject: [PATCH 06/12] setting bound as public and add velocity with a if instead of a try/catch --- .../Collision/CollisionComponent.cpp | 47 +++---------------- .../Collision/CollisionComponent.hpp | 18 +------ sources/System/Collision/CollisionSystem.cpp | 12 ++--- sources/System/Collision/CollisionSystem.hpp | 14 +++--- tests/CollisionTest.cpp | 6 +-- 5 files changed, 23 insertions(+), 74 deletions(-) diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index 32a72a8b..fe27492f 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -17,61 +17,26 @@ namespace BBM } CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function callback, Vector3f bound) - : WAL::Component(entity), onCollide(callback), _bound(bound) + : WAL::Component(entity), onCollide(callback), bound(bound) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function callback, float boundSize) - : WAL::Component(entity), onCollide(callback), _bound({boundSize, boundSize, boundSize}) + : WAL::Component(entity), onCollide(callback), bound({boundSize, boundSize, boundSize}) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback callback, Vector3f bound) - : WAL::Component(entity), onCollide(callback), _bound(bound) + : WAL::Component(entity), onCollide(callback), bound(bound) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback callback, float boundSize) - : WAL::Component(entity), onCollide(callback), _bound({boundSize, boundSize, boundSize}) + : WAL::Component(entity), onCollide(callback), bound({boundSize, boundSize, boundSize}) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound) - : WAL::Component(entity), onCollide(), _bound(bound) + : WAL::Component(entity), onCollide(), bound(bound) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize) - : WAL::Component(entity), onCollide(), _bound({boundSize, boundSize, boundSize}) + : WAL::Component(entity), onCollide(), bound({boundSize, boundSize, boundSize}) { } - - Vector3f CollisionComponent::getBounds(void) const - { - return _bound; - } - - float CollisionComponent::getBoundX(void) const - { - return _bound.x; - } - - float CollisionComponent::getBoundY(void) const - { - return _bound.y; - } - - float CollisionComponent::getBoundZ(void) const - { - return _bound.z; - } - - void CollisionComponent::setBoundX(float value) - { - _bound.x = value; - } - - void CollisionComponent::setBoundY(float value) - { - _bound.y = value; - } - - void CollisionComponent::setBoundZ(float value) - { - _bound.z = value; - } } \ No newline at end of file diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 1e4156e4..2f10ad5b 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -14,23 +14,9 @@ namespace BBM class CollisionComponent : public WAL::Component { private: - //! @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 - float getBoundY(void) const; - //! @brief get bound size on the Z axis - float getBoundZ(void) const; - //! @brief set bound size on the X axis - void setBoundX(float); - //! @brief set bound size on the Y axis - void setBoundY(float); - //! @brief set bound size on the Z axis - void setBoundZ(float); + //! @brief Bound size on all axis + Vector3f bound; //! @brief onCollide functions to be called WAL::Callback onCollide; //! @inherit diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 6c644555..152051d6 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -19,19 +19,17 @@ namespace BBM auto &posA = entity.getComponent(); auto &col = entity.getComponent(); Vector3f position = posA.position; - try { - 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()); + if (entity.hasComponent(typeid(MovableComponent))) + position += entity.getComponent()._velocity; + Vector3f minA = Vector3f::min(position, position + col.bound); + Vector3f maxA = Vector3f::max(position, position + col.bound); 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 colB = entity.getComponent().bound; auto posB = other.getComponent().position; Vector3f minB = Vector3f::min(posB, posB + colB); Vector3f maxB = Vector3f::max(posB, posB + colB); diff --git a/sources/System/Collision/CollisionSystem.hpp b/sources/System/Collision/CollisionSystem.hpp index f8748f61..04f7fd95 100644 --- a/sources/System/Collision/CollisionSystem.hpp +++ b/sources/System/Collision/CollisionSystem.hpp @@ -11,15 +11,15 @@ namespace BBM { - //! @brief A system to handle collisions. - class CollisionSystem : public WAL::System - { + //! @brief A system to handle collisions. + class CollisionSystem : public WAL::System + { private: //! @brief reference to the ECS engine to get other entities WAL::Wal &_wal; - public: - //! @inherit - void onFixedUpdate(WAL::Entity &entity) override; + public: + //! @inherit + void onFixedUpdate(WAL::Entity &entity) override; //! @brief A default constructor CollisionSystem(WAL::Wal &wal); @@ -29,5 +29,5 @@ namespace BBM ~CollisionSystem() override = default; //! @brief A Collision system is assignable. CollisionSystem &operator=(const CollisionSystem &) = default; - }; + }; } \ No newline at end of file diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 30c5f068..6a9331df 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -33,9 +33,9 @@ TEST_CASE("Collsion test", "[Component][System]") Entity &entity = wal.scene->getEntities()[0]; REQUIRE(entity.getComponent().position == Vector3f()); - entity.getComponent()._bound.x = 5; - entity.getComponent()._bound.y = 5; - entity.getComponent()._bound.z = 5; + entity.getComponent().bound.x = 5; + entity.getComponent().bound.y = 5; + entity.getComponent().bound.z = 5; collision.onUpdate(entity, std::chrono::nanoseconds(1)); collision.onFixedUpdate(entity); From ef492f3b5794f24951dc0b56b1d9c076ee08362e Mon Sep 17 00:00:00 2001 From: Bluub Date: Tue, 1 Jun 2021 09:55:49 +0200 Subject: [PATCH 07/12] adding a unit test to test with a movable entity --- .../Component/Movable/MovableComponent.hpp | 4 +- tests/CollisionTest.cpp | 43 ++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/sources/Component/Movable/MovableComponent.hpp b/sources/Component/Movable/MovableComponent.hpp index ba04f804..b9a997c9 100644 --- a/sources/Component/Movable/MovableComponent.hpp +++ b/sources/Component/Movable/MovableComponent.hpp @@ -15,9 +15,10 @@ namespace BBM private: //! @brief The acceleration of this entity. Vector3f _acceleration; + public: //! @brief The velocity of the entity. Vector3f _velocity; - public: + //! @brief Add an instant force to this entity. //! @param force The force to add to this entity's acceleration. The force is added instantly and in one go. void addForce(Vector3f force); @@ -35,6 +36,5 @@ namespace BBM MovableComponent &operator=(const MovableComponent &) = delete; friend class MovableSystem; - friend class CollisionSystem; }; } // namespace WAL \ No newline at end of file diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 6a9331df..7d296203 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -5,6 +5,8 @@ #include #include "Entity/Entity.hpp" #include "Component/Position/PositionComponent.hpp" +#include "Component/Movable/MovableComponent.hpp" +#include "System/Movable/MovableSystem.hpp" #include "System/Collision/CollisionSystem.hpp" #include "Wal.hpp" @@ -15,7 +17,7 @@ using namespace WAL; using namespace BBM; -TEST_CASE("Collsion test", "[Component][System]") +TEST_CASE("Collision test", "[Component][System]") { Wal wal; CollisionSystem collision(wal); @@ -55,4 +57,41 @@ TEST_CASE("Collsion test", "[Component][System]") REQUIRE(player.getComponent().position.x == 1.0); REQUIRE(player.getComponent().position.y == 1); REQUIRE(player.getComponent().position.z == 1); -} \ No newline at end of file +} + + +TEST_CASE("Collsion test with movable", "[Component][System]") +{ + Wal wal; + CollisionSystem collision(wal); + MovableSystem movable; + wal.scene = std::shared_ptr(new Scene); + wal.scene->addEntity("player") + .addComponent() + .addComponent([](Entity &actual, const Entity &) { + try { + auto &mov = actual.getComponent(); + mov._velocity = {0, 0, 0}; + } catch (std::exception &e) {}; + }, 5.0) + .addComponent(); + + wal.scene->addEntity("block") + .addComponent(0, 0, 0) + .addComponent(1); + Entity &entity = wal.scene->getEntities()[0]; + REQUIRE(entity.getComponent().position == Vector3f()); + + entity.getComponent().bound.x = 5; + entity.getComponent().bound.y = 5; + entity.getComponent().bound.z = 5; + + entity.getComponent().addForce({1, 1, 1}); + collision.onUpdate(entity, std::chrono::nanoseconds(1)); + collision.onFixedUpdate(entity); + movable.onUpdate(entity, std::chrono::nanoseconds(1)); + movable.onFixedUpdate(entity); + REQUIRE(entity.getComponent().position.x == 0.0); + REQUIRE(entity.getComponent().position.y == 0.0); + REQUIRE(entity.getComponent().position.z == 0.0); +} \ No newline at end of file From b3dd001532637b453324c138b1147ac2733f367a Mon Sep 17 00:00:00 2001 From: Bluub Date: Tue, 1 Jun 2021 14:43:48 +0200 Subject: [PATCH 08/12] adding on collided callback --- .../Collision/CollisionComponent.cpp | 31 +++++++++++++------ .../Collision/CollisionComponent.hpp | 22 ++++++++----- .../Component/Movable/MovableComponent.cpp | 11 +++++++ .../Component/Movable/MovableComponent.hpp | 8 ++++- sources/System/Collision/CollisionSystem.cpp | 14 +++++---- 5 files changed, 62 insertions(+), 24 deletions(-) diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index fe27492f..dc0992f4 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -16,27 +16,38 @@ namespace BBM return new CollisionComponent(entity); } - CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function callback, Vector3f bound) - : WAL::Component(entity), onCollide(callback), bound(bound) + CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, Vector3f bound) + : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound(bound) { } - CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function callback, float boundSize) - : WAL::Component(entity), onCollide(callback), bound({boundSize, boundSize, boundSize}) + CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, float boundSize) + : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound({boundSize, boundSize, boundSize}) { } - CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback callback, Vector3f bound) - : WAL::Component(entity), onCollide(callback), bound(bound) + CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, Vector3f bound) + : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound(bound) { } - CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback callback, float boundSize) - : WAL::Component(entity), onCollide(callback), bound({boundSize, boundSize, boundSize}) + CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, float boundSize) + : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound({boundSize, boundSize, boundSize}) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound) - : WAL::Component(entity), onCollide(), bound(bound) + : WAL::Component(entity), _onCollide(), _onCollided(), bound(bound) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize) - : WAL::Component(entity), onCollide(), bound({boundSize, boundSize, boundSize}) + : WAL::Component(entity), _onCollide(), _onCollided(), bound({boundSize, boundSize, boundSize}) { } + + const WAL::Callback &CollisionComponent::getOnCollide(void) const + { + return _onCollide; + } + + const WAL::Callback &CollisionComponent::getOnCollided(void) const + { + return _onCollided; + } + } \ No newline at end of file diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 2f10ad5b..8ef05bcd 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -14,28 +14,30 @@ namespace BBM class CollisionComponent : public WAL::Component { private: + //! @brief onCollide functions to be called + WAL::Callback _onCollide; + //! @brief onCollided functions to be called + WAL::Callback _onCollided; public: //! @brief Bound size on all axis Vector3f bound; - //! @brief onCollide functions to be called - WAL::Callback onCollide; //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; - + //! @brief A component can't be instantiated, it should be derived. explicit CollisionComponent(WAL::Entity &entity); //! @brief Constructor with a callback function - CollisionComponent(WAL::Entity &entity, std::function callback, Vector3f bound); + CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, Vector3f bound); //! @brief Constructor with a callback function, same boundSize for all axis - CollisionComponent(WAL::Entity &entity, std::function callback, float boundSize = 0); + CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, float boundSize = 0); //! @brief Constructor with a WAL::Callback - CollisionComponent(WAL::Entity &entity, WAL::Callback callback, Vector3f bound); + CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided,Vector3f bound); //! @brief Constructor with a WAL::Callback, same boundSize for all axis - CollisionComponent(WAL::Entity &entity, WAL::Callback callback, float boundSize = 0); + CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, float boundSize = 0); //! @brief Constructor of collider with no callback CollisionComponent(WAL::Entity &entity, Vector3f bound); @@ -51,5 +53,11 @@ namespace BBM //! @brief A component can't be assigned CollisionComponent &operator=(const CollisionComponent &) = delete; + + //! @brief Get reference of the onCollide callback + const WAL::Callback &getOnCollide(void) const; + + //! @brief Get reference of the onCollided callback + const WAL::Callback &getOnCollided(void) const; }; } \ No newline at end of file diff --git a/sources/Component/Movable/MovableComponent.cpp b/sources/Component/Movable/MovableComponent.cpp index 4fc9bc0c..51f0707d 100644 --- a/sources/Component/Movable/MovableComponent.cpp +++ b/sources/Component/Movable/MovableComponent.cpp @@ -19,4 +19,15 @@ namespace BBM { this->_acceleration += force; } + + void MovableComponent::resetVelocity(void) + { + this->_velocity = {0, 0, 0}; + } + + const Vector3f &MovableComponent::getVelocity(void) const + { + return _velocity; + } + } // namespace WAL \ No newline at end of file diff --git a/sources/Component/Movable/MovableComponent.hpp b/sources/Component/Movable/MovableComponent.hpp index b9a997c9..13c8bf90 100644 --- a/sources/Component/Movable/MovableComponent.hpp +++ b/sources/Component/Movable/MovableComponent.hpp @@ -15,14 +15,20 @@ namespace BBM private: //! @brief The acceleration of this entity. Vector3f _acceleration; - public: //! @brief The velocity of the entity. Vector3f _velocity; + public: //! @brief Add an instant force to this entity. //! @param force The force to add to this entity's acceleration. The force is added instantly and in one go. void addForce(Vector3f force); + //! @brief Set velocity to 0 + void resetVelocity(void); + + //! @brief Get velocity + const Vector3f &getVelocity(void) const; + //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 152051d6..d3063ebd 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -20,7 +20,7 @@ namespace BBM auto &col = entity.getComponent(); Vector3f position = posA.position; if (entity.hasComponent(typeid(MovableComponent))) - position += entity.getComponent()._velocity; + position += entity.getComponent().getVelocity(); Vector3f minA = Vector3f::min(position, position + col.bound); Vector3f maxA = Vector3f::max(position, position + col.bound); for (auto &other : _wal.scene->getEntities()) { @@ -29,14 +29,16 @@ namespace BBM if (!other.hasComponent(typeid(CollisionComponent)) || !other.hasComponent(typeid(PositionComponent))) continue; - auto colB = entity.getComponent().bound; + auto colB = entity.getComponent(); auto posB = other.getComponent().position; - Vector3f minB = Vector3f::min(posB, posB + colB); - Vector3f maxB = Vector3f::max(posB, posB + colB); + Vector3f minB = Vector3f::min(posB, posB + colB.bound); + Vector3f maxB = Vector3f::max(posB, posB + colB.bound); 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); + (minA.z <= maxB.z && maxA.z >= minB.z)) { + col.getOnCollide()(entity, other); + colB.getOnCollided()(entity, other); + } } } } \ No newline at end of file From 573df710f95dee3c0bc01c2fcb9b38ef284b6de4 Mon Sep 17 00:00:00 2001 From: Bluub Date: Tue, 1 Jun 2021 14:44:08 +0200 Subject: [PATCH 09/12] updating unit test --- tests/CollisionTest.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 7d296203..69c75207 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -31,7 +31,7 @@ TEST_CASE("Collision test", "[Component][System]") pos.position.y = 1; pos.position.z = 1; } catch (std::exception &e) {}; - }, 5.0); + }, [](Entity &, const Entity &){}, 5.0); Entity &entity = wal.scene->getEntities()[0]; REQUIRE(entity.getComponent().position == Vector3f()); @@ -68,17 +68,17 @@ TEST_CASE("Collsion test with movable", "[Component][System]") wal.scene = std::shared_ptr(new Scene); wal.scene->addEntity("player") .addComponent() - .addComponent([](Entity &actual, const Entity &) { - try { - auto &mov = actual.getComponent(); - mov._velocity = {0, 0, 0}; - } catch (std::exception &e) {}; - }, 5.0) + .addComponent([](Entity &actual, const Entity &) {}, [](Entity &actual, const Entity &) {}, 5.0) .addComponent(); wal.scene->addEntity("block") .addComponent(0, 0, 0) - .addComponent(1); + .addComponent([](Entity &actual, const Entity &){}, [](Entity &actual, const Entity &) { + try { + auto &mov = actual.getComponent(); + mov.resetVelocity(); + } catch (std::exception &e) {}; + }, 1); Entity &entity = wal.scene->getEntities()[0]; REQUIRE(entity.getComponent().position == Vector3f()); From 349da67b2957d704c05036ab7bdef4b6cffd5e92 Mon Sep 17 00:00:00 2001 From: Bluub Date: Tue, 1 Jun 2021 17:21:26 +0200 Subject: [PATCH 10/12] runner with the collision system --- a | 12 ++++++++ sources/Runner/Runner.cpp | 31 +++++++++++++++----- sources/System/Collision/CollisionSystem.cpp | 16 +++++++--- sources/System/Collision/CollisionSystem.hpp | 3 ++ 4 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 a diff --git a/a b/a new file mode 100644 index 00000000..473008ac --- /dev/null +++ b/a @@ -0,0 +1,12 @@ +0 1 -- 2 3 +false || false +0 2 -- 1 3 +true +0 3 -- 1 2 +true +1 2 -- 0 3 +true +1 3 -- 0 2 +true +2 3 -- 0 1 +false \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 9e527fcb..2cdb2bc8 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -10,11 +10,14 @@ #include #include #include +#include #include #include #include #include +#include #include +#include #include #include #include @@ -43,6 +46,7 @@ namespace BBM wal.addSystem() .addSystem() .addSystem() + .addSystem(wal) .addSystem(); } @@ -52,6 +56,7 @@ namespace BBM RAY::Window &window = RAY::Window::getInstance(600, 400, "Bomberman", FLAG_WINDOW_RESIZABLE); wal.addSystem>(); + wal.addSystem>(); wal.addSystem(window) .addSystem>(); @@ -61,20 +66,30 @@ namespace BBM std::shared_ptr loadGameScene() { auto scene = std::make_shared(); - scene->addEntity("cube") - .addComponent() - .addComponent>(Vector2f(), Vector2f(10, 10), RED) - .addComponent() - .addComponent() - .addComponent();; + RAY3D::Cube cube(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED); + RAY3D::Cube cubePlayer(Vector3f(0, 0, 0), Vector3f(3, 3, 3), GREEN); scene->addEntity("player") .addComponent() - .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) + .addComponent>(cubePlayer) .addComponent() .addComponent() + .addComponent(3) .addComponent(); + scene->addEntity("cube") + .addComponent(-5, 0, -5) + .addComponent>(cube) + .addComponent() + .addComponent() + .addComponent([](WAL::Entity &, const WAL::Entity &){}, + [](WAL::Entity &actual, const WAL::Entity &) { + try { + auto &mov = actual.getComponent(); + mov.resetVelocity(); + } catch (std::exception &e) { }; + }, 3); + scene->addEntity("camera") - .addComponent(0, 20, -5) + .addComponent(0, 20, -1) .addComponent(); return scene; } diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index d3063ebd..36616155 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -14,6 +14,16 @@ namespace BBM { } + + bool CollisionSystem::collide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB) + { + bool overlapX = (minA.x <= maxB.x && maxA.x >= minB.x) || (minB.x <= maxA.x && maxB.x >= minA.x); + bool overlapY = (minA.y <= maxB.y && maxA.y >= minB.y) || (minB.y <= maxA.y && maxB.y >= minA.y); + bool overlapZ = (minA.z <= maxB.z && maxA.z >= minB.z) || (minB.z <= maxA.z && maxB.z >= minA.z); + + return (overlapX && overlapY && overlapZ); + } + void CollisionSystem::onFixedUpdate(WAL::Entity &entity) { auto &posA = entity.getComponent(); @@ -29,13 +39,11 @@ namespace BBM if (!other.hasComponent(typeid(CollisionComponent)) || !other.hasComponent(typeid(PositionComponent))) continue; - auto colB = entity.getComponent(); + auto colB = other.getComponent(); auto posB = other.getComponent().position; Vector3f minB = Vector3f::min(posB, posB + colB.bound); Vector3f maxB = Vector3f::max(posB, posB + colB.bound); - 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)) { + if (collide(minA, maxA, minB, maxB)) { col.getOnCollide()(entity, other); colB.getOnCollided()(entity, other); } diff --git a/sources/System/Collision/CollisionSystem.hpp b/sources/System/Collision/CollisionSystem.hpp index 04f7fd95..c4c2de25 100644 --- a/sources/System/Collision/CollisionSystem.hpp +++ b/sources/System/Collision/CollisionSystem.hpp @@ -29,5 +29,8 @@ namespace BBM ~CollisionSystem() override = default; //! @brief A Collision system is assignable. CollisionSystem &operator=(const CollisionSystem &) = default; + + //! @brief check AABB collision + static bool collide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB); }; } \ No newline at end of file From 2bd387195f4fae1bd8a7ad54074112fecd855e85 Mon Sep 17 00:00:00 2001 From: Bluub Date: Tue, 1 Jun 2021 17:21:40 +0200 Subject: [PATCH 11/12] remove unrelated file --- a | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 a diff --git a/a b/a deleted file mode 100644 index 473008ac..00000000 --- a/a +++ /dev/null @@ -1,12 +0,0 @@ -0 1 -- 2 3 -false || false -0 2 -- 1 3 -true -0 3 -- 1 2 -true -1 2 -- 0 3 -true -1 3 -- 0 2 -true -2 3 -- 0 1 -false \ No newline at end of file From d0205b68e6ea5d8697537c65c10aaf69c3e0ed2e Mon Sep 17 00:00:00 2001 From: Bluub Date: Tue, 1 Jun 2021 17:45:09 +0200 Subject: [PATCH 12/12] public onCollide and onCollided --- .../Collision/CollisionComponent.cpp | 23 +++++-------------- .../Collision/CollisionComponent.hpp | 14 ++++------- sources/Runner/Runner.cpp | 9 ++++---- sources/System/Collision/CollisionSystem.cpp | 8 +++---- 4 files changed, 18 insertions(+), 36 deletions(-) diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index dc0992f4..22cbdef8 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -17,37 +17,26 @@ namespace BBM } CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, Vector3f bound) - : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound(bound) + : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound(bound) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, std::function onCollide, std::function onCollided, float boundSize) - : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound({boundSize, boundSize, boundSize}) + : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound({boundSize, boundSize, boundSize}) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, Vector3f bound) - : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound(bound) + : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound(bound) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, float boundSize) - : WAL::Component(entity), _onCollide(onCollide), _onCollided(onCollided), bound({boundSize, boundSize, boundSize}) + : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), bound({boundSize, boundSize, boundSize}) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound) - : WAL::Component(entity), _onCollide(), _onCollided(), bound(bound) + : WAL::Component(entity), onCollide(), onCollided(), bound(bound) { } CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize) - : WAL::Component(entity), _onCollide(), _onCollided(), bound({boundSize, boundSize, boundSize}) + : WAL::Component(entity), onCollide(), onCollided(), bound({boundSize, boundSize, boundSize}) { } - - const WAL::Callback &CollisionComponent::getOnCollide(void) const - { - return _onCollide; - } - - const WAL::Callback &CollisionComponent::getOnCollided(void) const - { - return _onCollided; - } - } \ No newline at end of file diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 8ef05bcd..4636c79a 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -14,11 +14,11 @@ namespace BBM class CollisionComponent : public WAL::Component { private: - //! @brief onCollide functions to be called - WAL::Callback _onCollide; - //! @brief onCollided functions to be called - WAL::Callback _onCollided; public: + //! @brief onCollide functions to be called + WAL::Callback onCollide; + //! @brief onCollided functions to be called + WAL::Callback onCollided; //! @brief Bound size on all axis Vector3f bound; //! @inherit @@ -53,11 +53,5 @@ namespace BBM //! @brief A component can't be assigned CollisionComponent &operator=(const CollisionComponent &) = delete; - - //! @brief Get reference of the onCollide callback - const WAL::Callback &getOnCollide(void) const; - - //! @brief Get reference of the onCollided callback - const WAL::Callback &getOnCollided(void) const; }; } \ No newline at end of file diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 2cdb2bc8..27ea76c5 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -67,13 +67,12 @@ namespace BBM { auto scene = std::make_shared(); RAY3D::Cube cube(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED); - RAY3D::Cube cubePlayer(Vector3f(0, 0, 0), Vector3f(3, 3, 3), GREEN); scene->addEntity("player") .addComponent() - .addComponent>(cubePlayer) + .addComponent>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() .addComponent() - .addComponent(3) + .addComponent(2) .addComponent(); scene->addEntity("cube") .addComponent(-5, 0, -5) @@ -83,8 +82,8 @@ namespace BBM .addComponent([](WAL::Entity &, const WAL::Entity &){}, [](WAL::Entity &actual, const WAL::Entity &) { try { - auto &mov = actual.getComponent(); - mov.resetVelocity(); + auto &mov = actual.getComponent(); + mov.resetVelocity(); } catch (std::exception &e) { }; }, 3); diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 36616155..24971304 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -36,16 +36,16 @@ namespace BBM for (auto &other : _wal.scene->getEntities()) { if (&other == &entity) continue; - if (!other.hasComponent(typeid(CollisionComponent)) || - !other.hasComponent(typeid(PositionComponent))) + if (!other.hasComponent() || + !other.hasComponent()) continue; auto colB = other.getComponent(); auto posB = other.getComponent().position; Vector3f minB = Vector3f::min(posB, posB + colB.bound); Vector3f maxB = Vector3f::max(posB, posB + colB.bound); if (collide(minA, maxA, minB, maxB)) { - col.getOnCollide()(entity, other); - colB.getOnCollided()(entity, other); + col.onCollide(entity, other); + colB.onCollided(entity, other); } } }