diff --git a/Escape-the-Dawn/Components/SoundEmitter.h b/Escape-the-Dawn/Components/SoundEmitter.h index d211c05..8e471d5 100644 --- a/Escape-the-Dawn/Components/SoundEmitter.h +++ b/Escape-the-Dawn/Components/SoundEmitter.h @@ -15,6 +15,7 @@ struct SoundEmitter : Component float ReferenceDistance; float Pitch; bool Loop; + std::string Path; }; } diff --git a/Escape-the-Dawn/GameWorld.cpp b/Escape-the-Dawn/GameWorld.cpp index 8979ec8..33aa55f 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,25 @@ 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, 10.f, 14.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(15.0f),glm::vec3(1,0,0)); + collision = AddComponent(entcamera, "Collision"); + collision->Phantom = false; + bounds = AddComponent(entcamera, "Bounds"); + bounds->VolumeVector = glm::vec3(0.1f,0.1f,0.1f); // Fucking lights ent = CreateEntity(); @@ -80,7 +86,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(1.0f); model = AddComponent(m_Player, "Model"); model->ModelFile = "Models/ship.obj"; @@ -92,15 +98,22 @@ void GameWorld::Initialize() pointLight->quadraticAttenuation = 0.f; pointLight->spotExponent = 0.0f; AddComponent(m_Player, "Input"); - auto bounds = AddComponent(m_Player, "Bounds"); - bounds->Origin = glm::vec3(0.f, 0.f, 2.f); - bounds->VolumeVector = glm::vec3(3.0f, 0.5f, 2.0f); + collision = AddComponent(m_Player, "Collision"); + collision->Phantom = false; + 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); ent = CreateEntity(); transform = AddComponent(ent, "Transform"); transform->Position = glm::vec3(10.f, 4.f, 0.f); model = AddComponent(ent, "Model"); model->ModelFile = "Models/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) @@ -130,7 +143,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 d797ca9..d2ac780 100644 --- a/Escape-the-Dawn/GameWorld.h +++ b/Escape-the-Dawn/GameWorld.h @@ -42,7 +42,7 @@ public: private: std::shared_ptr m_Renderer; - + EntityID entcamera, player2; EntityID m_Player; }; diff --git a/Escape-the-Dawn/Renderer.cpp b/Escape-the-Dawn/Renderer.cpp index 13c2967..47ba274 100644 --- a/Escape-the-Dawn/Renderer.cpp +++ b/Escape-the-Dawn/Renderer.cpp @@ -132,10 +132,19 @@ void Renderer::Draw(double dt) glEnable(GL_BLEND); glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); m_ShaderProgramDebugAABB.Bind(); - for (auto aabbModelMatrix : AABBsToRender) { + for (auto tuple : AABBsToRender) { + glm::mat4 modelMatrix; + bool colliding; + std::tie(modelMatrix, colliding) = tuple; + // Model matrix glm::mat4 cameraMatrix = m_Camera->ProjectionMatrix() * m_Camera->ViewMatrix(); - glm::mat4 MVP = cameraMatrix * aabbModelMatrix; + glm::mat4 MVP = cameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgramDebugAABB.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + // Color + glm::vec4 color(1.f, 1.f, 1.f, 0.f); + if (colliding) + color = glm::vec4(1.f, 0.f, 0.f, 0.f); + glUniform4fv(glGetUniformLocation(m_ShaderProgramDebugAABB.GetHandle(), "Color"), 1, glm::value_ptr(color)); glBindVertexArray(m_DebugAABB); glDrawArrays(GL_LINES, 0, 24); } @@ -319,6 +328,14 @@ void Renderer::AddPointLightToDraw( Lights = Light_constantAttenuation.size(); } +void Renderer::AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding) +{ + glm::mat4 model; + model *= glm::translate(origin); + model *= glm::scale(volumeVector); + AABBsToRender.push_back(std::make_tuple(model, colliding)); +} + GLuint Renderer::CreateQuad() { float quadVertices[] = { @@ -411,12 +428,6 @@ GLuint Renderer::CreateAABB() return vao; } -void Renderer::AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector) -{ - glm::mat4 model; - model *= glm::translate(origin); - model *= glm::scale(volumeVector); - AABBsToRender.push_back(model); } void Renderer::ClearStuff() @@ -429,5 +440,4 @@ void Renderer::ClearStuff() Light_constantAttenuation.clear(); Light_linearAttenuation.clear(); Light_quadraticAttenuation.clear(); - Light_spotExponent.clear(); -} + Light_spotExponent.clear(); \ No newline at end of file diff --git a/Escape-the-Dawn/Renderer.h b/Escape-the-Dawn/Renderer.h index 1a8361d..263a99d 100644 --- a/Escape-the-Dawn/Renderer.h +++ b/Escape-the-Dawn/Renderer.h @@ -47,7 +47,7 @@ public: std::vector Light_linearAttenuation; std::vector Light_quadraticAttenuation; std::vector Light_spotExponent; - std::vector AABBsToRender; + std::vector> AABBsToRender; Renderer(); @@ -66,7 +66,7 @@ public: float _quadraticAttenuation, float _spotExponent ); - void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector); + void AddAABBToDraw(glm::vec3 origin, glm::vec3 volumeVector, bool colliding); void LoadContent(); diff --git a/Escape-the-Dawn/Shaders/AABB.frag.glsl b/Escape-the-Dawn/Shaders/AABB.frag.glsl index 00cb7ab..16bbb30 100644 --- a/Escape-the-Dawn/Shaders/AABB.frag.glsl +++ b/Escape-the-Dawn/Shaders/AABB.frag.glsl @@ -1,5 +1,7 @@ #version 430 +uniform vec4 Color; + in VertexData { vec3 Position; vec3 Normal; @@ -11,5 +13,5 @@ out vec4 FragmentColor; void main() { //FragmentColor = vec4(1.0 - gl_Color.r, 1.0 - gl_Color.g, 1.0 - gl_Color.b, 0.0); - FragmentColor = vec4(1.0, 1.0, 1.0, 1.0); + FragmentColor = Color; } \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/CollisionSystem.cpp b/Escape-the-Dawn/Systems/CollisionSystem.cpp index f9c5a8a..097f05f 100644 --- a/Escape-the-Dawn/Systems/CollisionSystem.cpp +++ b/Escape-the-Dawn/Systems/CollisionSystem.cpp @@ -3,7 +3,7 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { - if (parent != 0) + /*if (parent != 0) { auto transform = m_World->GetComponent(entity, "Transform"); auto parentTransform = m_World->GetComponent(parent, "Transform"); @@ -11,6 +11,115 @@ void Systems::CollisionSystem::UpdateEntity(double dt, EntityID entity, EntityID transform->Position[0] += parentTransform->Position[0]; transform->Position[1] += parentTransform->Position[1]; transform->Position[2] += parentTransform->Position[2]; + }*/ + + auto collisionComponent = m_World->GetComponent(entity, "Collision"); + if (!collisionComponent) + return; + + // Clear old collisions + collisionComponent->CollidingEntities.clear(); + + 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); } - LOG_INFO("Updating entity %i with parent %i", entity, parent); } + +void Systems::CollisionSystem::Intersects(EntityID aEntity, EntityID bEntity) +{ + auto aTransform = m_World->GetComponent(aEntity, "Transform"); + if(aTransform == nullptr) + return; + auto bTransform = m_World->GetComponent(bEntity, "Transform"); + if(bTransform == nullptr) + return; + + auto aCollisionComponent = m_World->GetComponent(aEntity, "Collision"); + if(aCollisionComponent == nullptr) + return; + auto bCollisionComponent = m_World->GetComponent(bEntity, "Collision"); + if(bCollisionComponent == nullptr) + return; + + + auto aBounds = m_World->GetComponent(aEntity, "Bounds"); + + auto bBounds = m_World->GetComponent(bEntity, "Bounds"); + + 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; + } + } + } +} + +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(vertices), vertices, 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); +} \ No newline at end of file diff --git a/Escape-the-Dawn/Systems/CollisionSystem.h b/Escape-the-Dawn/Systems/CollisionSystem.h index 5c4c448..c4494b9 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,8 @@ public: : System(world) { } void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + 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 dd715e4..cb29cfb 100644 --- a/Escape-the-Dawn/Systems/PlayerSystem.cpp +++ b/Escape-the-Dawn/Systems/PlayerSystem.cpp @@ -58,7 +58,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) { + // DO STUFF + } } + + + 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 4025a5a..56734db 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/Systems/RenderSystem.cpp b/Escape-the-Dawn/Systems/RenderSystem.cpp index c113486..3165a0f 100644 --- a/Escape-the-Dawn/Systems/RenderSystem.cpp +++ b/Escape-the-Dawn/Systems/RenderSystem.cpp @@ -30,11 +30,12 @@ void Systems::RenderSystem::UpdateEntity( double dt, EntityID entity, EntityID p // Debug draw bounds #ifdef DEBUG + auto collision = m_World->GetComponent(entity, "Collision"); auto bounds = m_World->GetComponent(entity, "Bounds"); if (bounds != nullptr) { - glm::vec3 origin = transformComponent->Position + bounds->Origin; + glm::vec3 origin = transformComponent->Position + (transformComponent->Scale * bounds->Origin); glm::vec3 volumeVector = transformComponent->Scale * bounds->VolumeVector; - m_Renderer->AddAABBToDraw(origin, volumeVector); + m_Renderer->AddAABBToDraw(origin, volumeVector, (collision != nullptr && collision->CollidingEntities.size() > 0)); } #endif diff --git a/Escape-the-Dawn/Systems/RenderSystem.h b/Escape-the-Dawn/Systems/RenderSystem.h index 85ac40e..d2a182f 100644 --- a/Escape-the-Dawn/Systems/RenderSystem.h +++ b/Escape-the-Dawn/Systems/RenderSystem.h @@ -10,6 +10,7 @@ #include "Components/Transform.h" #include "Components/Camera.h" #include "Components/Bounds.h" +#include "Components/Collision.h" #include "Renderer.h" namespace Systems 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 diff --git a/Escape-the-Dawn/World.cpp b/Escape-the-Dawn/World.cpp index 4c03697..e52c803 100644 --- a/Escape-the-Dawn/World.cpp +++ b/Escape-the-Dawn/World.cpp @@ -65,7 +65,7 @@ void World::RemoveEntity(EntityID entity) for (auto pair : m_EntityComponents[entity]) { auto type = pair.first; auto component = pair.second; - m_ComponentsOfType[type].erase(std::remove(m_ComponentsOfType[type].begin(), m_ComponentsOfType[type].end(), component)); + m_ComponentsOfType[type].remove(component); } RecycleEntityID(entity); } diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index 009c768..ce8ca37 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,11 +79,10 @@ 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; + std::unordered_map>> m_ComponentsOfType; std::unordered_map>> m_EntityComponents; EntityID GenerateEntityID();