From b7d43fef6aff270c2169c03fed15024f7bafc156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Mon, 7 Jun 2021 18:01:19 +0200 Subject: [PATCH 01/18] starting to look into collisions --- CMakeLists.txt | 18 +++++++++--------- sources/Map/Map.cpp | 2 ++ sources/Runner/Runner.cpp | 14 +++++++------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 966a6d01..8a830ed8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -79,15 +79,15 @@ target_link_libraries(bomberman PUBLIC wal ray) add_executable(unit_tests EXCLUDE_FROM_ALL - ${SOURCES} - tests/EntityTests.cpp - tests/MainTest.cpp - tests/EngineTests.cpp - tests/CallbackTest.cpp - tests/MoveTests.cpp - tests/ViewTest.cpp - tests/CollisionTest.cpp -) + ${SOURCES} + tests/EntityTests.cpp + tests/MainTest.cpp + tests/EngineTests.cpp + tests/CallbackTest.cpp + tests/MoveTests.cpp + tests/ViewTest.cpp + tests/CollisionTest.cpp + ) target_include_directories(unit_tests PUBLIC sources) target_link_libraries(unit_tests PUBLIC wal ray) diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index c4709164..618725b6 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -16,6 +16,8 @@ namespace BBM auto *mov = entity.tryGetComponent(); if (!mov) return; + mov->_velocity = BBM::Vector3f (); + return; auto &pos = entity.getComponent(); const auto &wallPos = wall.getComponent(); auto diff = pos.position + mov->getVelocity() - wallPos.position; diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 011ffee3..1be23954 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -72,14 +72,14 @@ namespace BBM scene->addEntity("camera") .addComponent(8, 20, 7) .addComponent(Vector3f(8, 0, 8)); -// scene->addEntity("cube") -// .addComponent(5, 0, 5) -// .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) -// .addComponent() -// .addComponent() -// .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 3); + scene->addEntity("cube") + .addComponent(5, 0, 5) + .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) + .addComponent() + .addComponent() + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 3); std::srand(std::time(nullptr)); - MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); + //MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; } From 8203532d52395fa41455d95ae01a650798e2d6e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Mon, 7 Jun 2021 22:06:17 +0200 Subject: [PATCH 02/18] code style fix --- .../Collision/CollisionComponent.cpp | 45 +++++++++----- .../Collision/CollisionComponent.hpp | 60 ++++++++++--------- tests/CollisionTest.cpp | 8 +-- 3 files changed, 68 insertions(+), 45 deletions(-) diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index df727b2d..35da6cb8 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -4,31 +4,48 @@ #include "Component/Collision/CollisionComponent.hpp" - -namespace BBM +namespace BBM { CollisionComponent::CollisionComponent(WAL::Entity &entity) - : WAL::Component(entity) - { } + : WAL::Component(entity) + {} WAL::Component *CollisionComponent::clone(WAL::Entity &entity) const { return new CollisionComponent(entity); } - 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, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, + Vector3f 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}) - { } + CollisionComponent::CollisionComponent(WAL::Entity &entity, + const WAL::Callback &onCollide, + const 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(), 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}) + {} } \ No newline at end of file diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 5bfe7e16..0e4caf62 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -9,43 +9,49 @@ #include "Component/Component.hpp" #include "Entity/Entity.hpp" -namespace BBM +namespace BBM { class CollisionComponent : public WAL::Component { - private: - 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 - WAL::Component *clone(WAL::Entity &entity) const override; + 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; - //! @brief A component can't be instantiated, it should be derived. - explicit CollisionComponent(WAL::Entity &entity); + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; - //! @brief Constructor with a WAL::Callback - CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided,Vector3f bound); + //! @brief A component can't be instantiated, it should be derived. + explicit CollisionComponent(WAL::Entity &entity); - //! @brief Constructor with a WAL::Callback, same boundSize for all axis - CollisionComponent(WAL::Entity &entity, WAL::Callback onCollide, WAL::Callback onCollided, float boundSize = 0); + //! @brief Constructor with a WAL::Callback + CollisionComponent(WAL::Entity &entity, + const WAL::Callback& onCollide, + const WAL::Callback& onCollided, + Vector3f bound); - //! @brief Constructor of collider with no callback - CollisionComponent(WAL::Entity &entity, Vector3f bound); + //! @brief Constructor with a WAL::Callback, same boundSize for all axis + CollisionComponent(WAL::Entity &entity, + const WAL::Callback& onCollide, + const WAL::Callback& onCollided, + float boundSize = 0); - //! @brief Constructor no callback, same boundSize for all axis - CollisionComponent(WAL::Entity &entity, float boundSize); + //! @brief Constructor of collider with no callback + CollisionComponent(WAL::Entity &entity, Vector3f bound); - //! @brief Default copy constructor - CollisionComponent(const CollisionComponent &) = default; + //! @brief Constructor no callback, same boundSize for all axis + CollisionComponent(WAL::Entity &entity, float boundSize); - //! @brief default destructor - ~CollisionComponent() override = default; + //! @brief Default copy constructor + CollisionComponent(const CollisionComponent &) = default; - //! @brief A component can't be assigned - CollisionComponent &operator=(const CollisionComponent &) = delete; + //! @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/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 394b1ccc..8392aa85 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -26,10 +26,10 @@ TEST_CASE("Collision test", "[Component][System]") .addComponent() .addComponent([](Entity &actual, const Entity &) { try { - auto &pos = actual.getComponent(); - pos.position.x = 1; - pos.position.y = 1; - pos.position.z = 1; + auto &pos = actual.getComponent(); + pos.position.x = 1; + pos.position.y = 1; + pos.position.z = 1; } catch (std::exception &e) {}; }, [](Entity &, const Entity &){}, 5.0); Entity &entity = wal.scene->getEntities().front(); From 65af11f3f9833cf67638641cd8bfae1ad7c35c78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Mon, 7 Jun 2021 23:37:51 +0200 Subject: [PATCH 03/18] collisions are working ok with user defined offsets --- lib/Ray/sources/Camera/Camera3D.cpp | 1 + .../Collision/CollisionComponent.cpp | 18 +++++++---- .../Collision/CollisionComponent.hpp | 16 ++++++---- sources/Map/Map.cpp | 7 +++- sources/Runner/Runner.cpp | 12 +++---- sources/System/Collision/CollisionSystem.cpp | 30 ++++++++++++----- sources/System/Renderer/RenderSystem.cpp | 32 ++++++++++++++++--- 7 files changed, 85 insertions(+), 31 deletions(-) diff --git a/lib/Ray/sources/Camera/Camera3D.cpp b/lib/Ray/sources/Camera/Camera3D.cpp index aa2b2a9b..0c4b3d13 100644 --- a/lib/Ray/sources/Camera/Camera3D.cpp +++ b/lib/Ray/sources/Camera/Camera3D.cpp @@ -10,6 +10,7 @@ RAY::Camera::Camera3D::Camera3D(const RAY::Vector3 &position, const RAY::Vector3 &target, const RAY::Vector3 &up, float fovy, Projection projection): _camera({position, target, up, fovy, projection}) { + SetCameraMode(_camera, CAMERA_FREE); } void RAY::Camera::Camera3D::setPosition(const Vector3 &Position) diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index 35da6cb8..8f7db0d7 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -18,34 +18,40 @@ namespace BBM CollisionComponent::CollisionComponent(WAL::Entity &entity, const WAL::Callback &onCollide, const WAL::Callback &onCollided, + Vector3f positionOffset, Vector3f bound) : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), - bound(bound) + bound(bound), + positionOffset(positionOffset) {} CollisionComponent::CollisionComponent(WAL::Entity &entity, const WAL::Callback &onCollide, const WAL::Callback &onCollided, + float positionOffset, float boundSize) : WAL::Component(entity), onCollide(onCollide), onCollided(onCollided), - bound({boundSize, boundSize, boundSize}) + bound({boundSize, boundSize, boundSize}), + positionOffset({positionOffset, positionOffset, positionOffset}) {} - CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f bound) + CollisionComponent::CollisionComponent(WAL::Entity &entity, Vector3f positionOffset, Vector3f bound) : WAL::Component(entity), onCollide(), onCollided(), - bound(bound) + bound(bound), + positionOffset(positionOffset) {} - CollisionComponent::CollisionComponent(WAL::Entity &entity, float boundSize) + CollisionComponent::CollisionComponent(WAL::Entity &entity, float positionOffset, float boundSize) : WAL::Component(entity), onCollide(), onCollided(), - bound({boundSize, boundSize, boundSize}) + bound({boundSize, boundSize, boundSize}), + positionOffset({positionOffset, positionOffset, positionOffset}) {} } \ No newline at end of file diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 0e4caf62..933d2c31 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -20,6 +20,8 @@ namespace BBM WAL::Callback onCollided; //! @brief Bound size on all axis Vector3f bound; + //! @brief Offset from the position component + Vector3f positionOffset; //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; @@ -29,21 +31,23 @@ namespace BBM //! @brief Constructor with a WAL::Callback CollisionComponent(WAL::Entity &entity, - const WAL::Callback& onCollide, - const WAL::Callback& onCollided, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, + Vector3f positionOffset, Vector3f bound); //! @brief Constructor with a WAL::Callback, same boundSize for all axis CollisionComponent(WAL::Entity &entity, - const WAL::Callback& onCollide, - const WAL::Callback& onCollided, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, + float positionOffset, float boundSize = 0); //! @brief Constructor of collider with no callback - CollisionComponent(WAL::Entity &entity, Vector3f bound); + CollisionComponent(WAL::Entity &entity, Vector3f positionOffset, Vector3f bound); //! @brief Constructor no callback, same boundSize for all axis - CollisionComponent(WAL::Entity &entity, float boundSize); + CollisionComponent(WAL::Entity &entity, float positionOffset, float boundSize); //! @brief Default copy constructor CollisionComponent(const CollisionComponent &) = default; diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 618725b6..e688fa77 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -14,9 +14,14 @@ namespace BBM void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall) { auto *mov = entity.tryGetComponent(); + auto posspec = entity.getComponent(); + auto posspecwall = wall.getComponent(); + if (!mov) return; - mov->_velocity = BBM::Vector3f (); + + std::cout << "collided coords " << posspec.position << " cube " << posspecwall.position << std::endl; + //mov->_velocity = BBM::Vector3f (); return; auto &pos = entity.getComponent(); const auto &wallPos = wall.getComponent(); diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 06201ebd..06ff3cb2 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -70,21 +70,21 @@ namespace BBM .addComponent() .addComponent() .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) - .addComponent(1) + .addComponent(0, 1) .addComponent() .addComponent(1, [](WAL::Entity &entity) { auto &animation = entity.getComponent(); animation.setAnimIndex(5); }); scene->addEntity("camera") - .addComponent(8, 20, 7) - .addComponent(Vector3f(8, 0, 8)); + .addComponent(10, 20, 10) + .addComponent(Vector3f(2, 0, 2)); scene->addEntity("cube") - .addComponent(5, 0, 5) - .addComponent(Vector3f(-5, 0, -5), Vector3f(3, 3, 3), RED) + .addComponent(0, 0, 0) + .addComponent(Vector3f(0, 0, 0), Vector3f(3, 3, 3), RED) .addComponent() .addComponent() - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 3); + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, -1, 3); std::srand(std::time(nullptr)); //MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 6a3e36cd..50c5662a 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -26,19 +26,33 @@ namespace BBM void CollisionSystem::onFixedUpdate(WAL::ViewEntity &entity) { auto &posA = entity.get(); - auto &col = entity.get(); - Vector3f position = posA.position; + auto &colA = entity.get(); + Vector3f pointA = posA.position + colA.positionOffset; + if (auto *movable = entity->tryGetComponent()) - position += movable->getVelocity(); - Vector3f minA = Vector3f::min(position, position + col.bound); - Vector3f maxA = Vector3f::max(position, position + col.bound); + pointA += movable->getVelocity(); + + Vector3f minA = Vector3f::min(pointA, pointA + colA.bound); + Vector3f maxA = Vector3f::max(pointA, pointA + colA.bound); + for (auto &[other, posB, colB] : this->getView()) { if (other.getUid() == entity->getUid()) continue; - Vector3f minB = Vector3f::min(posB.position, posB.position + colB.bound); - Vector3f maxB = Vector3f::max(posB.position, posB.position + colB.bound); + + auto pointB = posB.position + colB.positionOffset; + + // TODO if B is also a movable we don't check with it's changing position + Vector3f minB = Vector3f::min(pointB, pointB + colB.bound); + Vector3f maxB = Vector3f::max(pointB, pointB + colB.bound); + if (collide(minA, maxA, minB, maxB)) { - col.onCollide(entity, other); + std::cout << "collided" << std::endl + << "minA " << minA << std::endl + << "maxA " << maxA << std::endl + << "minB " << minB << std::endl + << "maxB " << maxB << std::endl; + return; + colA.onCollide(entity, other); colB.onCollided(entity, other); } } diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index cdabb452..b6ef7891 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -10,13 +10,16 @@ #include "Component/Renderer/Drawable2DComponent.hpp" #include "Drawables/ADrawable3D.hpp" + +#include "Component/Collision/CollisionComponent.hpp" + namespace BBM { RenderSystem::RenderSystem(WAL::Wal &wal, RAY::Window &window, bool debugMode) : System(wal), - _window(window), - _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE), - _debugMode(debugMode) + _window(window), + _camera(Vector3f(), Vector3f(), Vector3f(0, 1, 0), 50, CAMERA_PERSPECTIVE), + _debugMode(debugMode) { this->_window.setFPS(this->FPS); } @@ -29,6 +32,26 @@ namespace BBM this->_window.useCamera(this->_camera); for (auto &[_, pos, drawable] : this->_wal.scene->view()) { + if (_.getName() == "cube") { + auto col = _.getComponent(); + DrawCubeWires({pos.position.x, pos.position.y, pos.position.z}, + col.bound.x, + col.bound.y, + col.bound.z, + WHITE); + DrawPoint3D({pos.position.x, pos.position.y, pos.position.z}, BLUE); + DrawPoint3D({pos.position.x + col.bound.x, pos.position.y + col.bound.y, pos.position.z + col.bound.z}, BLUE); + } + if (_.getName() == "player") { + auto col = _.getComponent(); + DrawCubeWires({pos.position.x, pos.position.y, pos.position.z}, + col.bound.x, + col.bound.y, + col.bound.z, + WHITE); + DrawPoint3D({pos.position.x, pos.position.y, pos.position.z}, BLUE); + DrawPoint3D({pos.position.x + col.bound.x, pos.position.y + col.bound.y, pos.position.z + col.bound.z}, BLUE); + } drawable.drawable->setPosition(pos.position); drawable.drawable->drawOn(this->_window); } @@ -44,7 +67,8 @@ namespace BBM this->_window.endDrawing(); } - void RenderSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) + void + RenderSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) { const auto &pos = entity.get(); const auto &cam = entity.get(); From 22606ecf68d343130699f47756c173769f33414e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 00:19:34 +0200 Subject: [PATCH 04/18] giving the collided Axis for context to callback on collisions --- .../Collision/CollisionComponent.cpp | 8 ++-- .../Collision/CollisionComponent.hpp | 19 +++++--- sources/Map/Map.cpp | 33 +++++++------- sources/Map/Map.hpp | 2 +- sources/Runner/Runner.cpp | 4 +- sources/System/Collision/CollisionSystem.cpp | 45 ++++++++++++++----- sources/System/Collision/CollisionSystem.hpp | 2 +- 7 files changed, 70 insertions(+), 43 deletions(-) diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index 8f7db0d7..678b0898 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -16,8 +16,8 @@ namespace BBM } CollisionComponent::CollisionComponent(WAL::Entity &entity, - const WAL::Callback &onCollide, - const WAL::Callback &onCollided, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, Vector3f positionOffset, Vector3f bound) : WAL::Component(entity), @@ -28,8 +28,8 @@ namespace BBM {} CollisionComponent::CollisionComponent(WAL::Entity &entity, - const WAL::Callback &onCollide, - const WAL::Callback &onCollided, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, float positionOffset, float boundSize) : WAL::Component(entity), diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 933d2c31..1be3590d 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -14,10 +14,17 @@ namespace BBM class CollisionComponent : public WAL::Component { public: + //! @brief Used to tell the collided axis + enum CollidedAxis { + X = 1, + Y = 2, + Z = 4 + }; + //! @brief onCollide functions to be called - WAL::Callback onCollide; + WAL::Callback onCollide; //! @brief onCollided functions to be called - WAL::Callback onCollided; + WAL::Callback onCollided; //! @brief Bound size on all axis Vector3f bound; //! @brief Offset from the position component @@ -31,15 +38,15 @@ namespace BBM //! @brief Constructor with a WAL::Callback CollisionComponent(WAL::Entity &entity, - const WAL::Callback &onCollide, - const WAL::Callback &onCollided, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, Vector3f positionOffset, Vector3f bound); //! @brief Constructor with a WAL::Callback, same boundSize for all axis CollisionComponent(WAL::Entity &entity, - const WAL::Callback &onCollide, - const WAL::Callback &onCollided, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, float positionOffset, float boundSize = 0); diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index e688fa77..937343c8 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -3,7 +3,8 @@ // Edited by Benjamin Henry on 5/26/21. // -#include +#include "Component/Collision/CollisionComponent.hpp" +#include "System/Collision/CollisionSystem.hpp" #include "Map.hpp" #include @@ -11,7 +12,7 @@ namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM { - void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall) + void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall, int collidedAxis) { auto *mov = entity.tryGetComponent(); auto posspec = entity.getComponent(); @@ -20,20 +21,16 @@ namespace BBM if (!mov) return; - std::cout << "collided coords " << posspec.position << " cube " << posspecwall.position << std::endl; - //mov->_velocity = BBM::Vector3f (); - return; auto &pos = entity.getComponent(); const auto &wallPos = wall.getComponent(); auto diff = pos.position + mov->getVelocity() - wallPos.position; -// mov->_velocity = Vector3f(); - if (diff.x <= 0 && mov->_velocity.x < 0) + //mov->_velocity = Vector3f(); + //return; + if (collidedAxis & CollisionComponent::CollidedAxis::X) mov->_velocity.x = 0; - if (diff.x >= 0 && mov->_velocity.x > 0) + if (collidedAxis & CollisionComponent::CollidedAxis::Y) mov->_velocity.x = 0; - if (diff.z <= 0 && mov->_velocity.z < 0) - mov->_velocity.z = 0; - if (diff.z >= 0 && mov->_velocity.z > 0) + if (collidedAxis & CollisionComponent::CollidedAxis::Z) mov->_velocity.z = 0; } @@ -60,7 +57,7 @@ namespace BBM if (!(i % 2) && !(j % 2)) { scene->addEntity("Unbreakable Wall") .addComponent(i, 0, j) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePng)); } } @@ -74,25 +71,25 @@ namespace BBM scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Upper Wall") .addComponent(Vector3f((width + 1) / 2, 0, height + 1)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") .addComponent(Vector3f(width + 1, 0, height / 2)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); scene->addEntity("Right Wall") .addComponent(Vector3f(-1, 0, height / 2)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); @@ -142,7 +139,7 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng)); } @@ -175,7 +172,7 @@ namespace BBM scene->addEntity("Unbreakable Block") .addComponent(coords) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) .addComponent(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePng)); } diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index f6b082a8..f705eebf 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -156,7 +156,7 @@ namespace BBM static const std::string secondFloorHolePath; public: - static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall); + static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall, int collidedAxis); //! @param width Width of the map diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 06ff3cb2..af08e264 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -80,11 +80,11 @@ namespace BBM .addComponent(10, 20, 10) .addComponent(Vector3f(2, 0, 2)); scene->addEntity("cube") - .addComponent(0, 0, 0) + .addComponent(-5, 0, -5) .addComponent(Vector3f(0, 0, 0), Vector3f(3, 3, 3), RED) .addComponent() .addComponent() - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, -1, 3); + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, -1, 3); std::srand(std::time(nullptr)); //MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 50c5662a..14b6c12a 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -14,7 +14,7 @@ namespace BBM : System(wal) { } - bool CollisionSystem::collide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB) + bool CollisionSystem::boxesCollide(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); @@ -28,32 +28,55 @@ namespace BBM auto &posA = entity.get(); auto &colA = entity.get(); Vector3f pointA = posA.position + colA.positionOffset; + Vector3f pointAx; + Vector3f pointAy; + Vector3f pointAz; - if (auto *movable = entity->tryGetComponent()) - pointA += movable->getVelocity(); + if (auto *movable = entity->tryGetComponent()) { + auto vel = movable->getVelocity(); + pointAx.x += vel.x; + pointAy.y += vel.y; + pointAz.z += vel.z; + } - Vector3f minA = Vector3f::min(pointA, pointA + colA.bound); - Vector3f maxA = Vector3f::max(pointA, pointA + colA.bound); + Vector3f minAx = Vector3f::min(pointAx, pointAx + colA.bound); + Vector3f maxAx = Vector3f::max(pointAx, pointAx + colA.bound); + + Vector3f minAy = Vector3f::min(pointAy, pointAy + colA.bound); + Vector3f maxAy = Vector3f::max(pointAy, pointAy + colA.bound); + + Vector3f minAz = Vector3f::min(pointAz, pointAz + colA.bound); + Vector3f maxAz = Vector3f::max(pointAz, pointAz + colA.bound); for (auto &[other, posB, colB] : this->getView()) { if (other.getUid() == entity->getUid()) continue; auto pointB = posB.position + colB.positionOffset; + int collidedAxis = 0; // TODO if B is also a movable we don't check with it's changing position Vector3f minB = Vector3f::min(pointB, pointB + colB.bound); Vector3f maxB = Vector3f::max(pointB, pointB + colB.bound); - if (collide(minA, maxA, minB, maxB)) { - std::cout << "collided" << std::endl + if (boxesCollide(minAx, maxAx, minB, maxB)) { + /* std::cout << "collided" << std::endl << "minA " << minA << std::endl << "maxA " << maxA << std::endl << "minB " << minB << std::endl - << "maxB " << maxB << std::endl; - return; - colA.onCollide(entity, other); - colB.onCollided(entity, other); + << "maxB " << maxB << std::endl;*/ + //return; + collidedAxis += CollisionComponent::CollidedAxis::X; + } + if (boxesCollide(minAy, maxAy, minB, maxB)) { + collidedAxis += CollisionComponent::CollidedAxis::Y; + } + if (boxesCollide(minAz, maxAz, minB, maxB)) { + collidedAxis += CollisionComponent::CollidedAxis::Z; + } + if (collidedAxis) { + colA.onCollide(entity, other, collidedAxis); + colB.onCollided(entity, other, collidedAxis); } } } diff --git a/sources/System/Collision/CollisionSystem.hpp b/sources/System/Collision/CollisionSystem.hpp index 2c0f5491..e1e75ede 100644 --- a/sources/System/Collision/CollisionSystem.hpp +++ b/sources/System/Collision/CollisionSystem.hpp @@ -31,6 +31,6 @@ namespace BBM CollisionSystem &operator=(const CollisionSystem &) = delete; //! @brief check AABB collision - static bool collide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB); + static bool boxesCollide(Vector3f minA, Vector3f maxA, Vector3f minB, Vector3f maxB); }; } \ No newline at end of file From b0292be205d1c1f0a0e217c5db1781260d8e16ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 00:25:14 +0200 Subject: [PATCH 05/18] collisions working as expected fine on debug mode --- sources/System/Collision/CollisionSystem.cpp | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index 14b6c12a..c45f3ca1 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -28,9 +28,9 @@ namespace BBM auto &posA = entity.get(); auto &colA = entity.get(); Vector3f pointA = posA.position + colA.positionOffset; - Vector3f pointAx; - Vector3f pointAy; - Vector3f pointAz; + Vector3f pointAx = pointA; + Vector3f pointAy = pointA; + Vector3f pointAz = pointA; if (auto *movable = entity->tryGetComponent()) { auto vel = movable->getVelocity(); @@ -60,12 +60,6 @@ namespace BBM Vector3f maxB = Vector3f::max(pointB, pointB + colB.bound); if (boxesCollide(minAx, maxAx, minB, maxB)) { - /* std::cout << "collided" << std::endl - << "minA " << minA << std::endl - << "maxA " << maxA << std::endl - << "minB " << minB << std::endl - << "maxB " << maxB << std::endl;*/ - //return; collidedAxis += CollisionComponent::CollidedAxis::X; } if (boxesCollide(minAy, maxAy, minB, maxB)) { From 556021a4ef13368f03122f3c236aab7370fdfc57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 00:35:34 +0200 Subject: [PATCH 06/18] map colliders seems nice --- sources/Component/Collision/CollisionComponent.hpp | 2 +- sources/Map/Map.cpp | 14 +++++++------- sources/Runner/Runner.cpp | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 1be3590d..77f5f429 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -48,7 +48,7 @@ namespace BBM const WAL::Callback &onCollide, const WAL::Callback &onCollided, float positionOffset, - float boundSize = 0); + float boundSize); //! @brief Constructor of collider with no callback CollisionComponent(WAL::Entity &entity, Vector3f positionOffset, Vector3f bound); diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 937343c8..276afb29 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -57,7 +57,7 @@ namespace BBM if (!(i % 2) && !(j % 2)) { scene->addEntity("Unbreakable Wall") .addComponent(i, 0, j) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePng)); } } @@ -71,25 +71,25 @@ namespace BBM scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Upper Wall") .addComponent(Vector3f((width + 1) / 2, 0, height + 1)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") .addComponent(Vector3f(width + 1, 0, height / 2)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); scene->addEntity("Right Wall") .addComponent(Vector3f(-1, 0, height / 2)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); @@ -139,7 +139,7 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) .addComponent(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng)); } @@ -172,7 +172,7 @@ namespace BBM scene->addEntity("Unbreakable Block") .addComponent(coords) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, .75) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) .addComponent(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePng)); } diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index af08e264..39fe9fd1 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -79,14 +79,14 @@ namespace BBM scene->addEntity("camera") .addComponent(10, 20, 10) .addComponent(Vector3f(2, 0, 2)); - scene->addEntity("cube") + /*scene->addEntity("cube") .addComponent(-5, 0, -5) .addComponent(Vector3f(0, 0, 0), Vector3f(3, 3, 3), RED) .addComponent() .addComponent() - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, -1, 3); + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, -1, 3);*/ std::srand(std::time(nullptr)); - //MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); + MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); return scene; } From 3649c8247c319869553506c432f9ba1a71ef2e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 00:47:39 +0200 Subject: [PATCH 07/18] cleanup before PR --- lib/Ray/sources/Camera/Camera3D.cpp | 1 - sources/Runner/Runner.cpp | 4 ++-- sources/System/Renderer/RenderSystem.cpp | 20 -------------------- 3 files changed, 2 insertions(+), 23 deletions(-) diff --git a/lib/Ray/sources/Camera/Camera3D.cpp b/lib/Ray/sources/Camera/Camera3D.cpp index 0c4b3d13..aa2b2a9b 100644 --- a/lib/Ray/sources/Camera/Camera3D.cpp +++ b/lib/Ray/sources/Camera/Camera3D.cpp @@ -10,7 +10,6 @@ RAY::Camera::Camera3D::Camera3D(const RAY::Vector3 &position, const RAY::Vector3 &target, const RAY::Vector3 &up, float fovy, Projection projection): _camera({position, target, up, fovy, projection}) { - SetCameraMode(_camera, CAMERA_FREE); } void RAY::Camera::Camera3D::setPosition(const Vector3 &Position) diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 39fe9fd1..6371c648 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -77,8 +77,8 @@ namespace BBM animation.setAnimIndex(5); }); scene->addEntity("camera") - .addComponent(10, 20, 10) - .addComponent(Vector3f(2, 0, 2)); + .addComponent(8, 20, 7) + .addComponent(Vector3f(8, 0, 8)); /*scene->addEntity("cube") .addComponent(-5, 0, -5) .addComponent(Vector3f(0, 0, 0), Vector3f(3, 3, 3), RED) diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index b6ef7891..7c5cfd9a 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -32,26 +32,6 @@ namespace BBM this->_window.useCamera(this->_camera); for (auto &[_, pos, drawable] : this->_wal.scene->view()) { - if (_.getName() == "cube") { - auto col = _.getComponent(); - DrawCubeWires({pos.position.x, pos.position.y, pos.position.z}, - col.bound.x, - col.bound.y, - col.bound.z, - WHITE); - DrawPoint3D({pos.position.x, pos.position.y, pos.position.z}, BLUE); - DrawPoint3D({pos.position.x + col.bound.x, pos.position.y + col.bound.y, pos.position.z + col.bound.z}, BLUE); - } - if (_.getName() == "player") { - auto col = _.getComponent(); - DrawCubeWires({pos.position.x, pos.position.y, pos.position.z}, - col.bound.x, - col.bound.y, - col.bound.z, - WHITE); - DrawPoint3D({pos.position.x, pos.position.y, pos.position.z}, BLUE); - DrawPoint3D({pos.position.x + col.bound.x, pos.position.y + col.bound.y, pos.position.z + col.bound.z}, BLUE); - } drawable.drawable->setPosition(pos.position); drawable.drawable->drawOn(this->_window); } From ca930db44d29fded1b6cc01c45e54df7385749e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 00:55:32 +0200 Subject: [PATCH 08/18] more cleanup --- sources/Component/Collision/CollisionComponent.hpp | 3 ++- sources/Map/Map.cpp | 8 -------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 77f5f429..02eaf4b9 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -15,6 +15,7 @@ namespace BBM { public: //! @brief Used to tell the collided axis + //! @note Usage: (collidedAxis (int given by callback)) & CollidedAxis::X enum CollidedAxis { X = 1, Y = 2, @@ -53,7 +54,7 @@ namespace BBM //! @brief Constructor of collider with no callback CollisionComponent(WAL::Entity &entity, Vector3f positionOffset, Vector3f bound); - //! @brief Constructor no callback, same boundSize for all axis + //! @brief Constructor no callback, same boundSize & positionOffset for all axis CollisionComponent(WAL::Entity &entity, float positionOffset, float boundSize); //! @brief Default copy constructor diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 276afb29..30a1f21f 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -15,17 +15,9 @@ namespace BBM void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall, int collidedAxis) { auto *mov = entity.tryGetComponent(); - auto posspec = entity.getComponent(); - auto posspecwall = wall.getComponent(); if (!mov) return; - - auto &pos = entity.getComponent(); - const auto &wallPos = wall.getComponent(); - auto diff = pos.position + mov->getVelocity() - wallPos.position; - //mov->_velocity = Vector3f(); - //return; if (collidedAxis & CollisionComponent::CollidedAxis::X) mov->_velocity.x = 0; if (collidedAxis & CollisionComponent::CollidedAxis::Y) From b17c60aa6ce996e2e55ad9c23954a2e24b04df80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 01:00:05 +0200 Subject: [PATCH 09/18] fixing unit tests compil --- tests/CollisionTest.cpp | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/tests/CollisionTest.cpp b/tests/CollisionTest.cpp index 8392aa85..34bff636 100644 --- a/tests/CollisionTest.cpp +++ b/tests/CollisionTest.cpp @@ -8,6 +8,7 @@ #include "Wal.hpp" #define private public + #include "System/Collision/CollisionSystem.hpp" #include "System/Movable/MovableSystem.hpp" #include "Component/Movable/MovableComponent.hpp" @@ -24,14 +25,14 @@ TEST_CASE("Collision test", "[Component][System]") wal.scene = std::make_shared(); wal.scene->addEntity("player") .addComponent() - .addComponent([](Entity &actual, const Entity &) { + .addComponent([](Entity &actual, const Entity &, int _) { try { auto &pos = actual.getComponent(); pos.position.x = 1; pos.position.y = 1; pos.position.z = 1; } catch (std::exception &e) {}; - }, [](Entity &, const Entity &){}, 5.0); + }, [](Entity &, const Entity &, int) {}, 0, 5.0); Entity &entity = wal.scene->getEntities().front(); REQUIRE(entity.getComponent().position == Vector3f()); @@ -44,10 +45,10 @@ TEST_CASE("Collision test", "[Component][System]") 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); + .addComponent(2, 2, 2) + .addComponent(0, 1); Entity &player = wal.scene->getEntities().front(); collision.update(std::chrono::nanoseconds(1)); REQUIRE(player.hasComponent(typeid(PositionComponent))); @@ -68,17 +69,19 @@ TEST_CASE("Collsion test with movable", "[Component][System]") wal.scene = std::make_shared(); wal.scene->addEntity("player") .addComponent() - .addComponent([](Entity &actual, const Entity &) {}, [](Entity &actual, const Entity &) {}, 5.0) + .addComponent([](Entity &actual, const Entity &, int) {}, + [](Entity &actual, const Entity &, int) {}, 0, 5.0) .addComponent(); - + wal.scene->addEntity("block") .addComponent(0, 0, 0) - .addComponent([](Entity &actual, const Entity &){}, [](Entity &actual, const Entity &) { - try { - auto &mov = actual.getComponent(); - mov._velocity = Vector3f(); - } catch (std::exception &e) {}; - }, 1); + .addComponent([](Entity &actual, const Entity &, int) {}, + [](Entity &actual, const Entity &, int) { + try { + auto &mov = actual.getComponent(); + mov._velocity = Vector3f(); + } catch (std::exception &e) {}; + }, 0, 1); Entity &entity = wal.scene->getEntities().front(); REQUIRE(entity.getComponent().position == Vector3f()); From aba85c918e9b9093b9bb9db6521bda48dbfa19b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 01:10:15 +0200 Subject: [PATCH 10/18] adding QoL changes --- sources/Map/Map.cpp | 14 +++++++------- sources/System/Collision/CollisionSystem.cpp | 1 + 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 30a1f21f..ad5b2f09 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -49,7 +49,7 @@ namespace BBM if (!(i % 2) && !(j % 2)) { scene->addEntity("Unbreakable Wall") .addComponent(i, 0, j) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePng)); } } @@ -63,25 +63,25 @@ namespace BBM scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Upper Wall") .addComponent(Vector3f((width + 1) / 2, 0, height + 1)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") .addComponent(Vector3f(width + 1, 0, height / 2)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); scene->addEntity("Right Wall") .addComponent(Vector3f(-1, 0, height / 2)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); @@ -131,7 +131,7 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) .addComponent(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng)); } @@ -164,7 +164,7 @@ namespace BBM scene->addEntity("Unbreakable Block") .addComponent(coords) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0, .90) + .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) .addComponent(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePng)); } diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index c45f3ca1..cbc2c155 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -37,6 +37,7 @@ namespace BBM pointAx.x += vel.x; pointAy.y += vel.y; pointAz.z += vel.z; + pointA += vel; } Vector3f minAx = Vector3f::min(pointAx, pointAx + colA.bound); From 8ac6b7d11fb83bd766e5a5538876af07d7a2fb80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 01:15:50 +0200 Subject: [PATCH 11/18] rm no longer useful statement --- sources/System/Collision/CollisionSystem.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index cbc2c155..c45f3ca1 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -37,7 +37,6 @@ namespace BBM pointAx.x += vel.x; pointAy.y += vel.y; pointAz.z += vel.z; - pointA += vel; } Vector3f minAx = Vector3f::min(pointAx, pointAx + colA.bound); From 23e26b59ae96410f6fd35757b243e1119e03140a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 11:30:40 +0200 Subject: [PATCH 12/18] coding style fix --- sources/System/Renderer/RenderSystem.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sources/System/Renderer/RenderSystem.cpp b/sources/System/Renderer/RenderSystem.cpp index 7c5cfd9a..e735c1e9 100644 --- a/sources/System/Renderer/RenderSystem.cpp +++ b/sources/System/Renderer/RenderSystem.cpp @@ -47,8 +47,8 @@ namespace BBM this->_window.endDrawing(); } - void - RenderSystem::onUpdate(WAL::ViewEntity &entity, std::chrono::nanoseconds dtime) + void RenderSystem::onUpdate(WAL::ViewEntity &entity, + std::chrono::nanoseconds dtime) { const auto &pos = entity.get(); const auto &cam = entity.get(); From d08613a9d41192d2c61fb9c34caba304ccdd6e90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 11:58:30 +0200 Subject: [PATCH 13/18] zoe has to smile ! --- .../Collision/CollisionComponent.cpp | 8 +- .../Collision/CollisionComponent.hpp | 12 +- sources/Map/Map.cpp | 46 +++- sources/Map/Map.hpp | 251 +++++++++--------- sources/System/Collision/CollisionSystem.cpp | 4 +- 5 files changed, 172 insertions(+), 149 deletions(-) diff --git a/sources/Component/Collision/CollisionComponent.cpp b/sources/Component/Collision/CollisionComponent.cpp index 678b0898..652e6727 100644 --- a/sources/Component/Collision/CollisionComponent.cpp +++ b/sources/Component/Collision/CollisionComponent.cpp @@ -16,8 +16,8 @@ namespace BBM } CollisionComponent::CollisionComponent(WAL::Entity &entity, - const WAL::Callback &onCollide, - const WAL::Callback &onCollided, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, Vector3f positionOffset, Vector3f bound) : WAL::Component(entity), @@ -28,8 +28,8 @@ namespace BBM {} CollisionComponent::CollisionComponent(WAL::Entity &entity, - const WAL::Callback &onCollide, - const WAL::Callback &onCollided, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, float positionOffset, float boundSize) : WAL::Component(entity), diff --git a/sources/Component/Collision/CollisionComponent.hpp b/sources/Component/Collision/CollisionComponent.hpp index 02eaf4b9..06878a76 100644 --- a/sources/Component/Collision/CollisionComponent.hpp +++ b/sources/Component/Collision/CollisionComponent.hpp @@ -23,9 +23,9 @@ namespace BBM }; //! @brief onCollide functions to be called - WAL::Callback onCollide; + WAL::Callback onCollide; //! @brief onCollided functions to be called - WAL::Callback onCollided; + WAL::Callback onCollided; //! @brief Bound size on all axis Vector3f bound; //! @brief Offset from the position component @@ -39,15 +39,15 @@ namespace BBM //! @brief Constructor with a WAL::Callback CollisionComponent(WAL::Entity &entity, - const WAL::Callback &onCollide, - const WAL::Callback &onCollided, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, Vector3f positionOffset, Vector3f bound); //! @brief Constructor with a WAL::Callback, same boundSize for all axis CollisionComponent(WAL::Entity &entity, - const WAL::Callback &onCollide, - const WAL::Callback &onCollided, + const WAL::Callback &onCollide, + const WAL::Callback &onCollided, float positionOffset, float boundSize); diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index ad5b2f09..1fd5b0bc 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -12,7 +12,9 @@ namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM { - void MapGenerator::wallCollide(WAL::Entity &entity, const WAL::Entity &wall, int collidedAxis) + void MapGenerator::wallCollide(WAL::Entity &entity, + const WAL::Entity &wall, + CollisionComponent::CollidedAxis collidedAxis) { auto *mov = entity.tryGetComponent(); @@ -49,8 +51,11 @@ namespace BBM if (!(i % 2) && !(j % 2)) { scene->addEntity("Unbreakable Wall") .addComponent(i, 0, j) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) - .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePng)); + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) + .addComponent(unbreakableObj, + std::make_pair(MAP_DIFFUSE, unbreakablePng)); } } } @@ -63,25 +68,33 @@ namespace BBM scene->addEntity("Bottom Wall") .addComponent(Vector3f((width + 1) / 2, 0, -1)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Upper Wall") .addComponent(Vector3f((width + 1) / 2, 0, height + 1)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(width + 3, 1, 1)); scene->addEntity("Left Wall") .addComponent(Vector3f(width + 1, 0, height / 2)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); scene->addEntity("Right Wall") .addComponent(Vector3f(-1, 0, height / 2)) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(unbreakableObj, std::make_pair(MAP_DIFFUSE, unbreakablePnj), RAY::Vector3(1, 1, height + 1)); @@ -97,7 +110,7 @@ namespace BBM if (map[std::make_tuple(i, 0, j)] != HOLE && map[std::make_tuple(i, -1, j)] != BUMPER) scene->addEntity("Unbreakable Wall") .addComponent(Vector3f(i, -1, j)) - .addComponent(floorObj, + .addComponent(floorObj, std::make_pair(MAP_DIFFUSE, floorPng)); } } @@ -131,7 +144,9 @@ namespace BBM scene->addEntity("Breakable Block") .addComponent(coords) .addComponent(1) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(breakableObj, std::make_pair(MAP_DIFFUSE, breakablePng)); } @@ -142,7 +157,7 @@ namespace BBM scene->addEntity("Floor") .addComponent(Vector3f(coords)) - //.addComponent(1) + //.addComponent(1) .addComponent(floorObj, std::make_pair(MAP_DIFFUSE, floorPng)); } @@ -164,7 +179,9 @@ namespace BBM scene->addEntity("Unbreakable Block") .addComponent(coords) - .addComponent(WAL::Callback(), &MapGenerator::wallCollide, 0.25, .75) + .addComponent( + WAL::Callback(), + &MapGenerator::wallCollide, 0.25, .75) .addComponent(UnbreakableObj, std::make_pair(MAP_DIFFUSE, UnbreakablePng)); } @@ -183,7 +200,8 @@ namespace BBM if (coords.y == 0) holeEntity.addComponent(holeObj, std::make_pair(MAP_DIFFUSE, holePng)); else - holeEntity.addComponent(secondFloorObj, std::make_pair(MAP_DIFFUSE, secondFloorPng)); + holeEntity.addComponent(secondFloorObj, + std::make_pair(MAP_DIFFUSE, secondFloorPng)); /*.addComponent([](WAL::Entity &other, const WAL::Entity &entity) { if (other.hasComponent()) { auto &health = other.getComponent(); @@ -215,7 +233,7 @@ namespace BBM scene->addEntity("Stairs Block") .addComponent(coords) - //.addComponent(1) + //.addComponent(1) .addComponent(stairsObj, std::make_pair(MAP_DIFFUSE, stairsPng)); } @@ -328,7 +346,7 @@ namespace BBM return (map); } - void MapGenerator::loadMap(int width, int height, MapBlock map, std::shared_ptr scene) + void MapGenerator::loadMap(int width, int height, MapBlock map, const std::shared_ptr &scene) { generateWall(width, height, scene); generateFloor(map, width, height, scene); diff --git a/sources/Map/Map.hpp b/sources/Map/Map.hpp index f705eebf..82af402a 100644 --- a/sources/Map/Map.hpp +++ b/sources/Map/Map.hpp @@ -27,149 +27,154 @@ namespace BBM class MapGenerator { - private: - //! @brief Enum of the block available. - enum BlockType { - NOTHING, - BREAKABLE, - HOLE, - UPPERFLOOR, - FLOOR, - BUMPER, - STAIRS, - SPAWNER, - UNBREAKABLE - }; + private: + //! @brief Enum of the block available. + enum BlockType + { + NOTHING, + BREAKABLE, + HOLE, + UPPERFLOOR, + FLOOR, + BUMPER, + STAIRS, + SPAWNER, + UNBREAKABLE + }; - using MapElem = std::function scene)>; - using MapBlock = std::map, BlockType>; + using MapElem = std::function scene)>; + using MapBlock = std::map, BlockType>; - //! @brief Generate random block type - static BlockType getRandomBlockType(); + //! @brief Generate random block type + static BlockType getRandomBlockType(); - //! @param map ASCII map - //! @param x x index on the block - //! @param z z index on the block - //! @param blockType blockType to compare with position - static bool isCloseToBlockType(std::map, BlockType> map, int x, int y, int z, BlockType blockType); + //! @param map ASCII map + //! @param x x index on the block + //! @param z z index on the block + //! @param blockType blockType to compare with position + static bool isCloseToBlockType(std::map, BlockType> map, + int x, int y, int z, + BlockType blockType); - //! @param width Width of the map - //! @param height Height of the map - //! @param scene Scene where the map is instanced - //! @brief Generate the unbreakable block of the map - static void generateUnbreakableBlock(int width, int height, std::shared_ptr scene); + //! @param width Width of the map + //! @param height Height of the map + //! @param scene Scene where the map is instanced + //! @brief Generate the unbreakable block of the map + static void generateUnbreakableBlock(int width, int height, std::shared_ptr scene); - //! @param width Width of the map - //! @param height Height of the map - //! @param scene Scene where the map is instanced - //! @brief Generate the wall of the map - static void generateWall(int width, int height, std::shared_ptr scene); + //! @param width Width of the map + //! @param height Height of the map + //! @param scene Scene where the map is instanced + //! @brief Generate the wall of the map + static void generateWall(int width, int height, std::shared_ptr scene); - //! @param width Width of the map - //! @param height Height of the map - //! @param scene Scene where the map is instanced - //! @brief Generate the floor of the map - static void generateFloor(MapBlock map, int width, int height, std::shared_ptr scene); + //! @param width Width of the map + //! @param height Height of the map + //! @param scene Scene where the map is instanced + //! @brief Generate the floor of the map + static void generateFloor(MapBlock map, int width, int height, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create element of the map - static void createElement(Vector3f coords, std::shared_ptr scene, BlockType blockType); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create element of the map + static void createElement(Vector3f coords, std::shared_ptr scene, BlockType blockType); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create breakable of the map - static void createBreakable(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create breakable of the map + static void createBreakable(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create unbreakable of the map - static void createUnbreakable(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create unbreakable of the map + static void createUnbreakable(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create hole of the map - static void createHole(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create hole of the map + static void createHole(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create bumper of the map - static void createBumper(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create bumper of the map + static void createBumper(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create floor of the map - static void createFloor(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create floor of the map + static void createFloor(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create upper floor of the map - static void createUpperFloor(Vector3f coords, std::shared_ptr scene); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create upper floor of the map + static void createUpperFloor(Vector3f coords, std::shared_ptr scene); - //! @param coords coords of the element - //! @param scene Scene where the map is instanced - //! @brief Create stair of the map - static void createStairs(Vector3f coords, std::shared_ptr scene); - - //! @param map Map to load with block declared inside - //! @param width Width of the map - //! @param height Height of the map - //! @brief Generate map of block to be loaded - static MapBlock createSpawner(MapBlock map, int width, int height); + //! @param coords coords of the element + //! @param scene Scene where the map is instanced + //! @brief Create stair of the map + static void createStairs(Vector3f coords, std::shared_ptr scene); - //! @param map Map to load with block declared inside - //! @param width Width of the map - //! @param height Height of the map - //! @brief Generate height for the map - static MapBlock createHeight(MapBlock map, int width, int height); + //! @param map Map to load with block declared inside + //! @param width Width of the map + //! @param height Height of the map + //! @brief Generate map of block to be loaded + static MapBlock createSpawner(MapBlock map, int width, int height); - //! @param map Map to load with block declared inside - //! @param width Width of the map - //! @param height Height of the map - //! @brief Clean breakable on stairs, bumpers, etc.. - static MapBlock cleanBreakable(MapBlock map, int width, int height); + //! @param map Map to load with block declared inside + //! @param width Width of the map + //! @param height Height of the map + //! @brief Generate height for the map + static MapBlock createHeight(MapBlock map, int width, int height); - - static const std::string assetsPath; - - static const std::string wallAssetsPath; - - static const std::string imageExtension; - - static const std::string objExtension; - - static const std::string unbreakableWallPath; - - static const std::string breakableWallPath; - - static const std::string floorPath; - - static const std::string stairsPath; - - static const std::string bumperPath; - - static const std::string secondFloorPath; - - static const std::string holePath; - - static const std::string secondFloorHolePath; - - public: - static void wallCollide(WAL::Entity &entity, const WAL::Entity &wall, int collidedAxis); + //! @param map Map to load with block declared inside + //! @param width Width of the map + //! @param height Height of the map + //! @brief Clean breakable on stairs, bumpers, etc.. + static MapBlock cleanBreakable(MapBlock map, int width, int height); - //! @param width Width of the map - //! @param height Height of the map - //! @brief Generate map of block to be loaded - static MapBlock createMap(int width, int height); + static const std::string assetsPath; + + static const std::string wallAssetsPath; + + static const std::string imageExtension; + + static const std::string objExtension; + + static const std::string unbreakableWallPath; + + static const std::string breakableWallPath; + + static const std::string floorPath; + + static const std::string stairsPath; + + static const std::string bumperPath; + + static const std::string secondFloorPath; + + static const std::string holePath; + + static const std::string secondFloorHolePath; + + public: + static void wallCollide(WAL::Entity &entity, + const WAL::Entity &wall, + CollisionComponent::CollidedAxis collidedAxis); + + + //! @param width Width of the map + //! @param height Height of the map + //! @brief Generate map of block to be loaded + static MapBlock createMap(int width, int height); + + //! @param width Width of the map + //! @param height Height of the map + //! @param map Map to load with block declared inside + //! @param scene Scene where the map is instanced + //! @brief Generate the map + static void loadMap(int width, int height, MapBlock map, const std::shared_ptr &scene); - //! @param width Width of the map - //! @param height Height of the map - //! @param map Map to load with block declared inside - //! @param scene Scene where the map is instanced - //! @brief Generate the map - static void loadMap(int width, int height, MapBlock map, std::shared_ptr scene); - }; } // namespace BBM \ No newline at end of file diff --git a/sources/System/Collision/CollisionSystem.cpp b/sources/System/Collision/CollisionSystem.cpp index c45f3ca1..db68e3c0 100644 --- a/sources/System/Collision/CollisionSystem.cpp +++ b/sources/System/Collision/CollisionSystem.cpp @@ -69,8 +69,8 @@ namespace BBM collidedAxis += CollisionComponent::CollidedAxis::Z; } if (collidedAxis) { - colA.onCollide(entity, other, collidedAxis); - colB.onCollided(entity, other, collidedAxis); + colA.onCollide(entity, other, static_cast(collidedAxis)); + colB.onCollided(entity, other, static_cast(collidedAxis)); } } } From 395566598cd6d834366d87df00fc793ccad1fd49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 12:02:56 +0200 Subject: [PATCH 14/18] last fixes --- sources/Map/Map.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sources/Map/Map.cpp b/sources/Map/Map.cpp index 1fd5b0bc..3f8ba259 100644 --- a/sources/Map/Map.cpp +++ b/sources/Map/Map.cpp @@ -13,7 +13,7 @@ namespace RAY3D = RAY::Drawables::Drawables3D; namespace BBM { void MapGenerator::wallCollide(WAL::Entity &entity, - const WAL::Entity &wall, + const WAL::Entity &wall, CollisionComponent::CollidedAxis collidedAxis) { auto *mov = entity.tryGetComponent(); @@ -157,7 +157,7 @@ namespace BBM scene->addEntity("Floor") .addComponent(Vector3f(coords)) - //.addComponent(1) + //.addComponent(1) .addComponent(floorObj, std::make_pair(MAP_DIFFUSE, floorPng)); } @@ -233,7 +233,7 @@ namespace BBM scene->addEntity("Stairs Block") .addComponent(coords) - //.addComponent(1) + //.addComponent(1) .addComponent(stairsObj, std::make_pair(MAP_DIFFUSE, stairsPng)); } From af7f972d30759fd7977866f752a481ec5c36428f Mon Sep 17 00:00:00 2001 From: Zoe Roux Date: Tue, 8 Jun 2021 14:28:46 +0200 Subject: [PATCH 15/18] Fixing has component with disabled ones --- lib/wal/sources/Entity/Entity.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/wal/sources/Entity/Entity.cpp b/lib/wal/sources/Entity/Entity.cpp index e4e07453..8e4ff0f1 100644 --- a/lib/wal/sources/Entity/Entity.cpp +++ b/lib/wal/sources/Entity/Entity.cpp @@ -70,7 +70,9 @@ namespace WAL auto cmp = this->_components.find(type); if (cmp == this->_components.end()) return false; - return !cmp->second->isDisabled(); + if (skipDisabled) + return !cmp->second->isDisabled(); + return true; } void Entity::_componentAdded(const std::type_index &type) From afc945c1748156b472df9d90da6f6385bd429db9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 16:23:29 +0200 Subject: [PATCH 16/18] setting player's hitbox on his head too --- sources/Runner/Runner.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 08a51d76..d6a20b6d 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -78,7 +78,7 @@ namespace BBM .addComponent() .addComponent() .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) - .addComponent(0, 1) + .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) .addComponent() .addComponent() .addComponent(1, [](WAL::Entity &entity) { From 44afd5cd44eeddfab26cab4afddf8446032321ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 18:27:12 +0200 Subject: [PATCH 17/18] adding joystick support for gamepad controller --- lib/Ray/sources/Controllers/Gamepad.cpp | 5 +++++ lib/Ray/sources/Controllers/Gamepad.hpp | 4 ++++ sources/Component/Gamepad/GamepadComponent.hpp | 6 ++++++ sources/Runner/Runner.cpp | 4 +++- sources/System/Gamepad/GamepadSystem.cpp | 11 ++++++----- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/Ray/sources/Controllers/Gamepad.cpp b/lib/Ray/sources/Controllers/Gamepad.cpp index b51bcc95..7845411e 100644 --- a/lib/Ray/sources/Controllers/Gamepad.cpp +++ b/lib/Ray/sources/Controllers/Gamepad.cpp @@ -42,3 +42,8 @@ void RAY::Controller::GamePad::setID(int id) { this->_id = id; } + +float RAY::Controller::GamePad::getAxisValue(int index) +{ + return GetGamepadAxisMovement(this->_id, index); +} \ No newline at end of file diff --git a/lib/Ray/sources/Controllers/Gamepad.hpp b/lib/Ray/sources/Controllers/Gamepad.hpp index 893c674f..9f17acc8 100644 --- a/lib/Ray/sources/Controllers/Gamepad.hpp +++ b/lib/Ray/sources/Controllers/Gamepad.hpp @@ -17,6 +17,7 @@ namespace RAY::Controller { class GamePad { public: typedef ::GamepadButton Button; + typedef ::GamepadAxis Axis; //! @brief A default constructor //! @param The id of the controller @@ -44,6 +45,9 @@ namespace RAY::Controller { //! @param Button The keycode of the button bool isReleased(Button); + //! @brief Get the value of an axis + float getAxisValue(int index); + //! @brief Returns true if Button is up on the gamepad //! @param Button The keycode of the button bool isUp(Button); diff --git a/sources/Component/Gamepad/GamepadComponent.hpp b/sources/Component/Gamepad/GamepadComponent.hpp index be00a49b..3825645d 100644 --- a/sources/Component/Gamepad/GamepadComponent.hpp +++ b/sources/Component/Gamepad/GamepadComponent.hpp @@ -10,6 +10,7 @@ #include "Entity/Entity.hpp" using Button = RAY::Controller::GamePad::Button; +using Axis = RAY::Controller::GamePad::Axis; using Gamepad = RAY::Controller::GamePad; namespace BBM @@ -35,6 +36,11 @@ namespace BBM //! @brief move down key Button keyDown = GAMEPAD_BUTTON_LEFT_FACE_DOWN; + Axis LeftStickX = GAMEPAD_AXIS_LEFT_X; + Axis LeftStickY = GAMEPAD_AXIS_LEFT_Y; + Axis RightStickX = GAMEPAD_AXIS_RIGHT_X; + Axis RightStickY = GAMEPAD_AXIS_RIGHT_Y; + //! @inherit WAL::Component *clone(WAL::Entity &entity) const override; diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index d6a20b6d..6a047352 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -76,7 +76,8 @@ namespace BBM .addComponent("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() .addComponent() - .addComponent() + //.addComponent() + .addComponent(0) .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) .addComponent() @@ -96,6 +97,7 @@ namespace BBM .addComponent(WAL::Callback(), &MapGenerator::wallCollide, -1, 3);*/ std::srand(std::time(nullptr)); MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene); + return scene; } diff --git a/sources/System/Gamepad/GamepadSystem.cpp b/sources/System/Gamepad/GamepadSystem.cpp index f6d67d99..ee4b8767 100644 --- a/sources/System/Gamepad/GamepadSystem.cpp +++ b/sources/System/Gamepad/GamepadSystem.cpp @@ -31,10 +31,11 @@ namespace BBM for (auto key : keyPressedMap) key.second = gamepad.isPressed(key.first); - controllable.move = Vector2f(); - controllable.move.x += gamepad.isPressed(gamepadComponent.keyRight); - controllable.move.x -= gamepad.isPressed(gamepadComponent.keyLeft); - controllable.move.y += gamepad.isPressed(gamepadComponent.keyUp); - controllable.move.y -= gamepad.isPressed(gamepadComponent.keyDown); + controllable.move.x = gamepad.getAxisValue(gamepadComponent.LeftStickX) * -1; + controllable.move.y = gamepad.getAxisValue(gamepadComponent.LeftStickY) * -1; + controllable.move.x -= gamepad.isDown(gamepadComponent.keyRight); + controllable.move.x += gamepad.isDown(gamepadComponent.keyLeft); + controllable.move.y += gamepad.isDown(gamepadComponent.keyUp); + controllable.move.y -= gamepad.isDown(gamepadComponent.keyDown); } } \ No newline at end of file From efacfb53e37087587522f857ca3b1bf92c60c800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Tue, 8 Jun 2021 18:37:57 +0200 Subject: [PATCH 18/18] fixing keyboard (he was switching between a and d) --- sources/Component/Keyboard/KeyboardComponent.hpp | 4 ++-- sources/Runner/Runner.cpp | 4 ++-- sources/System/Keyboard/KeyboardSystem.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sources/Component/Keyboard/KeyboardComponent.hpp b/sources/Component/Keyboard/KeyboardComponent.hpp index bd0f2ab7..2ad2e6f2 100644 --- a/sources/Component/Keyboard/KeyboardComponent.hpp +++ b/sources/Component/Keyboard/KeyboardComponent.hpp @@ -23,9 +23,9 @@ namespace BBM //! @brief pause key Key keyPause = KEY_ESCAPE; //! @brief move right key - Key keyRight = KEY_A; + Key keyRight = KEY_D; //! @brief move left key - Key keyLeft = KEY_D; + Key keyLeft = KEY_A; //! @brief move up key Key keyUp = KEY_W; //! @brief move down key diff --git a/sources/Runner/Runner.cpp b/sources/Runner/Runner.cpp index 6a047352..591ce261 100644 --- a/sources/Runner/Runner.cpp +++ b/sources/Runner/Runner.cpp @@ -76,8 +76,8 @@ namespace BBM .addComponent("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png")) .addComponent() .addComponent() - //.addComponent() - .addComponent(0) + .addComponent() + //.addComponent(0) .addComponent(RAY::ModelAnimations("assets/player/player.iqm"), 3) .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) .addComponent() diff --git a/sources/System/Keyboard/KeyboardSystem.cpp b/sources/System/Keyboard/KeyboardSystem.cpp index 740b9e3a..0f2a0cef 100644 --- a/sources/System/Keyboard/KeyboardSystem.cpp +++ b/sources/System/Keyboard/KeyboardSystem.cpp @@ -31,9 +31,9 @@ namespace BBM key.second = Keyboard::isPressed(key.first); controllable.move = Vector2f(); if (Keyboard::isDown(keyboard.keyRight)) - controllable.move.x += 1; - if (Keyboard::isDown(keyboard.keyLeft)) controllable.move.x -= 1; + if (Keyboard::isDown(keyboard.keyLeft)) + controllable.move.x += 1; if (Keyboard::isDown(keyboard.keyUp)) controllable.move.y += 1; if (Keyboard::isDown(keyboard.keyDown))