From 4459f671d5bd8bf7a72e84ce18742a036f760225 Mon Sep 17 00:00:00 2001 From: Adam Date: Wed, 12 Mar 2014 21:30:09 +0100 Subject: [PATCH 1/4] collisionz --- Escape-the-Dawn/GameWorld.h | 9 +++-- Escape-the-Dawn/Systems/CollisionSystem.cpp | 43 +++++++++++++++++++++ Escape-the-Dawn/Systems/CollisionSystem.h | 4 ++ Escape-the-Dawn/Systems/SoundSystem.h | 8 ++-- Escape-the-Dawn/Systems/SoundsSystem.cpp | 12 +++--- 5 files changed, 62 insertions(+), 14 deletions(-) diff --git a/Escape-the-Dawn/GameWorld.h b/Escape-the-Dawn/GameWorld.h index 1628205..7b87636 100644 --- a/Escape-the-Dawn/GameWorld.h +++ b/Escape-the-Dawn/GameWorld.h @@ -54,7 +54,7 @@ void GameWorld::Initialize() //AddSystem("LevelGenerationSystem"); AddSystem("InputSystem"); - //AddSystem("CollisionSystem"); + AddSystem("CollisionSystem"); //AddSystem("ParticleSystem"); AddSystem("PlayerSystem"); AddSystem("SoundSystem"); @@ -75,9 +75,12 @@ void GameWorld::Initialize() soundEmitter->MaxDistance = FLT_MAX; soundEmitter->ReferenceDistance = 10; soundEmitter->Loop = true; - soundEmitter->Pitch = 1; GetSystem("SoundSystem")->PlaySound(soundEmitter, "Sounds/hallelujah.wav"); + auto aabb = AddComponent(ent, "Bounds"); + aabb->Origin = transform->Position; + aabb->VolumeVector = glm::vec3(30.f, 20.f, 50.f); + GetSystem("CollisionSystem")->Intersects(ent, ent); ent = CreateEntity(); @@ -96,7 +99,7 @@ void GameWorld::RegisterSystems() { //m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); }); m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_Renderer); }); - //m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); + m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); //m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); }); m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); }); diff --git a/Escape-the-Dawn/Systems/CollisionSystem.cpp b/Escape-the-Dawn/Systems/CollisionSystem.cpp index f9c5a8a..004f393 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.cpp +++ b/Escape-the-Dawn/Systems/CollisionSystem.cpp @@ -14,3 +14,46 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID } LOG_INFO("Updating entity %i with parent %i", entity, parent); } + +bool Systems::CollisionSystem::Intersects(EntityID entity1, EntityID entity2) +{ + auto bounds1 = m_World->GetComponent(entity1, "Bounds"); + auto bounds2 = m_World->GetComponent(entity2, "Bounds"); + + float width = bounds1->VolumeVector.x; + float height = bounds1->VolumeVector.y; + float depth = bounds1->VolumeVector.z; + + GLfloat vertices1[] = { + bounds1->Origin.x - width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z - depth / 2, 1.0, + bounds1->Origin.x + width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z - depth / 2, 1.0, + bounds1->Origin.x + width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z - depth / 2, 1.0, + bounds1->Origin.x - width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z - depth / 2, 1.0, + bounds1->Origin.x - width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z + depth / 2, 1.0, + bounds1->Origin.x + width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z + depth / 2, 1.0, + bounds1->Origin.x + width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z + depth / 2, 1.0, + bounds1->Origin.x - width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z + depth / 2, 1.0, + }; + + GLuint vbo_vertices; + glGenBuffers(1, &vbo_vertices); + glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); + + GLushort elements[] = { + 0, 1, 2, 3, + 4, 5, 6, 7, + 0, 4, 1, 5, + 2, 6, 3, 7 + }; + GLuint ibo_elements; + glGenBuffers(1, &ibo_elements); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); + + + + return true; +} \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/CollisionSystem.h b/Escape-the-Dawn/Systems/CollisionSystem.h index 5c4c448..a4794ba 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.h +++ b/Escape-the-Dawn/Systems/CollisionSystem.h @@ -3,8 +3,11 @@ #include "System.h" #include "logging.h" +#include #include "Components/Transform.h" +#include "Components/Collision.h" +#include "Components/Bounds.h" namespace Systems { @@ -16,6 +19,7 @@ public: : System(world) { } void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + bool Intersects(EntityID, EntityID); }; } diff --git a/Escape-the-Dawn/Systems/SoundSystem.h b/Escape-the-Dawn/Systems/SoundSystem.h index 7284ad1..4a27d0a 100644 --- a/Escape-the-Dawn/Systems/SoundSystem.h +++ b/Escape-the-Dawn/Systems/SoundSystem.h @@ -6,9 +6,7 @@ #include "Components/SoundEmitter.h" #include #include -#include #include -#include #include namespace Systems @@ -30,11 +28,11 @@ private: //File-info char type[4]; - DWORD size, chunkSize; + unsigned long size, chunkSize; short formatType, channels; - DWORD sampleRate, avgBytesPerSec; + unsigned long sampleRate, avgBytesPerSec; short bytesPerSample, bitsPerSample; - DWORD dataSize; + unsigned long dataSize; std::map m_Source; diff --git a/Escape-the-Dawn/Systems/SoundsSystem.cpp b/Escape-the-Dawn/Systems/SoundsSystem.cpp index d35e322..88789c7 100644 --- a/Escape-the-Dawn/Systems/SoundsSystem.cpp +++ b/Escape-the-Dawn/Systems/SoundsSystem.cpp @@ -103,7 +103,7 @@ ALuint Systems::SoundSystem::LoadFile(std::string fileName) return 0; } - fread(&size, sizeof(DWORD), 1, fp); + fread(&size, sizeof(unsigned long), 1, fp); fread(type, sizeof(char), 4, fp); if(type[0]!='W' || type[1]!='A' || type[2]!='V' || type[3]!='E') { LOG_ERROR("ERROR: Not WAVE-file"); @@ -117,11 +117,11 @@ ALuint Systems::SoundSystem::LoadFile(std::string fileName) } //READ THE DATA FROM WAVE-FILE - fread(&chunkSize, sizeof(DWORD), 1, fp); + fread(&chunkSize, sizeof(unsigned long), 1, fp); fread(&formatType, sizeof(short), 1, fp); fread(&channels, sizeof(short), 1, fp); - fread(&sampleRate, sizeof(DWORD), 1, fp); - fread(&avgBytesPerSec, sizeof(DWORD), 1, fp); + fread(&sampleRate, sizeof(unsigned long), 1, fp); + fread(&avgBytesPerSec, sizeof(unsigned long), 1, fp); fread(&bytesPerSample, sizeof(short), 1, fp); fread(&bitsPerSample, sizeof(short), 1, fp); @@ -132,10 +132,10 @@ ALuint Systems::SoundSystem::LoadFile(std::string fileName) return 0; } - fread(&dataSize, sizeof(DWORD), 1, fp); + fread(&dataSize, sizeof(unsigned long), 1, fp); unsigned char* buf = new unsigned char[dataSize]; - fread(buf, sizeof(BYTE), dataSize, fp); + fread(buf, sizeof(unsigned char), dataSize, fp); fclose(fp); // Create buffer From 2beaa227585b2fea6a1a8859c1e28dfe0f2f1d87 Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 13 Mar 2014 00:29:47 +0100 Subject: [PATCH 2/4] moar collisionz --- Escape-the-Dawn/GameWorld.cpp | 46 ++++++++++---- Escape-the-Dawn/GameWorld.h | 1 + Escape-the-Dawn/Systems/CollisionSystem.cpp | 67 +++++++++++++++------ Escape-the-Dawn/Systems/CollisionSystem.h | 1 + 4 files changed, 85 insertions(+), 30 deletions(-) diff --git a/Escape-the-Dawn/GameWorld.cpp b/Escape-the-Dawn/GameWorld.cpp index 5d3711a..0e8075a 100644 --- a/Escape-the-Dawn/GameWorld.cpp +++ b/Escape-the-Dawn/GameWorld.cpp @@ -7,7 +7,7 @@ void GameWorld::Initialize() //AddSystem("LevelGenerationSystem"); AddSystem("InputSystem"); - //AddSystem("CollisionSystem"); + AddSystem("CollisionSystem"); //AddSystem("ParticleSystem"); AddSystem("PlayerSystem"); AddSystem("SoundSystem"); @@ -17,19 +17,26 @@ void GameWorld::Initialize() std::shared_ptr model; std::shared_ptr pointLight; std::shared_ptr camera; + std::shared_ptr bounds; + std::shared_ptr collision; EntityID ent; // Camera - ent = CreateEntity(); - SetProperty(ent, "Name", std::string("Camera")); - transform = AddComponent(ent, "Transform"); - AddComponent(ent, "Input"); + entcamera = CreateEntity(); + SetProperty(entcamera, "Name", std::string("Camera")); + transform = AddComponent(entcamera, "Transform"); + AddComponent(entcamera, "Input"); transform->Position = glm::vec3(0.f, 2.f, 10.f); - camera = AddComponent(ent, "Camera"); + camera = AddComponent(entcamera, "Camera"); camera->FOV = 45.f; camera->FarClip = 1000.f; camera->NearClip = 0.01f; transform->Orientation = glm::angleAxis(glm::radians(25.0f),glm::vec3(1,0,0)); + collision = AddComponent(entcamera, "Collision"); + collision->Phantom = false; + bounds = AddComponent(entcamera, "Bounds"); + bounds->Origin = transform->Position; + bounds->VolumeVector = glm::vec3(4.f,2.f,2.f); // Fucking lights ent = CreateEntity(); @@ -84,22 +91,35 @@ void GameWorld::Initialize() model->ModelFile = "plane.obj"; // Player - ent = CreateEntity(); - transform = AddComponent(ent, "Transform"); + player1 = CreateEntity(); + transform = AddComponent(player1, "Transform"); transform->Position = glm::vec3(0.f, 0.f, 0.f); - model = AddComponent(ent, "Model"); + model = AddComponent(player1, "Model"); model->ModelFile = "ship.obj"; + collision = AddComponent(player1, "Collision"); + collision->Phantom = false; + bounds = AddComponent(player1, "Bounds"); + bounds->Origin = transform->Position; + bounds->VolumeVector = glm::vec3(10.f,10.f,5.f); - ent = CreateEntity(); - transform = AddComponent(ent, "Transform"); + + player2 = CreateEntity(); + transform = AddComponent(player2, "Transform"); transform->Position = glm::vec3(10.f, 0.f, 0.f); - model = AddComponent(ent, "Model"); + model = AddComponent(player2, "Model"); model->ModelFile = "ship.obj"; + collision = AddComponent(player2, "Collision"); + collision->Phantom = false; + bounds = AddComponent(player2, "Bounds"); + bounds->Origin = transform->Position; + bounds->VolumeVector = glm::vec3(2.f,2.f,2.f); } void GameWorld::Update(double dt) { World::Update(dt); + if(GetSystem("CollisionSystem")->Intersects(player1, entcamera)) + assert(false); } void GameWorld::RegisterComponents() @@ -124,7 +144,7 @@ void GameWorld::RegisterSystems() { //m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); }); m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_Renderer); }); - //m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); + m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); //m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); }); m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); m_SystemFactory.Register("SoundSystem", [this]() { return new Systems::SoundSystem(this); }); diff --git a/Escape-the-Dawn/GameWorld.h b/Escape-the-Dawn/GameWorld.h index 49ff95a..7fda5df 100644 --- a/Escape-the-Dawn/GameWorld.h +++ b/Escape-the-Dawn/GameWorld.h @@ -42,6 +42,7 @@ public: private: std::shared_ptr m_Renderer; + EntityID entcamera, player1, player2; }; #endif // GameWorld_h__ diff --git a/Escape-the-Dawn/Systems/CollisionSystem.cpp b/Escape-the-Dawn/Systems/CollisionSystem.cpp index 004f393..167a3b7 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.cpp +++ b/Escape-the-Dawn/Systems/CollisionSystem.cpp @@ -17,28 +17,65 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID bool Systems::CollisionSystem::Intersects(EntityID entity1, EntityID entity2) { + auto collisionComponent1 = m_World->GetComponent(entity1, "Collision"); + if(collisionComponent1 == nullptr) + return false; + auto collisionComponent2 = m_World->GetComponent(entity2, "Collision"); + if(collisionComponent2 == nullptr) + return false; + collisionComponent1->CollidingEntities.clear(); + collisionComponent2->CollidingEntities.clear(); + auto bounds1 = m_World->GetComponent(entity1, "Bounds"); + auto bounds2 = m_World->GetComponent(entity2, "Bounds"); + - float width = bounds1->VolumeVector.x; - float height = bounds1->VolumeVector.y; - float depth = bounds1->VolumeVector.z; + glm::vec3 max1 = glm::vec3(bounds1->Origin.x + bounds1->VolumeVector.x, bounds1->Origin.y + bounds1->VolumeVector.y, bounds1->Origin.z + bounds1->VolumeVector.z); + glm::vec3 min1 = glm::vec3(bounds1->Origin.x - bounds1->VolumeVector.x, bounds1->Origin.y - bounds1->VolumeVector.y, bounds1->Origin.z - bounds1->VolumeVector.z); + glm::vec3 max2 = glm::vec3(bounds2->Origin.x + bounds2->VolumeVector.x, bounds2->Origin.y + bounds2->VolumeVector.y, bounds2->Origin.z + bounds2->VolumeVector.z); + glm::vec3 min2 = glm::vec3(bounds2->Origin.x - bounds2->VolumeVector.x, bounds2->Origin.y - bounds2->VolumeVector.y, bounds2->Origin.z - bounds2->VolumeVector.z); - GLfloat vertices1[] = { - bounds1->Origin.x - width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z - depth / 2, 1.0, - bounds1->Origin.x + width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z - depth / 2, 1.0, - bounds1->Origin.x + width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z - depth / 2, 1.0, - bounds1->Origin.x - width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z - depth / 2, 1.0, - bounds1->Origin.x - width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z + depth / 2, 1.0, - bounds1->Origin.x + width / 2, bounds1->Origin.y - height / 2, bounds1->Origin.z + depth / 2, 1.0, - bounds1->Origin.x + width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z + depth / 2, 1.0, - bounds1->Origin.x - width / 2, bounds1->Origin.y + height / 2, bounds1->Origin.z + depth / 2, 1.0, + + + for(int z = min1.z; z < max1.z; z++) + if (z > min2.z && z < max2.z) + for (int y = min1.y; y < max1.y; y++) + if(y > min2.y && y < max2.y) + for (int x = min1.x; x < max1.x; x++) + if(x > min2.x && x < max2.x) + { + //vi har en kollision!!! kanske :( + collisionComponent1->CollidingEntities.push_back(entity2); + collisionComponent2->CollidingEntities.push_back(entity1); + return true; + } + + + return false; +} + +void Systems::CollisionSystem::CreateBoundingBox(std::shared_ptr bounds) +{ + float width = abs(bounds->VolumeVector.x); + float height = abs(bounds->VolumeVector.y); + float depth = abs(bounds->VolumeVector.z); + + GLfloat vertices[] = { + bounds->Origin.x - width, bounds->Origin.y - height, bounds->Origin.z - depth, 1.0, + bounds->Origin.x + width, bounds->Origin.y - height, bounds->Origin.z - depth, 1.0, + bounds->Origin.x + width, bounds->Origin.y + height, bounds->Origin.z - depth, 1.0, + bounds->Origin.x - width, bounds->Origin.y + height, bounds->Origin.z - depth, 1.0, + bounds->Origin.x - width, bounds->Origin.y - height, bounds->Origin.z + depth, 1.0, + bounds->Origin.x + width, bounds->Origin.y - height, bounds->Origin.z + depth, 1.0, + bounds->Origin.x + width, bounds->Origin.y + height, bounds->Origin.z + depth, 1.0, + bounds->Origin.x - width, bounds->Origin.y + height, bounds->Origin.z + depth, 1.0, }; GLuint vbo_vertices; glGenBuffers(1, &vbo_vertices); glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices1), vertices1, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); GLushort elements[] = { @@ -52,8 +89,4 @@ bool Systems::CollisionSystem::Intersects(EntityID entity1, EntityID entity2) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); - - - - return true; } \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/CollisionSystem.h b/Escape-the-Dawn/Systems/CollisionSystem.h index a4794ba..b094f9f 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.h +++ b/Escape-the-Dawn/Systems/CollisionSystem.h @@ -20,6 +20,7 @@ public: void UpdateEntity(double dt, EntityID entity, EntityID parent) override; bool Intersects(EntityID, EntityID); + void CreateBoundingBox(std::shared_ptr); }; } From 41e0c7b07ae288d30694a502112102ac8594832c Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 13 Mar 2014 01:49:42 +0100 Subject: [PATCH 3/4] COLLIBRI --- Escape-the-Dawn/GameWorld.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Escape-the-Dawn/GameWorld.cpp b/Escape-the-Dawn/GameWorld.cpp index 9a1d074..8e9fb82 100644 --- a/Escape-the-Dawn/GameWorld.cpp +++ b/Escape-the-Dawn/GameWorld.cpp @@ -103,7 +103,7 @@ 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); + //bounds->VolumeVector = glm::vec3(1.f,0.7f,1.f); player2 = CreateEntity(); From 76da942fdb8833423d7952e05794156acb922aaf Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 13 Mar 2014 02:56:29 +0100 Subject: [PATCH 4/4] Collision with everything omfg --- Escape-the-Dawn/GameWorld.cpp | 7 ++- Escape-the-Dawn/Systems/CollisionSystem.cpp | 55 +++++++++++++++------ Escape-the-Dawn/Systems/CollisionSystem.h | 2 +- Escape-the-Dawn/Systems/PlayerSystem.cpp | 7 +++ Escape-the-Dawn/Systems/PlayerSystem.h | 3 ++ Escape-the-Dawn/World.h | 5 +- 6 files changed, 58 insertions(+), 21 deletions(-) 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;