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); }; }