Particle system up and running, but with bugged rendering
This commit is contained in:
+46
-16
@@ -52,6 +52,8 @@
|
|||||||
#include "Physics/CRectangleShape.h"
|
#include "Physics/CRectangleShape.h"
|
||||||
#include "Physics/ESetImpulse.h"
|
#include "Physics/ESetImpulse.h"
|
||||||
#include "Physics/CWaterVolume.h"
|
#include "Physics/CWaterVolume.h"
|
||||||
|
#include "Physics/CParticle.h"
|
||||||
|
#include "Physics/CParticleEmitter.h"
|
||||||
|
|
||||||
#include "Game/EGameStart.h"
|
#include "Game/EGameStart.h"
|
||||||
#include "Sound/EPlaySound.h"
|
#include "Sound/EPlaySound.h"
|
||||||
@@ -129,6 +131,8 @@ public:
|
|||||||
m_World->ComponentFactory.Register<Components::Template>();
|
m_World->ComponentFactory.Register<Components::Template>();
|
||||||
m_World->ComponentFactory.Register<Components::PointLight>();
|
m_World->ComponentFactory.Register<Components::PointLight>();
|
||||||
m_World->ComponentFactory.Register<Components::WaterVolume>();
|
m_World->ComponentFactory.Register<Components::WaterVolume>();
|
||||||
|
m_World->ComponentFactory.Register<Components::Particle>();
|
||||||
|
m_World->ComponentFactory.Register<Components::ParticleEmitter>();
|
||||||
m_World->Initialize();
|
m_World->Initialize();
|
||||||
|
|
||||||
|
|
||||||
@@ -165,7 +169,7 @@ public:
|
|||||||
pl->Radius = 20.f;
|
pl->Radius = 20.f;
|
||||||
pl->Diffuse = glm::vec3(0.8f, 0.7f, 0.05f);
|
pl->Diffuse = glm::vec3(0.8f, 0.7f, 0.05f);
|
||||||
auto model = m_World->AddComponent<Components::Model>(t_Light);
|
auto model = m_World->AddComponent<Components::Model>(t_Light);
|
||||||
model->ModelFile = "Core/UnitSphere.obj";
|
model->ModelFile = "Models/Core/UnitSphere.obj";
|
||||||
m_World->CommitEntity(t_Light);
|
m_World->CommitEntity(t_Light);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
@@ -176,7 +180,7 @@ public:
|
|||||||
pl->Radius = 15.f;
|
pl->Radius = 15.f;
|
||||||
pl->Diffuse = glm::vec3(0.1f, 0.5f, 0.8f);
|
pl->Diffuse = glm::vec3(0.1f, 0.5f, 0.8f);
|
||||||
auto model = m_World->AddComponent<Components::Model>(t_Light);
|
auto model = m_World->AddComponent<Components::Model>(t_Light);
|
||||||
model->ModelFile = "Core/UnitSphere.obj";
|
model->ModelFile = "Models/Core/UnitSphere.obj";
|
||||||
m_World->CommitEntity(t_Light);
|
m_World->CommitEntity(t_Light);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,7 +192,7 @@ public:
|
|||||||
transform->Scale = glm::vec3(6.f, 6.f, 10.f);
|
transform->Scale = glm::vec3(6.f, 6.f, 10.f);
|
||||||
auto model = m_World->AddComponent<Components::Model>(t_halfPipe);
|
auto model = m_World->AddComponent<Components::Model>(t_halfPipe);
|
||||||
model->ModelFile = "Models/Test/halfpipe/Halfpipe.obj";
|
model->ModelFile = "Models/Test/halfpipe/Halfpipe.obj";
|
||||||
model->Color = glm::vec4(1.f, 1.f, 1.f, 0.3f);
|
model->Color = glm::vec4(1.f, 0.f, 0.f, 0.3f);
|
||||||
m_World->CommitEntity(t_halfPipe);
|
m_World->CommitEntity(t_halfPipe);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -203,16 +207,44 @@ public:
|
|||||||
m_World->CommitEntity(background);
|
m_World->CommitEntity(background);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Water test
|
//ParticleTest
|
||||||
{
|
// {
|
||||||
auto t_waterBody = m_World->CreateEntity();
|
// auto Pe = m_World->CreateEntity();
|
||||||
auto transform = m_World->AddComponent<Components::Transform>(t_waterBody);
|
// auto transform = m_World->AddComponent<Components::Transform>(Pe);
|
||||||
transform->Position = glm::vec3(0.f, -0.5f, -10.f);
|
// auto particleEmitter= m_World->AddComponent<Components::ParticleEmitter>(Pe);
|
||||||
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
|
//
|
||||||
auto water = m_World->AddComponent<Components::WaterVolume>(t_waterBody);
|
// transform->Position = glm::vec3(0.f, -5.f, -10.f);
|
||||||
auto body = m_World->AddComponent<Components::RectangleShape>(t_waterBody);
|
// particleEmitter->GravityScale = 0.0f;
|
||||||
m_World->CommitEntity(t_waterBody);
|
// particleEmitter->SpawnRate = 1.0f;
|
||||||
}
|
//
|
||||||
|
// {
|
||||||
|
// auto Pt = m_World->CreateEntity(Pe);
|
||||||
|
// auto PtTransform = m_World->AddComponent<Components::Transform>(Pt);
|
||||||
|
// auto PtSprite = m_World->AddComponent<Components::Sprite>(Pt);
|
||||||
|
// //auto PtModel = m_World->AddComponent<Components::Model>(Pt);
|
||||||
|
// auto PtParticle = m_World->AddComponent<Components::Particle>(Pt);
|
||||||
|
// auto PtTemplate = m_World->AddComponent<Components::Template>(Pt);
|
||||||
|
//
|
||||||
|
// PtTransform->Velocity = glm::vec3(1.0f, 0.f, 0.f);
|
||||||
|
// PtTransform->Position = transform->Position;
|
||||||
|
// PtSprite->SpriteFile = "Textures/Ball.png";
|
||||||
|
//
|
||||||
|
// //PtModel->ModelFile = "Models/Brick/Brick.obj";
|
||||||
|
// PtParticle->LifeTime = 1000.f;
|
||||||
|
// PtParticle->Flags = ParticleFlags::Type::powderParticle;
|
||||||
|
// }
|
||||||
|
// m_World->CommitEntity(Pe);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// {
|
||||||
|
// auto Pt = m_World->CreateEntity();
|
||||||
|
// auto PtTransform = m_World->AddComponent<Components::Transform>(Pt);
|
||||||
|
// auto PtSprite = m_World->AddComponent<Components::Sprite>(Pt);
|
||||||
|
//
|
||||||
|
// PtTransform->Position = glm::vec3(2.f, 0.f, -10.f);
|
||||||
|
// PtSprite->SpriteFile = "Textures/Ball.png";
|
||||||
|
// }
|
||||||
|
|
||||||
//Water test
|
//Water test
|
||||||
{
|
{
|
||||||
auto t_waterBody = m_World->CreateEntity();
|
auto t_waterBody = m_World->CreateEntity();
|
||||||
@@ -371,7 +403,7 @@ public:
|
|||||||
// Swap event queues to get fresh input data in the read queue
|
// Swap event queues to get fresh input data in the read queue
|
||||||
//m_EventBroker->Swap();
|
//m_EventBroker->Swap();
|
||||||
|
|
||||||
ResourceManager::Update();
|
//ResourceManager::Update();
|
||||||
if (m_GameIsRunning) {
|
if (m_GameIsRunning) {
|
||||||
m_World->Update(dt);
|
m_World->Update(dt);
|
||||||
}
|
}
|
||||||
@@ -460,7 +492,6 @@ public:
|
|||||||
auto spriteComponent = m_World->GetComponent<Components::Sprite>(entity);
|
auto spriteComponent = m_World->GetComponent<Components::Sprite>(entity);
|
||||||
if (spriteComponent)
|
if (spriteComponent)
|
||||||
{
|
{
|
||||||
|
|
||||||
std::string normal = spriteComponent->NormalTexture;
|
std::string normal = spriteComponent->NormalTexture;
|
||||||
std::string spec = spriteComponent->SpecularTexture;
|
std::string spec = spriteComponent->SpecularTexture;
|
||||||
|
|
||||||
@@ -483,7 +514,6 @@ public:
|
|||||||
EnqueueSprite(texturediff, texturenorm, texturespec, modelMatrix, spriteComponent->Color, absoluteTransform.Position.z);
|
EnqueueSprite(texturediff, texturenorm, texturespec, modelMatrix, spriteComponent->Color, absoluteTransform.Position.z);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: Get this out of engine.h
|
//TODO: Get this out of engine.h
|
||||||
|
|||||||
Executable
+65
@@ -0,0 +1,65 @@
|
|||||||
|
#ifndef COMPONENTS_PARTICLE_H__
|
||||||
|
#define COMPONENTS_PARTICLE_H__
|
||||||
|
#include "Core/Component.h"
|
||||||
|
|
||||||
|
namespace dd {
|
||||||
|
|
||||||
|
namespace ParticleFlags {
|
||||||
|
|
||||||
|
enum Type {
|
||||||
|
waterParticle = 0,
|
||||||
|
zombieParticle = 1 << 1,
|
||||||
|
wallParticle = 1 << 2,
|
||||||
|
springParticle = 1 << 3,
|
||||||
|
elasticParticle = 1 << 4,
|
||||||
|
viscousParticle = 1 << 5,
|
||||||
|
powderParticle = 1 << 6,
|
||||||
|
tensileParticle = 1 << 7,
|
||||||
|
colorMixingParticle = 1 << 8,
|
||||||
|
destructionListenerParticle = 1 << 9,
|
||||||
|
barrierParticle = 1 << 10,
|
||||||
|
staticPressureParticle = 1 << 11,
|
||||||
|
reactiveParticle = 1 << 12,
|
||||||
|
repulsiveParticle = 1 << 13,
|
||||||
|
fixtureContactListenerParticle = 1 << 14,
|
||||||
|
particleContactListenerParticle = 1 << 15,
|
||||||
|
fixtureContactFilterParticle = 1 << 16,
|
||||||
|
particleContactFilterParticle = 1 << 17
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Components
|
||||||
|
{
|
||||||
|
|
||||||
|
struct Particle : public Component
|
||||||
|
{
|
||||||
|
float Radius = 0.5f;
|
||||||
|
float LifeTime = 5.0f;
|
||||||
|
|
||||||
|
ParticleFlags::Type Flags =
|
||||||
|
ParticleFlags::Type::waterParticle |
|
||||||
|
ParticleFlags::Type::zombieParticle |
|
||||||
|
ParticleFlags::Type::wallParticle |
|
||||||
|
ParticleFlags::Type::springParticle |
|
||||||
|
ParticleFlags::Type::elasticParticle |
|
||||||
|
ParticleFlags::Type::viscousParticle |
|
||||||
|
ParticleFlags::Type::powderParticle |
|
||||||
|
ParticleFlags::Type::tensileParticle |
|
||||||
|
ParticleFlags::Type::colorMixingParticle |
|
||||||
|
ParticleFlags::Type::destructionListenerParticle |
|
||||||
|
ParticleFlags::Type::barrierParticle |
|
||||||
|
ParticleFlags::Type::staticPressureParticle |
|
||||||
|
ParticleFlags::Type::reactiveParticle |
|
||||||
|
ParticleFlags::Type::repulsiveParticle |
|
||||||
|
ParticleFlags::Type::fixtureContactListenerParticle |
|
||||||
|
ParticleFlags::Type::particleContactListenerParticle |
|
||||||
|
ParticleFlags::Type::fixtureContactFilterParticle |
|
||||||
|
ParticleFlags::Type::particleContactFilterParticle;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
Executable
+25
@@ -0,0 +1,25 @@
|
|||||||
|
#ifndef COMPONENTS_PARTICLEEMITTER_H__
|
||||||
|
#define COMPONENTS_PARTICLEEMITTER_H__
|
||||||
|
#include "Core/Component.h"
|
||||||
|
|
||||||
|
namespace dd
|
||||||
|
{
|
||||||
|
namespace Components
|
||||||
|
{
|
||||||
|
|
||||||
|
struct ParticleEmitter : public Component
|
||||||
|
{
|
||||||
|
int Amount = 10.f;
|
||||||
|
float InitialSpeed = 1.f;
|
||||||
|
double SpawnRate = 1.f;
|
||||||
|
double TimeSinceLastSpawn = 0;
|
||||||
|
|
||||||
|
//ParticleSystem variables
|
||||||
|
float GravityScale = 1.0f;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -18,6 +18,9 @@
|
|||||||
#include "Physics/CWaterVolume.h"
|
#include "Physics/CWaterVolume.h"
|
||||||
#include "Game/CBall.h"
|
#include "Game/CBall.h"
|
||||||
#include "Rendering/CSprite.h"
|
#include "Rendering/CSprite.h"
|
||||||
|
#include "Physics/CParticle.h"
|
||||||
|
#include "Physics/CParticleEmitter.h"
|
||||||
|
#include "Core/CTemplate.h"
|
||||||
|
|
||||||
|
|
||||||
namespace dd
|
namespace dd
|
||||||
@@ -66,18 +69,29 @@ private:
|
|||||||
std::unordered_map<b2Body*, EntityID> m_BodiesToEntities;
|
std::unordered_map<b2Body*, EntityID> m_BodiesToEntities;
|
||||||
|
|
||||||
void CreateBody(EntityID entity);
|
void CreateBody(EntityID entity);
|
||||||
void CreateParticleSystem(float radius);
|
|
||||||
|
|
||||||
std::vector<b2ParticleSystem*> m_ParticleSystem;
|
std::vector<b2ParticleSystem*> m_ParticleSystem;
|
||||||
std::vector<b2ParticleGroup*> t_ParticleGroup;
|
std::vector<b2ParticleGroup*> t_ParticleGroup;
|
||||||
|
|
||||||
std::unordered_map<EntityID, const b2ParticleHandle*> m_EntitiesToParticleHandle;
|
std::vector<std::unordered_map<EntityID, const b2ParticleHandle*>> m_EntitiesToParticleHandle;
|
||||||
std::unordered_map<const b2ParticleHandle*, EntityID> m_ParticleHandleToEntities;
|
std::vector<std::unordered_map<const b2ParticleHandle*, EntityID>> m_ParticleHandleToEntities;
|
||||||
|
|
||||||
|
b2ParticleSystem* CreateParticleSystem(float radius, float gravityScale);
|
||||||
void InitializeWater();
|
void InitializeWater();
|
||||||
void SyncWater(); //TODO: Probably remove this
|
|
||||||
void CreateParticleGroup(EntityID entity);
|
void CreateParticleGroup(EntityID entity);
|
||||||
|
void CreateParticleEmitter(EntityID entity);
|
||||||
|
void UpdateParticleEmitters(double dt); //TODO: Remove them and particles if needed.
|
||||||
|
|
||||||
|
//TODO: Fill struct with info needed.
|
||||||
|
struct ParticleEmitter
|
||||||
|
{
|
||||||
|
std::vector<b2ParticleSystem*> ParticleSystem;
|
||||||
|
std::vector<EntityID> ParticleEmitter;
|
||||||
|
std::vector<EntityID> ParticleTemplate;
|
||||||
|
};
|
||||||
|
ParticleEmitter m_ParticleEmitters;
|
||||||
|
|
||||||
|
//TODO: Struct med listor är bättre än en lista med structs
|
||||||
struct Impulse
|
struct Impulse
|
||||||
{
|
{
|
||||||
b2Body* Body;
|
b2Body* Body;
|
||||||
|
|||||||
@@ -312,7 +312,7 @@ void dd::Renderer::Draw(RenderQueueCollection& rq)
|
|||||||
void dd::Renderer::DrawDeferred(RenderQueue &objects, RenderQueue &lights)
|
void dd::Renderer::DrawDeferred(RenderQueue &objects, RenderQueue &lights)
|
||||||
{
|
{
|
||||||
// Pass #1: Fill G-buffers
|
// Pass #1: Fill G-buffers
|
||||||
glDisable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
glCullFace(GL_BACK);
|
glCullFace(GL_BACK);
|
||||||
glDepthMask(GL_TRUE);
|
glDepthMask(GL_TRUE);
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
@@ -444,6 +444,7 @@ void dd::Renderer::DrawScene(RenderQueue &objects, ShaderProgram &program)
|
|||||||
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
|
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
|
||||||
glUniform4fv(glGetUniformLocation(shaderProgramHandle, "Color"), 1, glm::value_ptr(modelJob->Color));
|
glUniform4fv(glGetUniformLocation(shaderProgramHandle, "Color"), 1, glm::value_ptr(modelJob->Color));
|
||||||
|
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, spriteJob->DiffuseTexture);
|
glBindTexture(GL_TEXTURE_2D, spriteJob->DiffuseTexture);
|
||||||
glActiveTexture(GL_TEXTURE1);
|
glActiveTexture(GL_TEXTURE1);
|
||||||
|
|||||||
@@ -30,9 +30,8 @@ void dd::Systems::PhysicsSystem::Initialize()
|
|||||||
void dd::Systems::PhysicsSystem::InitializeWater()
|
void dd::Systems::PhysicsSystem::InitializeWater()
|
||||||
{
|
{
|
||||||
float radius = 0.13f;
|
float radius = 0.13f;
|
||||||
|
float gravityScale = 1.0f;
|
||||||
|
CreateParticleSystem(radius, gravityScale);
|
||||||
CreateParticleSystem(radius);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
||||||
@@ -58,24 +57,22 @@ bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dd::Systems::PhysicsSystem::Update(double dt)
|
void dd::Systems::PhysicsSystem::Update(double dt) {
|
||||||
{
|
|
||||||
|
|
||||||
m_Accumulator += dt;
|
m_Accumulator += dt;
|
||||||
while(m_Accumulator >= m_TimeStep)
|
while (m_Accumulator >= m_TimeStep) {
|
||||||
{
|
|
||||||
|
|
||||||
for (auto i : m_EntitiesToBodies) {
|
for (auto i : m_EntitiesToBodies) {
|
||||||
EntityID entity = i.first;
|
EntityID entity = i.first;
|
||||||
b2Body* body = i.second;
|
b2Body *body = i.second;
|
||||||
|
|
||||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||||
if (! transformComponent) {
|
if (!transformComponent) {
|
||||||
continue;
|
continue;
|
||||||
LOG_ERROR("RigidBody with no TransformComponent");
|
LOG_ERROR("RigidBody with no TransformComponent");
|
||||||
}
|
}
|
||||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||||
if (! physicsComponent) {
|
if (!physicsComponent) {
|
||||||
continue;
|
continue;
|
||||||
LOG_ERROR("RigidBody with no PhysicsComponent");
|
LOG_ERROR("RigidBody with no PhysicsComponent");
|
||||||
}
|
}
|
||||||
@@ -102,19 +99,19 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
UpdateParticleEmitters(dt);
|
||||||
|
|
||||||
for (auto i : m_Impulses) {
|
for (auto i : m_Impulses) {
|
||||||
i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true);
|
i.Body->ApplyLinearImpulse(i.Impulse, i.Point, true);
|
||||||
}
|
}
|
||||||
m_Impulses.clear();
|
m_Impulses.clear();
|
||||||
|
|
||||||
|
|
||||||
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
|
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for (auto i : m_EntitiesToBodies) {
|
for (auto i : m_EntitiesToBodies) {
|
||||||
EntityID entity = i.first;
|
EntityID entity = i.first;
|
||||||
b2Body* body = i.second;
|
b2Body *body = i.second;
|
||||||
|
|
||||||
if (body == nullptr) {
|
if (body == nullptr) {
|
||||||
LOG_ERROR("This body should not exist");
|
LOG_ERROR("This body should not exist");
|
||||||
@@ -122,15 +119,17 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||||
if (! transformComponent)
|
if (!transformComponent)
|
||||||
continue;
|
continue;
|
||||||
auto parent = m_World->GetEntityParent(entity);
|
auto parent = m_World->GetEntityParent(entity);
|
||||||
if (parent == 0) {
|
if (parent == 0) {
|
||||||
|
|
||||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||||
if (physicsComponent->Calculate) { //TODO: REPLACE THIS WITH PARTICLE COLLISION FILTERS
|
if (physicsComponent->Calculate) { //TODO: REPLACE THIS WITH PARTICLE COLLISION FILTERS
|
||||||
transformComponent->Position.x = transformComponent->Position.x + (transformComponent->Velocity.x * m_TimeStep);
|
transformComponent->Position.x =
|
||||||
transformComponent->Position.y = transformComponent->Position.y + (transformComponent->Velocity.y * m_TimeStep);
|
transformComponent->Position.x + (transformComponent->Velocity.x * m_TimeStep);
|
||||||
|
transformComponent->Position.y =
|
||||||
|
transformComponent->Position.y + (transformComponent->Velocity.y * m_TimeStep);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
b2Vec2 position = body->GetPosition();
|
b2Vec2 position = body->GetPosition();
|
||||||
@@ -138,36 +137,42 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
|||||||
transformComponent->Position.y = position.y;
|
transformComponent->Position.y = position.y;
|
||||||
|
|
||||||
float angle = body->GetAngle();
|
float angle = body->GetAngle();
|
||||||
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, angle));
|
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, angle));
|
||||||
|
|
||||||
b2Vec2 velocity = body->GetLinearVelocity();
|
b2Vec2 velocity = body->GetLinearVelocity();
|
||||||
transformComponent->Velocity.x = velocity.x;
|
transformComponent->Velocity.x = velocity.x;
|
||||||
transformComponent->Velocity.y = velocity.y;
|
transformComponent->Velocity.y = velocity.y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
b2Vec2* positionBuffer = m_ParticleSystem[0]->GetPositionBuffer();
|
for ( int e = 0; e < m_EntitiesToParticleHandle.size(); e++) {
|
||||||
for (auto i : m_EntitiesToParticleHandle) {
|
b2Vec2 *positionBuffer = m_ParticleSystem[e]->GetPositionBuffer();
|
||||||
EntityID entity = i.first;
|
for (auto i : m_EntitiesToParticleHandle[e]){
|
||||||
b2ParticleHandle* particleH = i.second;
|
EntityID entity = i.first;
|
||||||
|
b2ParticleHandle *particleH = i.second;
|
||||||
|
|
||||||
b2Vec2 positionB2 = positionBuffer[particleH->GetIndex()];
|
b2Vec2 positionB2 = positionBuffer[particleH->GetIndex()];
|
||||||
glm::vec2 position = glm::vec2(positionB2.x, positionB2.y);
|
glm::vec2 position = glm::vec2(positionB2.x, positionB2.y);
|
||||||
|
|
||||||
|
EntityID entityParent = m_World->GetEntityParent(entity);
|
||||||
|
glm::vec3 parentTransform = glm::vec3(0.f);
|
||||||
|
if(entityParent) {
|
||||||
|
auto tp = m_World->GetComponent<Components::Transform>(entityParent);
|
||||||
|
parentTransform = tp->Position;
|
||||||
|
}
|
||||||
|
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||||
|
|
||||||
EntityID entityParent = m_World->GetEntityParent(entity);
|
transform->Position = glm::vec3(position.x, position.y, -10) - parentTransform;
|
||||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
|
||||||
auto transformParent = m_World->GetComponent<Components::Transform>(entityParent);
|
|
||||||
|
|
||||||
transform->Position = glm::vec3(position.x, position.y, -10) - transformParent->Position;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_Accumulator -= dt;
|
m_Accumulator -= dt;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -177,15 +182,23 @@ void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
|
|||||||
{
|
{
|
||||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||||
auto waterComponent = m_World->GetComponent<Components::WaterVolume>(entity);
|
auto waterComponent = m_World->GetComponent<Components::WaterVolume>(entity);
|
||||||
|
auto particleEmitterComponent = m_World->GetComponent<Components::ParticleEmitter>(entity);
|
||||||
|
|
||||||
if (physicsComponent && waterComponent) {
|
if (physicsComponent && waterComponent) {
|
||||||
LOG_ERROR("Entity has both water and physics component, this is illegal. The police has been alerted.");
|
LOG_ERROR("Entity has both water and physics component, this is illegal. The police has been alerted.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (physicsComponent && particleEmitterComponent) {
|
||||||
|
LOG_ERROR("Entity has both particle and physics component, this is illegal. The police has been alerted.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (physicsComponent) {
|
if (physicsComponent) {
|
||||||
CreateBody(entity);
|
CreateBody(entity);
|
||||||
} else if (waterComponent) {
|
} else if (waterComponent) {
|
||||||
CreateParticleGroup(entity);
|
CreateParticleGroup(entity);
|
||||||
|
} else if (particleEmitterComponent) {
|
||||||
|
CreateParticleEmitter(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -208,7 +221,6 @@ void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity)
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||||
{
|
{
|
||||||
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
||||||
@@ -292,41 +304,128 @@ void dd::Systems::PhysicsSystem::CreateParticleGroup(EntityID e)
|
|||||||
auto transform = m_World->GetComponent<Components::Transform>(e);
|
auto transform = m_World->GetComponent<Components::Transform>(e);
|
||||||
if (!transform) {
|
if (!transform) {
|
||||||
LOG_ERROR("No Transform component in CreateParticleGroup");
|
LOG_ERROR("No Transform component in CreateParticleGroup");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
b2ParticleGroupDef pd;
|
b2ParticleGroupDef pd;
|
||||||
b2PolygonShape shape;
|
b2PolygonShape shape;
|
||||||
//TODO: FIX SO IT CAN NOTICE WHAT PARTICLE SYSTEM IS WATER! [0] IS A FUCKING BULLSHIT FIX.
|
auto water = m_World->GetComponent<Components::WaterVolume>(e);
|
||||||
|
if (water) {
|
||||||
|
|
||||||
shape.SetAsBox(transform->Scale.x/2.f, transform->Scale.y/2.f);
|
shape.SetAsBox(transform->Scale.x/2.f, transform->Scale.y/2.f);
|
||||||
pd.shape = &shape;
|
pd.shape = &shape;
|
||||||
pd.flags = b2_tensileParticle;
|
pd.flags = b2_tensileParticle;
|
||||||
pd.position.Set(transform->Position.x, transform->Position.y);
|
pd.position.Set(transform->Position.x, transform->Position.y);
|
||||||
t_ParticleGroup.push_back(m_ParticleSystem[0]->CreateParticleGroup(pd));
|
t_ParticleGroup.push_back(m_ParticleSystem[0]->CreateParticleGroup(pd));
|
||||||
b2Vec2* t_ParticlePositions = m_ParticleSystem[0]->GetPositionBuffer();
|
b2Vec2* t_ParticlePositions = m_ParticleSystem[0]->GetPositionBuffer();
|
||||||
for(int i = 0; i < m_ParticleSystem[0]->GetParticleCount(); i++){
|
for(int i = 0; i < m_ParticleSystem[0]->GetParticleCount(); i++){
|
||||||
{
|
{
|
||||||
auto t_waterparticle = m_World->CreateEntity(e);
|
auto t_waterparticle = m_World->CreateEntity(e);
|
||||||
auto transformChild = m_World->AddComponent<Components::Transform>(t_waterparticle);
|
auto transformChild = m_World->AddComponent<Components::Transform>(t_waterparticle);
|
||||||
|
|
||||||
transformChild->Position = glm::vec3(t_ParticlePositions[i].x - transform->Position.x, t_ParticlePositions[i].y - transform->Position.y, -9.5f);
|
transformChild->Position = glm::vec3(t_ParticlePositions[i].x - transform->Position.x, t_ParticlePositions[i].y - transform->Position.y, -9.5f);
|
||||||
transformChild->Scale = glm::vec3(m_ParticleSystem[0]->GetRadius())/transform->Scale;
|
transformChild->Scale = glm::vec3(m_ParticleSystem[0]->GetRadius())/transform->Scale;
|
||||||
m_World->CommitEntity(t_waterparticle);
|
m_World->CommitEntity(t_waterparticle);
|
||||||
|
|
||||||
|
m_EntitiesToParticleHandle[0].insert(std::make_pair(t_waterparticle, m_ParticleSystem[0]->GetParticleHandleFromIndex(i)));
|
||||||
|
m_ParticleHandleToEntities[0].insert(std::make_pair( m_ParticleSystem[0]->GetParticleHandleFromIndex(i), t_waterparticle));
|
||||||
|
}
|
||||||
|
|
||||||
m_EntitiesToParticleHandle.insert(std::make_pair(t_waterparticle, m_ParticleSystem[0]->GetParticleHandleFromIndex(i)));
|
|
||||||
m_ParticleHandleToEntities.insert(std::make_pair( m_ParticleSystem[0]->GetParticleHandleFromIndex(i), t_waterparticle));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void dd::Systems::PhysicsSystem::CreateParticleSystem(float radius)
|
void dd::Systems::PhysicsSystem::UpdateParticleEmitters(double dt)
|
||||||
{
|
{
|
||||||
|
for (int i = 0; i < m_ParticleEmitters.ParticleSystem.size(); i++) {
|
||||||
|
auto e = m_ParticleEmitters.ParticleEmitter[i];
|
||||||
|
auto pt = m_ParticleEmitters.ParticleTemplate[i];
|
||||||
|
auto ps = m_ParticleEmitters.ParticleSystem[i];
|
||||||
|
|
||||||
|
|
||||||
|
auto emitter = m_World->GetComponent<Components::ParticleEmitter>(e);
|
||||||
|
auto particleTemplate = m_World->GetComponent<Components::Particle>(pt);
|
||||||
|
emitter->TimeSinceLastSpawn += dt;
|
||||||
|
if(emitter->TimeSinceLastSpawn < emitter->SpawnRate) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
emitter->TimeSinceLastSpawn -= emitter->SpawnRate;
|
||||||
|
auto templateTransform = m_World->GetComponent<Components::Transform>(pt);
|
||||||
|
|
||||||
|
b2ParticleDef particleDef;
|
||||||
|
particleDef.flags = particleTemplate->Flags;
|
||||||
|
//particleDef.color TODO: Implement this if we want color mixing and shit.
|
||||||
|
particleDef.lifetime = particleTemplate->LifeTime;
|
||||||
|
particleDef.velocity = b2Vec2(templateTransform->Velocity.x, templateTransform->Velocity.y);
|
||||||
|
particleDef.position = b2Vec2(templateTransform->Position.x, templateTransform->Position.y);
|
||||||
|
|
||||||
|
auto particle = m_World->CloneEntity(pt, 0);
|
||||||
|
m_World->RemoveComponent<Components::Template>(particle);
|
||||||
|
|
||||||
|
auto b2Particle = ps->CreateParticle(particleDef);
|
||||||
|
auto b2ParticleHandle = ps->GetParticleHandleFromIndex(b2Particle);
|
||||||
|
m_EntitiesToParticleHandle[i].insert(std::make_pair(particle, b2ParticleHandle));
|
||||||
|
m_ParticleHandleToEntities[i].insert(std::make_pair(b2ParticleHandle, particle));
|
||||||
|
|
||||||
|
//TODO: Ta bort detta
|
||||||
|
int particles = 0;
|
||||||
|
for (auto i : m_EntitiesToParticleHandle)
|
||||||
|
{
|
||||||
|
particles += i.size();
|
||||||
|
}
|
||||||
|
LOG_INFO("--I have %i particles.", particles);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dd::Systems::PhysicsSystem::CreateParticleEmitter(EntityID entity)
|
||||||
|
{
|
||||||
|
auto childEntities = m_World->GetEntityChildren(entity);
|
||||||
|
if (childEntities.empty()) {
|
||||||
|
LOG_ERROR("Particle Emitter does not have any child. Please adopt one or this will not work.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
int pf = 0;
|
||||||
|
dd::Components::Particle* particle;
|
||||||
|
EntityID childEntity;
|
||||||
|
for ( auto c : childEntities ) {
|
||||||
|
auto p = m_World->GetComponent<Components::Particle>(c);
|
||||||
|
auto particleTemplate = m_World->GetComponent<Components::Template>(c);
|
||||||
|
if (!p) {
|
||||||
|
continue;
|
||||||
|
LOG_WARNING("--ParticleEmitter's Child is not a particle.");
|
||||||
|
}
|
||||||
|
if(!particleTemplate) {
|
||||||
|
LOG_ERROR("ParticleEmitter's particleChild is not a template.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (pf != 0) {
|
||||||
|
LOG_WARNING("ParticleEmitter has more than one child that is a particleTemplate, only the first one is used.");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
childEntity = c;
|
||||||
|
particle = p;
|
||||||
|
pf++;
|
||||||
|
}
|
||||||
|
//TODO: Skicka med fler flaggor till particlesystemet;
|
||||||
|
m_ParticleEmitters.ParticleSystem.push_back(CreateParticleSystem(particle->Radius, 0.f));
|
||||||
|
m_ParticleEmitters.ParticleEmitter.push_back(entity);
|
||||||
|
m_ParticleEmitters.ParticleTemplate.push_back(childEntity);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
b2ParticleSystem* dd::Systems::PhysicsSystem::CreateParticleSystem(float radius, float gravityScale)
|
||||||
|
{
|
||||||
|
std::unordered_map<EntityID, const b2ParticleHandle*> m1;
|
||||||
|
std::unordered_map<const b2ParticleHandle*, EntityID> m2;
|
||||||
|
m_EntitiesToParticleHandle.push_back(m1);
|
||||||
|
m_ParticleHandleToEntities.push_back(m2);
|
||||||
|
|
||||||
b2ParticleSystemDef m_ParticleSystemDef;
|
b2ParticleSystemDef m_ParticleSystemDef;
|
||||||
m_ParticleSystemDef.radius = radius;
|
m_ParticleSystemDef.radius = radius;
|
||||||
|
m_ParticleSystemDef.gravityScale = gravityScale;
|
||||||
|
|
||||||
m_ParticleSystem.push_back(m_PhysicsWorld->CreateParticleSystem(&m_ParticleSystemDef));
|
m_ParticleSystem.push_back(m_PhysicsWorld->CreateParticleSystem(&m_ParticleSystemDef));
|
||||||
|
|
||||||
|
return m_ParticleSystem.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
dd::Systems::PhysicsSystem::~PhysicsSystem()
|
dd::Systems::PhysicsSystem::~PhysicsSystem()
|
||||||
|
|||||||
Reference in New Issue
Block a user