Continued work on water, can now get 1 particle? maybe? Gotta get rendering to work so i can test it.

This commit is contained in:
tleety
2015-09-21 16:00:20 +02:00
parent b32934dba6
commit 1e70e151ee
8 changed files with 1123 additions and 30 deletions
+25 -28
View File
@@ -100,34 +100,28 @@ public:
m_World->Initialize();
//TODO: Remove tobias light-test code.
/*{
}*/
//OctoBall
{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(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<Components::Model>(ent);
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> ball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
auto plight = m_World->AddComponent<Components::PointLight>(ent);
plight->Radius = 2.f;
m_World->CommitEntity(ent);
}
// {
// auto ent = m_World->CreateEntity();
// std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(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<Components::Model>(ent);
// model->ModelFile = "Models/Test/Ball/Ballopus.obj";
//
// std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
// std::shared_ptr<Components::Ball> ball = m_World->AddComponent<Components::Ball>(ent);
//
// std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
// physics->Static = false;
//
// auto plight = m_World->AddComponent<Components::PointLight>(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<Components::PointLight>(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<Components::Model>(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<Components::Transform>(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<Components::WaterVolume>(t_waterBody);
auto sprite = m_World->AddComponent<Components::Sprite>(t_waterBody);
sprite->SpriteFile = "Textures/Test/Brick_Diffuse.png";
auto body = m_World->AddComponent<Components::RectangleShape>(t_waterBody);
m_World->CommitEntity(t_waterBody);
}
{
+421
View File
@@ -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 <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <sstream>
#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<EventBroker>();
m_Renderer = std::make_shared<Renderer>();
m_Renderer->SetFullscreen(false);
m_Renderer->SetResolution(Rectangle(0, 0, 1920, 1080));
m_Renderer->Initialize();
m_InputManager = std::make_shared<InputManager>(m_Renderer->Window(), m_EventBroker);
m_World = std::make_shared<World>(m_EventBroker);
//TODO: Move this out of engine.h
m_World->ComponentFactory.Register<Components::Transform>();
m_World->SystemFactory.Register<Systems::TransformSystem>(
[this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::TransformSystem>();
m_World->ComponentFactory.Register<Components::Sprite>();
m_World->ComponentFactory.Register<Components::RectangleShape>();
m_World->ComponentFactory.Register<Components::Physics>();
m_World->ComponentFactory.Register<Components::Ball>();
m_World->ComponentFactory.Register<Components::Brick>();
m_World->ComponentFactory.Register<Components::Pad>();
m_World->ComponentFactory.Register<Components::Life>();
m_World->SystemFactory.Register<Systems::PhysicsSystem>(
[this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PhysicsSystem>();
m_World->SystemFactory.Register<Systems::LevelSystem>([this]() { return new Systems::LevelSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::LevelSystem>();
m_World->SystemFactory.Register<Systems::PadSystem>([this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PadSystem>();
m_World->SystemFactory.Register<Systems::BallSystem>([this]() { return new Systems::BallSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::BallSystem>();
m_World->ComponentFactory.Register<Components::Model>();
m_World->ComponentFactory.Register<Components::Template>();
m_World->ComponentFactory.Register<Components::PointLight>();
m_World->ComponentFactory.Register<Components::WaterVolume>();
m_World->Initialize();
//TODO: Remove tobias light-test code.
/*{
}*/
//OctoBall
{
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(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<Components::Model>(ent);
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> ball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
auto plight = m_World->AddComponent<Components::PointLight>(ent);
plight->Radius = 2.f;
m_World->CommitEntity(ent);
}
//PointLightTest
{
auto t_Light = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_Light);
transform->Position = glm::vec3(2.f, 1.5f, -9.f);
auto pl = m_World->AddComponent<Components::PointLight>(t_Light);
pl->Radius = 8.f;
}
//Halfpipe background test model.
{
auto t_halfPipe = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_halfPipe);
transform->Position = glm::vec3(0.f, 0.f, -15.f);
transform->Scale = glm::vec3(15.f);
auto model = m_World->AddComponent<Components::Model>(t_halfPipe);
model->ModelFile = "Models/Test/halfpipe/Halfpipe.obj";
}
//Water test
{
auto t_waterBody = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(t_waterBody);
transform->Position = glm::vec3(0.f, 0.f, -10.f);
auto water = m_World->AddComponent<Components::WaterVolume>(t_waterBody);
auto sprite = m_World->AddComponent<Components::Sprite>(t_waterBody);
sprite->SpriteFile = "Textures/Test/Brick_Diffuse.png";
auto body = m_World->AddComponent<Components::RectangleShape>(t_waterBody);
}
{
auto topWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
transform->Position = glm::vec3(0.f, 6.f, -10.f);
transform->Scale = glm::vec3(18.f, 0.5f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(topWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
physics->Static = true;
m_World->CommitEntity(topWall);
}
{
auto leftWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(leftWall);
transform->Position = glm::vec3(-9.f, 1.f, -10.f);
transform->Scale = glm::vec3(0.5f, 10.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(leftWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(leftWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(leftWall);
physics->Static = true;
m_World->CommitEntity(leftWall);
}
{
auto rightWall = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(rightWall);
transform->Position = glm::vec3(9.f, 1.f, -10.f);
transform->Scale = glm::vec3(0.5f, 10.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(rightWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(rightWall);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(rightWall);
physics->Static = true;
m_World->CommitEntity(rightWall);
}
{
auto ent = m_World->CreateEntity();
m_World->SetProperty(ent, "Name", "Pad");
auto ctransform = m_World->AddComponent<Components::Transform>(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<Components::RectangleShape>(ent);
auto physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
auto csprite = m_World->AddComponent<Components::Sprite>(ent);
auto pad = m_World->AddComponent<Components::Pad>(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<Systems::TransformSystem> m_TransformSystem;
std::shared_ptr<Systems::LevelSystem> m_LevelSystem;
//TODO: Get this out of engine.h
void TEMPAddToRenderQueue()
{
if (!m_TransformSystem)
m_TransformSystem = m_World->GetSystem<Systems::TransformSystem>();
m_RendererQueue.Clear();
for (auto &pair : *m_World->GetEntities())
{
EntityID entity = pair.first;
auto templateComponent = m_World->GetComponent<Components::Template>(entity);
if (templateComponent)
continue;
auto transform = m_World->GetComponent<Components::Transform>(entity);
if (!transform)
continue;
auto modelComponent = m_World->GetComponent<Components::Model>(entity);
if (modelComponent)
{
Model* modelAsset = nullptr;
modelAsset = ResourceManager::Load<Model>(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<Components::PointLight>(entity);
if (pointLightComponent)
{
Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity);
EnqueuePointLight(absoluteTransform.Position,
pointLightComponent->Diffuse,
pointLightComponent->Specular,
pointLightComponent->Radius);
}
auto spriteComponent = m_World->GetComponent<Components::Sprite>(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<Texture>(spriteComponent->SpriteFile);
auto texturenorm = ResourceManager::Load<Texture>(normal);
auto texturespec = ResourceManager::Load<Texture>(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<ResourceManager> m_ResourceManager;
std::shared_ptr<EventBroker> m_EventBroker;
std::shared_ptr<Renderer> m_Renderer;
RenderQueueCollection m_RendererQueue;
std::shared_ptr<InputManager> m_InputManager;
std::shared_ptr<World> m_World;
double m_LastTime;
};
}
+1
View File
@@ -67,6 +67,7 @@ private:
const b2ParticleSystemDef m_ParticleSystemDef;
b2ParticleSystem *m_ParticleSystem;
b2ParticleGroup* t_watergroup;
void InitializeWater();
+136
View File
@@ -0,0 +1,136 @@
#ifndef DAYDREAM_PHYSICSSYSTEM_H
#define DAYDREAM_PHYSICSSYSTEM_H
#include <unordered_map>
#include "Core/System.h"
#include "Core/World.h"
#include "CRectangleShape.h"
#include "Physics/CPhysics.h"
#include <Box2D/Box2D.h>
#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<dd::EventBroker> eventBroker)
: System(world, eventBroker) {}
~PhysicsSystem();
EventRelay<PhysicsSystem, Events::SetImpulse> 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<EntityID, b2Body*> m_EntitiesToBodies;
std::unordered_map<b2Body*, EntityID> 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<Components::Physics>(entityA);
auto physicsComponentB = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(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
+2 -1
View File
@@ -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;
+262
View File
@@ -0,0 +1,262 @@
//
// Created by Adniklastrator on 2015-09-10.
//
#include "PrecompiledHeader.h"
#include "Game/PadSystem.h"
#include "Core/World.h"
#include <iostream>
#include <Game/CPad.h>
#include <Rendering/CPointLight.h>
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<Components::Ball>(entity);
if (ball != NULL) {
if (ReplaceBall() == true) {
SetReplaceBall(false);
auto transformEntity = m_World->GetComponent<Components::Transform>(entity);
transformEntity->Position = glm::vec3(20, 20, -10);
//Temporary. Create new ball.
/*auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(0.5f, 0.f, -10.f);
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Ball.png";
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;*/
auto ent = m_World->CreateEntity();
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(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<Components::Model>(ent);
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
//auto pointlight = m_World->AddComponent<Components::PointLight>(ent);
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(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<std::string>(it->first, "Name") == "Pad") {
SetEntity(it->first);
SetTransform(m_World->GetComponent<Components::Transform>(Entity()));
SetPad(m_World->GetComponent<Components::Pad>(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<Components::Ball>(entityBall);
if (ball == NULL) {
return false;
}
EntityID entityPad = event.Entity1;
auto pad = m_World->GetComponent<Components::Pad>(entityPad);
if (pad == NULL) {
return false;
}
auto transformBall = m_World->GetComponent<Components::Transform>(entityBall);
auto transformPad = m_World->GetComponent<Components::Transform>(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<Components::Transform> transform = m_World->AddComponent<Components::Transform>(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<Components::Model>(ent);
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
//auto pointlight = m_World->AddComponent<Components::PointLight>(ent);
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(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<float>(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;
}
+11 -1
View File
@@ -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()
+265
View File
@@ -0,0 +1,265 @@
#include "PrecompiledHeader.h"
#include "Physics/PhysicsSystem.h"
void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
{
cf->Register<Components::CircleShape>();
}
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<Components::Transform>(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<Components::Transform>(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<Components::Physics>(entity);
auto waterComponent = m_World->GetComponent<Components::WaterVolume>(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<Components::Physics>(entity);
if(!physicsComponent){
LOG_ERROR("No PhysicsComponent in CreateBody");
return;
}
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
if(!transformComponent) {
LOG_ERROR("No TransformComponent in CreateBody");
return;
}
auto absoluteTransform = m_World->GetSystem<Systems::TransformSystem>()->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<Components::RectangleShape>(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<Components::CircleShape>(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<Components::Transform>(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;
}
}