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] 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();