diff --git a/Escape-the-Dawn/GameWorld.cpp b/Escape-the-Dawn/GameWorld.cpp index 8e9fb82..1b38ff7 100644 --- a/Escape-the-Dawn/GameWorld.cpp +++ b/Escape-the-Dawn/GameWorld.cpp @@ -93,7 +93,7 @@ void GameWorld::Initialize() m_Player = CreateEntity(); SetProperty(m_Player, "Name", std::string("PlayerShip")); transform = AddComponent(m_Player, "Transform"); - transform->Position = glm::vec3(0.f, 4.f, 0.f); + transform->Position = glm::vec3(0.f, 4.f, -5.f); transform->Scale = glm::vec3(2.0f); model = AddComponent(m_Player, "Model"); model->ModelFile = "ship.obj"; @@ -103,7 +103,6 @@ void GameWorld::Initialize() bounds = AddComponent(m_Player, "Bounds"); bounds->Origin = glm::vec3(transform->Position.x, transform->Position.y - 4, transform->Position.z + 3); bounds->VolumeVector = glm::vec3(4.f,0.7f,4); - //bounds->VolumeVector = glm::vec3(1.f,0.7f,1.f); player2 = CreateEntity(); @@ -121,8 +120,8 @@ void GameWorld::Initialize() void GameWorld::Update(double dt) { World::Update(dt); - if(GetSystem("CollisionSystem")->Intersects(m_Player, entcamera)) - assert(false); + /*if(GetSystem("CollisionSystem")->Intersects(m_Player, entcamera)) + assert(false);*/ } void GameWorld::RegisterComponents() diff --git a/Escape-the-Dawn/Systems/CollisionSystem.cpp b/Escape-the-Dawn/Systems/CollisionSystem.cpp index 5b8ec97..8cd9c53 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.cpp +++ b/Escape-the-Dawn/Systems/CollisionSystem.cpp @@ -13,46 +13,73 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID transform->Position[2] += parentTransform->Position[2]; } LOG_INFO("Updating entity %i with parent %i", entity, parent); + + auto entities = m_World->GetEntities(); + + for (auto pair : *entities) { + EntityID entity2 = pair.first; + EntityID parent2 = pair.second; + + // Check if entity2 is a child of entity + /*auto currentParent = parent; + bool childOfEntity2 = false; + while (currentParent != 0) { + if (currentParent == entity2) { + childOfEntity2 = true; + break; + } + currentParent = entities[currentParent]; + } + if (childOfEntity2) + continue;*/ + + // Check if we're the parent to entity2 + + //pair.first, pair.second; + if(entity == entity2) + break; + Intersects(entity, entity2); + } } -bool Systems::CollisionSystem::Intersects(EntityID aEntity, EntityID bEntity) +void Systems::CollisionSystem::Intersects(EntityID aEntity, EntityID bEntity) { auto aTransform = m_World->GetComponent(aEntity, "Transform"); if(aTransform == nullptr) - return false; + return; auto bTransform = m_World->GetComponent(bEntity, "Transform"); if(bTransform == nullptr) - return false; + return; auto aCollisionComponent = m_World->GetComponent(aEntity, "Collision"); if(aCollisionComponent == nullptr) - return false; + return; auto bCollisionComponent = m_World->GetComponent(bEntity, "Collision"); if(bCollisionComponent == nullptr) - return false; - aCollisionComponent->CollidingEntities.clear(); - bCollisionComponent->CollidingEntities.clear(); + return; + auto aBounds = m_World->GetComponent(aEntity, "Bounds"); auto bBounds = m_World->GetComponent(bEntity, "Bounds"); - glm::vec3 aMax = aTransform->Position + aBounds->Origin + aBounds->VolumeVector; - glm::vec3 aMin = aTransform->Position + aBounds->Origin - aBounds->VolumeVector; - glm::vec3 bMax = bTransform->Position + bBounds->Origin + bBounds->VolumeVector; - glm::vec3 bMin = bTransform->Position + bBounds->Origin - bBounds->VolumeVector; + glm::vec3 aPos = aTransform->Position + (aBounds->Origin * aTransform->Scale); + glm::vec3 bPos = bTransform->Position + (bBounds->Origin * bTransform->Scale); + + glm::vec3 aMax = aPos + aBounds->VolumeVector * aTransform->Scale; + glm::vec3 aMin = aPos - aBounds->VolumeVector * aTransform->Scale; + glm::vec3 bMax = bPos + bBounds->VolumeVector * bTransform->Scale; + glm::vec3 bMin = bPos - bBounds->VolumeVector * bTransform->Scale; if (aMin.x <= bMax.x && bMin.x <= aMax.x) { if (aMin.y <= bMax.y && bMin.y <= aMax.y) { if (aMin.z <= bMax.z && bMin.z <= aMax.z) { aCollisionComponent->CollidingEntities.push_back(aEntity); bCollisionComponent->CollidingEntities.push_back(bEntity); - return true; + return; } } } - - return false; } void Systems::CollisionSystem::CreateBoundingBox(std::shared_ptr bounds) diff --git a/Escape-the-Dawn/Systems/CollisionSystem.h b/Escape-the-Dawn/Systems/CollisionSystem.h index b094f9f..c4494b9 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.h +++ b/Escape-the-Dawn/Systems/CollisionSystem.h @@ -19,7 +19,7 @@ public: : System(world) { } void UpdateEntity(double dt, EntityID entity, EntityID parent) override; - bool Intersects(EntityID, EntityID); + void Intersects(EntityID, EntityID); void CreateBoundingBox(std::shared_ptr); }; diff --git a/Escape-the-Dawn/Systems/PlayerSystem.cpp b/Escape-the-Dawn/Systems/PlayerSystem.cpp index 361f33a..5894dc9 100644 --- a/Escape-the-Dawn/Systems/PlayerSystem.cpp +++ b/Escape-the-Dawn/Systems/PlayerSystem.cpp @@ -60,8 +60,15 @@ void Systems::PlayerSystem::UpdateEntity(double dt, EntityID entity, EntityID pa //--------------------------------------------------------------------- // TOUCHING THIS CODE MIGHT COUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS } + + auto collision = m_World->GetComponent(entity, "Collision"); + if(collision->CollidingEntities.size() != 0) + int c = 1; // lol } + + + name = m_World->GetProperty(entity, "Name"); if (name == "PlayerShip") { diff --git a/Escape-the-Dawn/Systems/PlayerSystem.h b/Escape-the-Dawn/Systems/PlayerSystem.h index 6d995e0..f0dd0db 100644 --- a/Escape-the-Dawn/Systems/PlayerSystem.h +++ b/Escape-the-Dawn/Systems/PlayerSystem.h @@ -6,8 +6,11 @@ #include "System.h" #include "Components/Transform.h" #include "Components/Input.h" +#include "Components/Collision.h" #include "logging.h" + + namespace Systems { diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index 009c768..8db9a3e 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -68,6 +68,8 @@ public: // Recursively update through the scene graph void RecursiveUpdate(std::shared_ptr system, double dt, EntityID parentEntity); + std::unordered_map* GetEntities() { return &m_EntityParents; } + protected: SystemFactory m_SystemFactory; ComponentFactory m_ComponentFactory; @@ -77,8 +79,7 @@ protected: EntityID m_LastEntityID; std::stack m_RecycledEntityIDs; // A bottom to top tree. A map of child entities to parent entities. - std::unordered_map m_EntityParents; - + std::unordered_map m_EntityParents ; std::unordered_map> m_EntityProperties; std::unordered_map>> m_ComponentsOfType;