diff --git a/include/Core/Engine.h b/include/Core/Engine.h index 9aa53b9..cdbb688 100755 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -100,34 +100,28 @@ public: m_World->Initialize(); - //TODO: Remove tobias light-test code. - /*{ - }*/ - //OctoBall - { - auto ent = m_World->CreateEntity(); - std::shared_ptr transform = m_World->AddComponent(ent); - transform->Position = glm::vec3(0.5f, 0.f, -9.9f); - transform->Scale = glm::vec3(1.f, 1.f, 1.f); - transform->Velocity = glm::vec3(1.0f, -5.f, 0.f); - - auto model = m_World->AddComponent(ent); - model->ModelFile = "Models/Test/Ball/Ballopus.obj"; - - std::shared_ptr circleShape = m_World->AddComponent(ent); - std::shared_ptr ball = m_World->AddComponent(ent); - - std::shared_ptr physics = m_World->AddComponent(ent); - physics->Static = false; - - auto plight = m_World->AddComponent(ent); - plight->Radius = 2.f; - - m_World->CommitEntity(ent); - - - } +// { +// auto ent = m_World->CreateEntity(); +// std::shared_ptr transform = m_World->AddComponent(ent); +// transform->Position = glm::vec3(0.5f, 0.f, -9.9f); +// transform->Scale = glm::vec3(1.f, 1.f, 1.f); +// transform->Velocity = glm::vec3(1.0f, -5.f, 0.f); +// +// auto model = m_World->AddComponent(ent); +// model->ModelFile = "Models/Test/Ball/Ballopus.obj"; +// +// std::shared_ptr circleShape = m_World->AddComponent(ent); +// std::shared_ptr ball = m_World->AddComponent(ent); +// +// std::shared_ptr physics = m_World->AddComponent(ent); +// physics->Static = false; +// +// auto plight = m_World->AddComponent(ent); +// plight->Radius = 2.f; +// +// m_World->CommitEntity(ent); +// } //PointLightTest { @@ -136,6 +130,7 @@ public: transform->Position = glm::vec3(2.f, 1.5f, -9.f); auto pl = m_World->AddComponent(t_Light); pl->Radius = 8.f; + m_World->CommitEntity(t_Light); } //Halfpipe background test model. @@ -146,17 +141,19 @@ public: transform->Scale = glm::vec3(15.f); auto model = m_World->AddComponent(t_halfPipe); model->ModelFile = "Models/Test/halfpipe/Halfpipe.obj"; + m_World->CommitEntity(t_halfPipe); } //Water test { auto t_waterBody = m_World->CreateEntity(); auto transform = m_World->AddComponent(t_waterBody); - transform->Position = glm::vec3(0.f, 0.f, -10.f); + transform->Position = glm::vec3(1.5f, 1.5f, -10.f); auto water = m_World->AddComponent(t_waterBody); auto sprite = m_World->AddComponent(t_waterBody); sprite->SpriteFile = "Textures/Test/Brick_Diffuse.png"; auto body = m_World->AddComponent(t_waterBody); + m_World->CommitEntity(t_waterBody); } { diff --git a/include/Core/Engine.h.orig b/include/Core/Engine.h.orig new file mode 100755 index 0000000..7bd2f2a --- /dev/null +++ b/include/Core/Engine.h.orig @@ -0,0 +1,421 @@ +/* + This file is part of Daydream Engine. + Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung + + Daydream Engine is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Daydream Engine is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with Daydream Engine. If not, see . +*/ + +#include +#include + +#include "ResourceManager.h" +#include "OBJ.h" +#include "Model.h" +#include "Texture.h" +#include "EventBroker.h" +#include "RenderQueue.h" +#include "Renderer.h" +#include "InputManager.h" +//TODO: Remove includes that are only here for the temporary draw solution. +#include "World.h" +#include "CTransform.h" +#include "Core/EventBroker.h" +#include "Rendering/CModel.h" +#include "Rendering/CSprite.h" +#include "CTemplate.h" +#include "Rendering/CPointLight.h" +#include "Transform/TransformSystem.h" +#include "Game/LevelSystem.h" +#include "Game/PadSystem.h" +#include "Game/CBall.h" +#include "Game/CBrick.h" +#include "Game/CPad.h" +#include "Game/BallSystem.h" + +#include "Physics/PhysicsSystem.h" +#include "Physics/CPhysics.h" +#include "Physics/CRectangleShape.h" +#include "Physics/ESetImpulse.h" +#include "Physics/CWaterVolume.h" + + +namespace dd +{ + +class Engine +{ +public: + Engine(int argc, char* argv[]) { + m_EventBroker = std::make_shared(); + + m_Renderer = std::make_shared(); + m_Renderer->SetFullscreen(false); + m_Renderer->SetResolution(Rectangle(0, 0, 1920, 1080)); + m_Renderer->Initialize(); + + m_InputManager = std::make_shared(m_Renderer->Window(), m_EventBroker); + + m_World = std::make_shared(m_EventBroker); + + //TODO: Move this out of engine.h + m_World->ComponentFactory.Register(); + m_World->SystemFactory.Register( + [this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); + + m_World->ComponentFactory.Register(); + + m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); + m_World->SystemFactory.Register( + [this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); + + m_World->SystemFactory.Register([this]() { return new Systems::LevelSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); + m_World->SystemFactory.Register([this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); + m_World->SystemFactory.Register([this]() { return new Systems::BallSystem(m_World.get(), m_EventBroker); }); + m_World->AddSystem(); + + m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); + m_World->ComponentFactory.Register(); + m_World->Initialize(); + + + //TODO: Remove tobias light-test code. + /*{ + }*/ + + //OctoBall + { + auto ent = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(ent); + transform->Position = glm::vec3(0.5f, 0.f, -9.9f); + transform->Scale = glm::vec3(1.f, 1.f, 1.f); +<<<<<<< HEAD +======= + transform->Velocity = glm::vec3(1.0f, -5.f, 0.f); +>>>>>>> origin/physics + + auto model = m_World->AddComponent(ent); + model->ModelFile = "Models/Test/Ball/Ballopus.obj"; + + std::shared_ptr circleShape = m_World->AddComponent(ent); + std::shared_ptr ball = m_World->AddComponent(ent); + + std::shared_ptr physics = m_World->AddComponent(ent); + physics->Static = false; + + auto plight = m_World->AddComponent(ent); + plight->Radius = 2.f; + + m_World->CommitEntity(ent); + + + } + + //PointLightTest + { + auto t_Light = m_World->CreateEntity(); + auto transform = m_World->AddComponent(t_Light); + transform->Position = glm::vec3(2.f, 1.5f, -9.f); + auto pl = m_World->AddComponent(t_Light); + pl->Radius = 8.f; + } + + //Halfpipe background test model. + { + auto t_halfPipe = m_World->CreateEntity(); + auto transform = m_World->AddComponent(t_halfPipe); + transform->Position = glm::vec3(0.f, 0.f, -15.f); + transform->Scale = glm::vec3(15.f); + auto model = m_World->AddComponent(t_halfPipe); + model->ModelFile = "Models/Test/halfpipe/Halfpipe.obj"; + } + + //Water test + { + auto t_waterBody = m_World->CreateEntity(); + auto transform = m_World->AddComponent(t_waterBody); + transform->Position = glm::vec3(0.f, 0.f, -10.f); + auto water = m_World->AddComponent(t_waterBody); + auto sprite = m_World->AddComponent(t_waterBody); + sprite->SpriteFile = "Textures/Test/Brick_Diffuse.png"; + auto body = m_World->AddComponent(t_waterBody); + } + + { + auto topWall = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(topWall); + transform->Position = glm::vec3(0.f, 6.f, -10.f); + transform->Scale = glm::vec3(18.f, 0.5f, 1.f); + + std::shared_ptr sprite = m_World->AddComponent(topWall); + sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; + + std::shared_ptr boxShape = m_World->AddComponent(topWall); + + std::shared_ptr physics = m_World->AddComponent(topWall); + physics->Static = true; + + m_World->CommitEntity(topWall); + } + { + auto leftWall = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(leftWall); + transform->Position = glm::vec3(-9.f, 1.f, -10.f); + transform->Scale = glm::vec3(0.5f, 10.f, 1.f); + + std::shared_ptr sprite = m_World->AddComponent(leftWall); + sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; + + std::shared_ptr boxShape = m_World->AddComponent(leftWall); + + std::shared_ptr physics = m_World->AddComponent(leftWall); + physics->Static = true; + + m_World->CommitEntity(leftWall); + } + { + auto rightWall = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(rightWall); + transform->Position = glm::vec3(9.f, 1.f, -10.f); + transform->Scale = glm::vec3(0.5f, 10.f, 1.f); + + std::shared_ptr sprite = m_World->AddComponent(rightWall); + sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; + + std::shared_ptr boxShape = m_World->AddComponent(rightWall); + + std::shared_ptr physics = m_World->AddComponent(rightWall); + physics->Static = true; + + m_World->CommitEntity(rightWall); + } + + { + auto ent = m_World->CreateEntity(); + m_World->SetProperty(ent, "Name", "Pad"); + auto ctransform = m_World->AddComponent(ent); + ctransform->Position = glm::vec3(0.f, -5.f, -10.f); + ctransform->Scale = glm::vec3(3.2, 0.8, 0.); + auto rectangle = m_World->AddComponent(ent); + auto physics = m_World->AddComponent(ent); + physics->Static = false; + auto csprite = m_World->AddComponent(ent); + auto pad = m_World->AddComponent(ent); + csprite->SpriteFile = "Textures/Pad.png"; + m_World->CommitEntity(ent); + } + + m_LastTime = glfwGetTime(); + } + + bool Running() const { return !glfwWindowShouldClose(m_Renderer->Window()); } + + void Tick() + { + double currentTime = glfwGetTime(); + double dt = currentTime - m_LastTime; + m_LastTime = currentTime; + + ResourceManager::Update(); + + // Update input + m_InputManager->Update(dt); + + m_World->Update(dt); + + +// +// if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) { +// ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl"); +// } +// + //TODO Fill up the renderQueue with models (Temp fix) +// TEMPAddToRenderQueue(); + + // Render scene + //TODO send renderqueue to draw. +// m_Renderer->Draw(m_RendererQueue); + + if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) { + ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl"); + } + + //TODO Fill up the renderQueue with models (Temp fix) + TEMPAddToRenderQueue(); + + // Render scene + //TODO send renderqueue to draw. + m_Renderer->Draw(m_RendererQueue); + + // Swap event queues + m_EventBroker->Clear(); + + glfwPollEvents(); + } + + std::shared_ptr m_TransformSystem; + std::shared_ptr m_LevelSystem; + + //TODO: Get this out of engine.h + void TEMPAddToRenderQueue() + { + + if (!m_TransformSystem) + m_TransformSystem = m_World->GetSystem(); + + m_RendererQueue.Clear(); + + for (auto &pair : *m_World->GetEntities()) + { + EntityID entity = pair.first; + + auto templateComponent = m_World->GetComponent(entity); + if (templateComponent) + continue; + + auto transform = m_World->GetComponent(entity); + if (!transform) + continue; + + auto modelComponent = m_World->GetComponent(entity); + if (modelComponent) + { + Model* modelAsset = nullptr; + modelAsset = ResourceManager::Load(modelComponent->ModelFile); + + if (modelAsset) + { + Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); + glm::mat4 modelMatrix = glm::translate(glm::mat4(), absoluteTransform.Position) + * glm::toMat4(absoluteTransform.Orientation) + * glm::scale(absoluteTransform.Scale); + EnqueueModel(modelAsset, modelMatrix, modelComponent->Transparent, modelComponent->Color, modelComponent->ModelFile); + } + } + + //TODO: Add LightLoadShit + + auto pointLightComponent = m_World->GetComponent(entity); + if (pointLightComponent) + { + Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); + EnqueuePointLight(absoluteTransform.Position, + pointLightComponent->Diffuse, + pointLightComponent->Specular, + pointLightComponent->Radius); + } + + + auto spriteComponent = m_World->GetComponent(entity); + if (spriteComponent) + { + + std::string normal = spriteComponent->NormalTexture; + std::string spec = spriteComponent->SpecularTexture; + + if (normal.empty()) { + normal = "Textures/Core/NeutralNormalMap.png"; + } + if (spec.empty()) { + spec = "Textures/Core/NeutralSpecularMap.png"; + } + auto texturediff = ResourceManager::Load(spriteComponent->SpriteFile); + auto texturenorm = ResourceManager::Load(normal); + auto texturespec = ResourceManager::Load(spec); + + + Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); + glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(absoluteTransform.Orientation).z, glm::vec3(0, 0, -1)); + glm::mat4 modelMatrix = glm::translate(absoluteTransform.Position) + * glm::toMat4(orientation2D) + * glm::scale(absoluteTransform.Scale); + EnqueueSprite(texturediff, texturenorm, texturespec, modelMatrix, spriteComponent->Color, absoluteTransform.Position.z); + } + } + + m_RendererQueue.Sort(); + } + + //TODO: Get this out of engine.h + void EnqueueModel(Model* model, glm::mat4 modelMatrix, float transparent, glm::vec4 color, std::string fileName) + { + for (auto texGroup : model->TextureGroups) + { + ModelJob job; + job.TextureID = (texGroup.Texture) ? texGroup.Texture->ResourceID : 0; + job.DiffuseTexture = (texGroup.Texture) ? *texGroup.Texture : 0; + job.NormalTexture = (texGroup.NormalMap) ? *texGroup.NormalMap : 0; + job.SpecularTexture = (texGroup.SpecularMap) ? *texGroup.SpecularMap : 0; + job.VAO = model->VAO; + job.ElementBuffer = model->ElementBuffer; + job.StartIndex = texGroup.StartIndex; + job.EndIndex = texGroup.EndIndex; + job.ModelMatrix = modelMatrix; + job.Color = color; + job.fileName = fileName; + + m_RendererQueue.Deferred.Add(job); + } + } + + // TODO: Get this out of engine.h + void EnqueueSprite(Texture* texture, Texture* normalTexture, Texture* specularTexture, glm::mat4 modelMatrix, glm::vec4 color, float depth) + { + SpriteJob job; + job.TextureID = texture->ResourceID; + job.DiffuseTexture = *texture; + job.NormalTexture = *normalTexture; + job.SpecularTexture = *specularTexture; + job.ModelMatrix = modelMatrix; + job.Color = color; + job.Depth = depth; + + + m_RendererQueue.Forward.Add(job); + } + + void EnqueuePointLight(glm::vec3 position, glm::vec3 diffuseColor, glm::vec3 specularColor, float radius) + { + PointLightJob job; + job.Position = position; + job.DiffuseColor = diffuseColor; + job.SpecularColor = specularColor; + job.Radius = radius; + + m_RendererQueue.Lights.Add(job); + + } + +private: + std::shared_ptr m_ResourceManager; + std::shared_ptr m_EventBroker; + std::shared_ptr m_Renderer; + RenderQueueCollection m_RendererQueue; + std::shared_ptr m_InputManager; + std::shared_ptr m_World; + + double m_LastTime; +}; + +} diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index 3fd8e22..cab4912 100755 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -67,6 +67,7 @@ private: const b2ParticleSystemDef m_ParticleSystemDef; b2ParticleSystem *m_ParticleSystem; + b2ParticleGroup* t_watergroup; void InitializeWater(); diff --git a/include/Physics/PhysicsSystem.h.orig b/include/Physics/PhysicsSystem.h.orig new file mode 100755 index 0000000..eab88e9 --- /dev/null +++ b/include/Physics/PhysicsSystem.h.orig @@ -0,0 +1,136 @@ +#ifndef DAYDREAM_PHYSICSSYSTEM_H +#define DAYDREAM_PHYSICSSYSTEM_H + +#include + +#include "Core/System.h" +#include "Core/World.h" +#include "CRectangleShape.h" +#include "Physics/CPhysics.h" +#include +#include "Core/CTransform.h" +#include "Transform/TransformSystem.h" +#include "Physics/EContact.h" +#include "Physics/ESetImpulse.h" +#include "Physics/CCircleShape.h" +#include "Core/EventBroker.h" +<<<<<<< HEAD +#include "Physics/CWaterVolume.h" +======= +#include "Game/CPad.h" +>>>>>>> origin/physics + + +namespace dd +{ + +namespace Systems +{ + + +class PhysicsSystem : public System +{ + friend class ContractListener; + +public: + PhysicsSystem(World* world, std::shared_ptr eventBroker) + : System(world, eventBroker) {} + + ~PhysicsSystem(); + + EventRelay m_SetImpulse; + bool SetImpulse(const Events::SetImpulse &event); + + void RegisterComponents(ComponentFactory* cf) override; + void Initialize() override; + + // Called once per system every tick + void Update(double dt) override; + // Called once for every entity in the world every tick + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + + // Called when components are committed to an entity + void OnEntityCommit(EntityID entity) override; + + // Called when an entity is removed + void OnEntityRemoved(EntityID entity) override; + +private: + b2Vec2 m_Gravity; + b2World* m_PhysicsWorld; + float m_TimeStep; + float m_Accumulator; + + int m_VelocityIterations, m_PositionIterations; + + std::unordered_map m_EntitiesToBodies; + std::unordered_map m_BodiesToEntities; + + void CreateBody(EntityID entity); + + const b2ParticleSystemDef m_ParticleSystemDef; + b2ParticleSystem *m_ParticleSystem; + + + void InitializeWater(); + void SyncWater(); //TODO: Probably remove this + void CreateParticleGroup(EntityID entity); + + + + class ContactListener : public b2ContactListener + { + public: + ContactListener(PhysicsSystem* physicsSystem) + : m_PhysicsSystem(physicsSystem) { } + + void BeginContact(b2Contact* contact) + { + b2WorldManifold worldManifold; + + contact->GetWorldManifold(&worldManifold); + + Events::Contact e; + e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()]; + e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()]; + e.Normal = glm::normalize(glm::vec2(worldManifold.normal.x, worldManifold.normal.y)); + + + m_PhysicsSystem->EventBroker->Publish(e); + + LOG_INFO("Entity1 = %i, Entity2 = %i\n", e.Entity1, e.Entity2); + } + void EndContact(b2Contact* contact) + { + + } + void PreSolve(b2Contact* contact, const b2Manifold* oldManifold) + { + EntityID entityA = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()]; + EntityID entityB = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()]; + + auto physicsComponentA = m_PhysicsSystem->m_World->GetComponent(entityA); + auto physicsComponentB = m_PhysicsSystem->m_World->GetComponent(entityB); + + if (physicsComponentA != nullptr || physicsComponentB != nullptr) { + // Turn of collisions + contact->SetEnabled(false); + } + + } + void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse) + { + + } + + private: + PhysicsSystem* m_PhysicsSystem; + }; + + ContactListener* m_ContactListener; +}; + +} +} + +#endif //DAYDREAM_PHYSICSSYSTEM_H diff --git a/src/game/Game/PadSystem.cpp b/src/game/Game/PadSystem.cpp index b046af8..ec75a3f 100755 --- a/src/game/Game/PadSystem.cpp +++ b/src/game/Game/PadSystem.cpp @@ -104,7 +104,8 @@ void dd::Systems::PadSystem::Update(double dt) transform->Velocity -= transform->Velocity * pad->SlowdownModifier * (float)dt; - + if (Left()) { + acceleration.x = -pad->AccelerationSpeed; } else if (Right()) { acceleration.x = pad->AccelerationSpeed; diff --git a/src/game/Game/PadSystem.cpp.orig b/src/game/Game/PadSystem.cpp.orig new file mode 100755 index 0000000..2a58277 --- /dev/null +++ b/src/game/Game/PadSystem.cpp.orig @@ -0,0 +1,262 @@ +// +// Created by Adniklastrator on 2015-09-10. +// + +#include "PrecompiledHeader.h" +#include "Game/PadSystem.h" +#include "Core/World.h" +#include +#include +#include + + +void dd::Systems::PadSystem::Initialize() +{ + Events::BindKey r; + r.KeyCode = GLFW_KEY_RIGHT; + r.Command = "right"; + EventBroker->Publish(r); + + Events::BindKey l; + l.KeyCode = GLFW_KEY_LEFT; + l.Command = "left"; + EventBroker->Publish(l); + + EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, PadSystem::OnKeyDown); + EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, PadSystem::OnKeyUp); + EVENT_SUBSCRIBE_MEMBER(m_EContact, PadSystem::OnContact); + EVENT_SUBSCRIBE_MEMBER(m_EResetBall, PadSystem::ResetBall); + + return; +} + +void dd::Systems::PadSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + auto ball = m_World->GetComponent(entity); + if (ball != NULL) { + if (ReplaceBall() == true) { + SetReplaceBall(false); + + auto transformEntity = m_World->GetComponent(entity); + transformEntity->Position = glm::vec3(20, 20, -10); + + //Temporary. Create new ball. + /*auto ent = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(ent); + transform->Position = glm::vec3(0.5f, 0.f, -10.f); + transform->Scale = glm::vec3(1.f, 1.f, 1.f); + std::shared_ptr sprite = m_World->AddComponent(ent); + sprite->SpriteFile = "Textures/Ball.png"; + std::shared_ptr circleShape = m_World->AddComponent(ent); + std::shared_ptr cball = m_World->AddComponent(ent); + std::shared_ptr physics = m_World->AddComponent(ent); + physics->Static = false;*/ + + auto ent = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(ent); + transform->Position = glm::vec3(0.5f, 0.f, -10.f); + transform->Scale = glm::vec3(1.f, 1.f, 1.f); + auto model = m_World->AddComponent(ent); + model->ModelFile = "Models/Test/Ball/Ballopus.obj"; + //auto pointlight = m_World->AddComponent(ent); + std::shared_ptr circleShape = m_World->AddComponent(ent); + std::shared_ptr cball = m_World->AddComponent(ent); + std::shared_ptr physics = m_World->AddComponent(ent); + physics->Static = false; + + m_World->RemoveEntity(entity); + m_World->CommitEntity(ent); + + Events::SetImpulse e; + e.Entity = ent; + e.Impulse = glm::vec2(0.f, -7.f); + e.Point = glm::vec2(transform->Position.x, transform->Position.y); + EventBroker->Publish(e); + } + } +} + +void dd::Systems::PadSystem::Update(double dt) +{ + if (Entity() == 0) { + for (auto it = m_World->GetEntities()->begin(); it != m_World->GetEntities()->end(); it++) { + if (m_World->GetProperty(it->first, "Name") == "Pad") { + SetEntity(it->first); + SetTransform(m_World->GetComponent(Entity())); + SetPad(m_World->GetComponent(Entity())); + break; + } + } + } + + auto transform = Transform(); + auto pad = Pad(); + auto acceleration = Acceleration(); + + if (transform->Velocity.x < -pad->MaxSpeed) { + transform->Velocity.x = -pad->MaxSpeed; + } + else if (transform->Velocity.x > pad->MaxSpeed) { + transform->Velocity.x = pad->MaxSpeed; + } + transform->Position += transform->Velocity * (float)dt; +<<<<<<< HEAD + transform->Velocity += acceleration * (float)dt; + transform->Velocity -= transform->Velocity * pad->SlowdownModifier * (float)dt; + + if (Left()) { + acceleration.x = -pad->AccelerationSpeed; +======= + transform->Velocity += acceleration * (float)dt; + transform->Velocity -= transform->Velocity * (0.9f * (float)dt); + + + + if (left) + { + acceleration.x = -20.f; +>>>>>>> origin/physics + } + else if (Right()) { + acceleration.x = pad->AccelerationSpeed; + } + else { + acceleration.x = 0.f; + } + + SetTransform(transform); + SetPad(pad); + SetAcceleration(acceleration); + + return; +} + +bool dd::Systems::PadSystem::OnKeyDown(const dd::Events::KeyDown &event) +{ + int val = event.KeyCode; + if (val == GLFW_KEY_UP) { + //std::cout << "Up!" << std::endl; + } else if (val == GLFW_KEY_DOWN) { + //std::cout << "Down!" << std::endl; + } else if (val == GLFW_KEY_LEFT) { + //std::cout << "Left!" << std::endl; + //acceleration.x = -0.01f; + SetLeft(true); + } else if (val == GLFW_KEY_RIGHT) { + //std::cout << "Right!" << std::endl; + //acceleration.x = 0.01f; + SetRight(true); + } else if (val == GLFW_KEY_R) { + SetReplaceBall(true); + } + return true; +} + +bool dd::Systems::PadSystem::OnKeyUp(const dd::Events::KeyUp &event) +{ + int val = event.KeyCode; + if (val == GLFW_KEY_UP) { + + } else if (val == GLFW_KEY_DOWN) { + + } else if (val == GLFW_KEY_LEFT) { + SetLeft(false); + } else if (val == GLFW_KEY_RIGHT) { + SetRight(false); + } + return true; +} + +bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event) +{ + /* + EntityID entityBall = event.Entity2; + auto ball = m_World->GetComponent(entityBall); + if (ball == NULL) { + return false; + } + EntityID entityPad = event.Entity1; + auto pad = m_World->GetComponent(entityPad); + if (pad == NULL) { + return false; + } + auto transformBall = m_World->GetComponent(entityBall); + auto transformPad = m_World->GetComponent(entityPad); + + float movementMultiplier = 4.f; + + float whatX = transformBall->Position.x - transformPad->Position.x; + float movementX; + if (whatX > 0) { + movementX = movementMultiplier * whatX; + //std::cout << "Right!" << std::endl; + } else { + movementX = movementMultiplier * whatX; + //std::cout << "Left!" << std::endl; + } + + //float movementY = 1; + + //float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier; +<<<<<<< HEAD + float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2) + 1; + + //std::cout << movementX << " " << movementY << std::endl; + + //Temporary solution. Add a new ball and delete the old one. + auto ent = m_World->CreateEntity(); + std::shared_ptr transform = m_World->AddComponent(ent); + transform->Position = glm::vec3(transformBall->Position.x, transformBall->Position.y + 0.01f, -10.f); + transform->Scale = glm::vec3(1.f, 1.f, 1.f); + auto model = m_World->AddComponent(ent); + model->ModelFile = "Models/Test/Ball/Ballopus.obj"; + //auto pointlight = m_World->AddComponent(ent); + std::shared_ptr circleShape = m_World->AddComponent(ent); + std::shared_ptr cball = m_World->AddComponent(ent); + std::shared_ptr physics = m_World->AddComponent(ent); + physics->Static = false; + + m_World->RemoveEntity(entityBall); + m_World->CommitEntity(ent); + + Events::SetImpulse e; + e.Entity = ent; + e.Impulse = glm::vec2(movementX, movementY); + e.Point = glm::vec2(transform->Position.x, transform->Position.y); + EventBroker->Publish(e); +======= + //float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f; + + // std::cout << movementX << " " << movementY << std::endl; + + float len = glm::length(transformBall->Velocity); + + transformBall->Velocity += glm::vec3(transformPad->Velocity.x, 0, 0); + transformBall->Velocity = glm::normalize(transformBall->Velocity) * len; + + + //transform->Velocity = glm::vec3(movementX, movementY, 0.f); + */ +>>>>>>> origin/physics +} + +bool dd::Systems::PadSystem::ResetBall(const dd::Events::ResetBall &event) +{ + SetReplaceBall(true); + return true; +} + +bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event) +{ + std::string command = event.Command; + + std::cout << "Command!" << std::endl; + + if (command == "right") { + std::cout << "Right!" << std::endl; + } else if (command == "left") { + std::cout << "Left!" << std::endl; + } + + return true; +} \ No newline at end of file diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index fcf5305..a83ef5d 100755 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -22,6 +22,8 @@ void dd::Systems::PhysicsSystem::Initialize() m_PhysicsWorld->SetContactListener(m_ContactListener); + InitializeWater(); + EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, PhysicsSystem::SetImpulse); } @@ -225,15 +227,23 @@ void dd::Systems::PhysicsSystem::CreateParticleGroup(EntityID e) LOG_ERROR("No Transform component in CreateParticleGroup"); return; } - LOG_INFO("------ CREATING PARTICLE GROUP"); b2ParticleGroupDef pd; b2PolygonShape shape; + shape.SetAsBox(transform->Scale.x, transform->Scale.y); pd.shape = &shape; pd.flags = b2_elasticParticle; pd.angle = -0.5f; pd.angularVelocity = 2.0f; + pd.position.Set(transform->Position.x, transform->Position.y); + //pd.particleCount = 10; + //TODO: PUT IN LIST + t_watergroup = m_ParticleSystem->CreateParticleGroup(pd); + LOG_INFO("ParticleCount: %i", t_watergroup->GetParticleCount()); + + + } dd::Systems::PhysicsSystem::~PhysicsSystem() diff --git a/src/game/Physics/PhysicsSystem.cpp.orig b/src/game/Physics/PhysicsSystem.cpp.orig new file mode 100755 index 0000000..7730cb0 --- /dev/null +++ b/src/game/Physics/PhysicsSystem.cpp.orig @@ -0,0 +1,265 @@ +#include "PrecompiledHeader.h" +#include "Physics/PhysicsSystem.h" + + + +void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) +{ + cf->Register(); +} + +void dd::Systems::PhysicsSystem::Initialize() +{ + m_ContactListener = new ContactListener(this); + + m_Gravity = b2Vec2(0.f, 0.f); + m_PhysicsWorld = new b2World(m_Gravity); + + m_TimeStep = 1.f/60.f; + m_VelocityIterations = 6; + m_PositionIterations = 2; + m_Accumulator = 0.f; + + m_PhysicsWorld->SetContactListener(m_ContactListener); + + EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, PhysicsSystem::SetImpulse); +} + +void dd::Systems::PhysicsSystem::InitializeWater() +{ + m_ParticleSystem = m_PhysicsWorld->CreateParticleSystem(&m_ParticleSystemDef); +} + +bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event) +{ + b2Body* body = m_EntitiesToBodies[event.Entity]; + + b2Vec2 impulse; + impulse.x = event.Impulse.x; + impulse.y = event.Impulse.y; + + b2Vec2 point; + point.x = event.Point.x; + point.y = event.Point.y; + + body->ApplyLinearImpulse(impulse, point, true); + + return true; +} + +void dd::Systems::PhysicsSystem::Update(double dt) +{ + for (auto i : m_EntitiesToBodies) { + EntityID entity = i.first; + b2Body* body = i.second; + + auto transformComponent = m_World->GetComponent(entity); + if (! transformComponent) + continue; + + if (body == nullptr) { + //LOG_ERROR("This body should not exist"); + continue; + } + + if (m_World->GetEntityParent(entity) == 0) { + b2Vec2 position; + position.x = transformComponent->Position.x; + position.y = transformComponent->Position.y; + + float angle = -glm::eulerAngles(transformComponent->Orientation).z; + + body->SetTransform(position, angle); +<<<<<<< HEAD +======= + + + body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y)); + + +>>>>>>> origin/physics + } + } + + m_Accumulator += dt; + while(m_Accumulator >= m_TimeStep) + { + m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations); + m_Accumulator -= dt; + } + +<<<<<<< HEAD +======= + +>>>>>>> origin/physics + for (auto i : m_EntitiesToBodies) { + EntityID entity = i.first; + b2Body* body = i.second; + + if (body == nullptr) { + //LOG_ERROR("This body should not exist"); + continue; + } + +<<<<<<< HEAD + auto transformComponent = m_World->GetComponent(entity); + if (! transformComponent) + continue; + +======= +>>>>>>> origin/physics + if (m_World->GetEntityParent(entity) == 0) { + b2Vec2 position = body->GetPosition(); + transformComponent->Position.x = position.x; + transformComponent->Position.y = position.y; + + float angle = body->GetAngle(); + + //TODO: CHECK IF THIS IS CORRECT + transformComponent->Orientation = glm::quat(glm::vec3(0, 0, -angle)); +<<<<<<< HEAD +======= + + b2Vec2 velocity = body->GetLinearVelocity(); + transformComponent->Velocity.x = velocity.x; + transformComponent->Velocity.y = velocity.y; + +>>>>>>> origin/physics + } + } +} + +void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + +} + +void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity) +{ + auto physicsComponent = m_World->GetComponent(entity); + auto waterComponent = m_World->GetComponent(entity); + + if (physicsComponent) { + CreateBody(entity); + } + if (waterComponent) { + CreateParticleGroup(entity); + } +} + + +void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity) +{ + b2Body* body = m_EntitiesToBodies[entity]; + + if (body != nullptr) { + m_EntitiesToBodies.erase(entity); + m_BodiesToEntities.erase(body); + + m_PhysicsWorld->DestroyBody(body); + } + +} + + +void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) +{ + auto physicsComponent = m_World->GetComponent(entity); + if(!physicsComponent){ + LOG_ERROR("No PhysicsComponent in CreateBody"); + return; + } + + + auto transformComponent = m_World->GetComponent(entity); + if(!transformComponent) { + LOG_ERROR("No TransformComponent in CreateBody"); + return; + } + + auto absoluteTransform = m_World->GetSystem()->AbsoluteTransform(entity); + + b2BodyDef bodyDef; + bodyDef.position.Set(absoluteTransform.Position.x, absoluteTransform.Position.y); + + + bodyDef.angle = -glm::eulerAngles(absoluteTransform.Orientation).z; + + if (physicsComponent->Static) { + bodyDef.type = b2_staticBody; + } else { + bodyDef.type = b2_dynamicBody; + + } + + + b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef); + + b2Shape* pShape; + + auto boxComponent = m_World->GetComponent(entity); + if (boxComponent) { + b2PolygonShape* bShape = new b2PolygonShape(); + bShape->SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2); //TODO: THIS SUCKS DUDE 4?!?!?!? + pShape = bShape; + } else { + auto circleComponent = m_World->GetComponent(entity); + if (circleComponent) { + pShape = new b2CircleShape(); + pShape->m_radius = absoluteTransform.Scale.x; + + + if (absoluteTransform.Scale.x != absoluteTransform.Scale.y && absoluteTransform.Scale.y != absoluteTransform.Scale.z) { + LOG_WARNING("Circles has to be of uniform scale."); + } + pShape->m_radius = absoluteTransform.Scale.x/2; //TODO: THIS ALSO SUCKS 4 WTH + + } + } + + if(physicsComponent->Static) { + body->CreateFixture(pShape, 0); //Density kanske ska vara 0 på statiska kroppar + } + else { + //TODO: FIX THIS SHIT INTO COMPONENTS + b2FixtureDef fixtureDef; + fixtureDef.shape = pShape; + fixtureDef.density = 1.f; + fixtureDef.restitution = 1.0f; + fixtureDef.friction = 0.0f; + body->CreateFixture(&fixtureDef); + + } + + delete pShape; + + m_EntitiesToBodies.insert(std::make_pair(entity, body)); + m_BodiesToEntities.insert(std::make_pair(body, entity)); +} + +void dd::Systems::PhysicsSystem::CreateParticleGroup(EntityID e) +{ + //TODO: Lägg alla pd i en lista + auto transform = m_World->GetComponent(e); + if (!transform) { + LOG_ERROR("No Transform component in CreateParticleGroup"); + return; + } + LOG_INFO("------ CREATING PARTICLE GROUP"); + + b2ParticleGroupDef pd; + b2PolygonShape shape; + shape.SetAsBox(transform->Scale.x, transform->Scale.y); + pd.shape = &shape; + pd.flags = b2_elasticParticle; + pd.angle = -0.5f; + pd.angularVelocity = 2.0f; +} + +dd::Systems::PhysicsSystem::~PhysicsSystem() +{ + if (m_ContactListener != nullptr) { + delete m_ContactListener; + m_ContactListener = nullptr; + } +}