Partycles.
This commit is contained in:
+2
-1
@@ -53,7 +53,8 @@ public:
|
||||
|
||||
/// Register a destruction listener. The listener is owned by you and must
|
||||
/// remain in scope.
|
||||
void SetDestructionListener(b2DestructionListener* listener);
|
||||
void
|
||||
SetDestructionListener(b2DestructionListener* listener);
|
||||
|
||||
/// Register a contact filter to provide specific control over collision.
|
||||
/// Otherwise the default filter is used (b2_defaultFilter). The listener is
|
||||
|
||||
+1
-1
@@ -654,7 +654,7 @@ int32 b2ParticleSystem::CreateParticle(const b2ParticleDef& def)
|
||||
// If the oldest particle should be destroyed...
|
||||
if (m_def.destroyByAge)
|
||||
{
|
||||
DestroyOldestParticle(0, false);
|
||||
DestroyOldestParticle(0, true);
|
||||
// Need to destroy this particle *now* so that it's possible to
|
||||
// create a new particle.
|
||||
SolveZombie();
|
||||
|
||||
+2
-2
@@ -299,7 +299,7 @@ public:
|
||||
/// b2World::Step()).
|
||||
void DestroyParticle(int32 index)
|
||||
{
|
||||
DestroyParticle(index, false);
|
||||
DestroyParticle(index, true);
|
||||
}
|
||||
|
||||
/// Destroy a particle.
|
||||
@@ -330,7 +330,7 @@ public:
|
||||
/// @return Number of particles destroyed.
|
||||
int32 DestroyParticlesInShape(const b2Shape& shape, const b2Transform& xf)
|
||||
{
|
||||
return DestroyParticlesInShape(shape, xf, false);
|
||||
return DestroyParticlesInShape(shape, xf, true);
|
||||
}
|
||||
|
||||
/// Destroy particles inside a shape.
|
||||
|
||||
+5
-16
@@ -227,9 +227,10 @@ public:
|
||||
|
||||
transform->Position = glm::vec3(0.f, 0.f, -10.f);
|
||||
particleEmitter->GravityScale = 0.0f;
|
||||
particleEmitter->SpawnRate = 0.1f;
|
||||
particleEmitter->SpawnRate = 1.1f;
|
||||
particleEmitter->NumberOfTicks = 1000;
|
||||
particleEmitter->Speed = 2.f;
|
||||
particleEmitter->ParticlesPerTick = 5;
|
||||
particleEmitter->Spread = glm::pi<float>()/2;
|
||||
particleEmitter->EmittingAngle = glm::pi<float>();
|
||||
|
||||
@@ -242,25 +243,13 @@ public:
|
||||
|
||||
//PtTransform->Velocity = glm::vec3(1.0f, 0.f, 0.f);
|
||||
PtTransform->Position = transform->Position;
|
||||
PtSprite->SpriteFile = "Textures/Test/Water.png";
|
||||
PtParticle->LifeTime = 5.f;
|
||||
PtParticle->Flags = ParticleFlags::Type::staticPressureParticle;
|
||||
PtSprite->SpriteFile = "Textures/Background.png";
|
||||
PtParticle->LifeTime = 1.f;
|
||||
PtParticle->Flags = ParticleFlags::Type::powderParticle | ParticleFlags::Type::particleContactFilterParticle | ParticleFlags::Type::fixtureContactFilterParticle;
|
||||
}
|
||||
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);
|
||||
auto PtParticle = m_World->AddComponent<Components::Particle>(Pt);
|
||||
|
||||
PtTransform->Velocity = glm::vec3(1.0f, 0.f, 0.f);
|
||||
PtTransform->Position = glm::vec3(0.f, 0.f, -10.f);
|
||||
PtSprite->SpriteFile = "Textures/Ball.png";
|
||||
//PtModel->ModelFile = "Models/Brick/Brick.obj";
|
||||
}
|
||||
|
||||
//TODO: Why does the ball not collide with these bricks?
|
||||
//BottomBox
|
||||
{
|
||||
|
||||
@@ -12,6 +12,7 @@ struct ParticleEmitter : public Component
|
||||
int NumberOfTicks = 10.f;
|
||||
float InitialSpeed = 1.f;
|
||||
double SpawnRate = 1;
|
||||
int ParticlesPerTick = 1;
|
||||
double TimeSinceLastSpawn = 0;
|
||||
int MaxCount = 0;
|
||||
float EmittingAngle = 0.f;
|
||||
|
||||
@@ -36,6 +36,7 @@ namespace Systems
|
||||
class PhysicsSystem : public System
|
||||
{
|
||||
friend class ContractListener;
|
||||
friend class DestructionListener;
|
||||
|
||||
public:
|
||||
PhysicsSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
|
||||
@@ -108,7 +109,36 @@ private:
|
||||
};
|
||||
std::list<Impulse> m_Impulses;
|
||||
|
||||
class DestructionListener : public b2DestructionListener
|
||||
{
|
||||
public:
|
||||
DestructionListener(PhysicsSystem* physicsSystem)
|
||||
: m_PhysicsSystem(physicsSystem) { }
|
||||
|
||||
void SayGoodbye(b2Joint*) {LOG_INFO("joint körs");};
|
||||
void SayGoodbye(b2Fixture*) {LOG_INFO("Fixture körs");};
|
||||
|
||||
void SayGoodbye(b2ParticleSystem* particleSystem, int32 index) override
|
||||
{
|
||||
LOG_INFO("Particle ded");
|
||||
|
||||
|
||||
b2ParticleHandle* handle = particleSystem->GetParticleHandleFromIndex(index);
|
||||
for (int i = 0; i < m_PhysicsSystem->m_ParticleEmitters.ParticleSystem.size(); i++) {
|
||||
if(m_PhysicsSystem->m_ParticleEmitters.ParticleSystem[i] == particleSystem) {
|
||||
std::unordered_map<const b2ParticleHandle*, EntityID>::iterator it = m_PhysicsSystem->m_ParticleHandleToEntities[i].find(handle);
|
||||
if(it != m_PhysicsSystem->m_ParticleHandleToEntities[i].end()) {
|
||||
EntityID id = it->second;
|
||||
m_PhysicsSystem->m_World->RemoveEntity(id);
|
||||
m_PhysicsSystem->m_ParticleHandleToEntities[i].erase(it);
|
||||
LOG_INFO("Removing from list");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
PhysicsSystem* m_PhysicsSystem;
|
||||
};
|
||||
|
||||
class ContactListener : public b2ContactListener
|
||||
{
|
||||
@@ -130,6 +160,7 @@ private:
|
||||
|
||||
m_PhysicsSystem->EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
void EndContact(b2Contact* contact)
|
||||
{
|
||||
|
||||
@@ -149,7 +180,6 @@ private:
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
|
||||
{
|
||||
@@ -160,6 +190,25 @@ private:
|
||||
PhysicsSystem* m_PhysicsSystem;
|
||||
};
|
||||
|
||||
class ParticleContactDisabler : public b2ContactFilter
|
||||
{
|
||||
public:
|
||||
ParticleContactDisabler() { };
|
||||
|
||||
//Particles to particles
|
||||
virtual bool ShouldCollide(b2ParticleSystem *particleSystem, int32 particleIndexA, int32 particleIndexB)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
virtual bool ShouldCollide(b2Fixture* fixture, b2ParticleSystem* particleSystem, int32 particleIndex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
DestructionListener* m_DestructionListener;
|
||||
ParticleContactDisabler m_ParticleContactDisabler;
|
||||
ContactListener* m_ContactListener;
|
||||
};
|
||||
|
||||
|
||||
@@ -358,12 +358,10 @@ void dd::Renderer::DrawForward(RenderQueue &objects, RenderQueue &lights)
|
||||
{
|
||||
// Forward-render semi-transparent objects on top of the current framebuffer
|
||||
glDisable(GL_CULL_FACE);
|
||||
glCullFace(GL_BACK);
|
||||
glDepthMask(GL_TRUE);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_BLEND);
|
||||
//glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
|
||||
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, m_FbDeferred3);
|
||||
// glClearColor(1, 0.9, 0.8f, 1);
|
||||
@@ -444,7 +442,6 @@ void dd::Renderer::DrawScene(RenderQueue &objects, ShaderProgram &program)
|
||||
glUniformMatrix4fv(glGetUniformLocation(shaderProgramHandle, "V"), 1, GL_FALSE, glm::value_ptr(viewMatrix));
|
||||
glUniform4fv(glGetUniformLocation(shaderProgramHandle, "Color"), 1, glm::value_ptr(modelJob->Color));
|
||||
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, spriteJob->DiffuseTexture);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
|
||||
@@ -13,6 +13,7 @@ void dd::Systems::PhysicsSystem::Initialize()
|
||||
std::random_device rd;
|
||||
gen = std::mt19937(rd());
|
||||
|
||||
m_DestructionListener = new DestructionListener(this);
|
||||
m_ContactListener = new ContactListener(this);
|
||||
|
||||
m_Gravity = b2Vec2(0.f, -9.82f);
|
||||
@@ -24,6 +25,8 @@ void dd::Systems::PhysicsSystem::Initialize()
|
||||
m_Accumulator = 0.f;
|
||||
|
||||
m_PhysicsWorld->SetContactListener(m_ContactListener);
|
||||
m_PhysicsWorld->SetContactFilter(&m_ParticleContactDisabler);
|
||||
m_PhysicsWorld->SetDestructionListener(m_DestructionListener);
|
||||
|
||||
InitializeWater();
|
||||
|
||||
@@ -198,7 +201,7 @@ void dd::Systems::PhysicsSystem::Update(double dt) {
|
||||
}
|
||||
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
||||
|
||||
transform->Position = glm::vec3(position.x, position.y, -10);
|
||||
transform->Position = glm::vec3(position.x, position.y, transform->Position.z);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,7 +212,28 @@ void dd::Systems::PhysicsSystem::Update(double dt) {
|
||||
|
||||
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
auto particle = m_World->GetComponent<Components::Particle>(entity);
|
||||
auto pTemplate = m_World->GetComponent<Components::Template>(entity);
|
||||
if (particle && !pTemplate) {
|
||||
particle->LifeTime -= dt;
|
||||
if (particle->LifeTime <= 0) {
|
||||
for (int i = 0; i < m_EntitiesToParticleHandle.size(); i++) {
|
||||
b2ParticleHandle* handle = m_EntitiesToParticleHandle[i][entity];
|
||||
std::unordered_map<EntityID, const b2ParticleHandle*>::iterator iter1;
|
||||
std::unordered_map<const b2ParticleHandle*,EntityID>::iterator iter2;
|
||||
iter1 = m_EntitiesToParticleHandle[i].find(entity);
|
||||
iter2 = m_ParticleHandleToEntities[i].find(handle);
|
||||
if (iter1 != m_EntitiesToParticleHandle[i].end()) {
|
||||
m_EntitiesToParticleHandle[i].erase(iter1);
|
||||
}
|
||||
if (iter2 != m_ParticleHandleToEntities[i].end()) {
|
||||
m_ParticleHandleToEntities[i].erase(iter2);
|
||||
}
|
||||
m_World->RemoveEntity(entity);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
|
||||
@@ -370,11 +394,12 @@ void dd::Systems::PhysicsSystem::CreateParticleGroup(EntityID e)
|
||||
|
||||
void dd::Systems::PhysicsSystem::UpdateParticleEmitters(double dt)
|
||||
{
|
||||
//TODO: ParticleEmitters system are more than one? Wtf?
|
||||
int pSizeTest = 0;
|
||||
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];
|
||||
pSizeTest += ps->GetParticleCount();
|
||||
|
||||
|
||||
auto emitter = m_World->GetComponent<Components::ParticleEmitter>(e);
|
||||
@@ -397,35 +422,47 @@ void dd::Systems::PhysicsSystem::UpdateParticleEmitters(double dt)
|
||||
emitter->NumberOfTicks--;
|
||||
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;
|
||||
float eAngle = emitter->EmittingAngle;
|
||||
float halfSpread = emitter->Spread / 2;
|
||||
for ( int j = 0; j < emitter->ParticlesPerTick; j++) {
|
||||
LOG_INFO("Creating particle #%i", j);
|
||||
particleDef.flags = particleTemplate->Flags;
|
||||
//particleDef.color TODO: Implement this if we want color mixing and shit.
|
||||
particleDef.lifetime = particleTemplate->LifeTime;
|
||||
float eAngle = emitter->EmittingAngle;
|
||||
float halfSpread = emitter->Spread / 2;
|
||||
|
||||
std::uniform_real_distribution<float> dis(eAngle-halfSpread, eAngle+halfSpread);
|
||||
float pAngle = dis(gen);
|
||||
LOG_INFO("Radian angle: %f", pAngle);
|
||||
glm::vec2 unitVec = glm::normalize(glm::vec2(glm::cos(pAngle), glm::sin(pAngle)));
|
||||
glm::vec2 vel = unitVec * emitter->Speed;
|
||||
particleDef.velocity = b2Vec2(vel.x, vel.y);
|
||||
particleDef.position = b2Vec2(templateTransform->Position.x, templateTransform->Position.y);
|
||||
std::uniform_real_distribution<float> dis(eAngle - halfSpread, eAngle + halfSpread);
|
||||
float pAngle = dis(gen);
|
||||
glm::vec2 unitVec = glm::normalize(glm::vec2(glm::cos(pAngle), glm::sin(pAngle)));
|
||||
glm::vec2 vel = unitVec * emitter->Speed;
|
||||
particleDef.velocity = b2Vec2(vel.x, vel.y);
|
||||
particleDef.position = b2Vec2(templateTransform->Position.x, templateTransform->Position.y);
|
||||
|
||||
|
||||
auto particle = m_World->CloneEntity(pt, 0);
|
||||
m_World->RemoveComponent<Components::Template>(particle);
|
||||
auto transform = m_World->GetComponent<Components::Transform>(particle);
|
||||
transform->Scale = glm::vec3 (particleTemplate->Radius * 2.f, particleTemplate->Radius * 2.f, 1);
|
||||
auto particle = m_World->CloneEntity(pt, 0);
|
||||
m_World->RemoveComponent<Components::Template>(particle);
|
||||
auto transform2 = m_World->GetComponent<Components::Transform>(particle);
|
||||
|
||||
std::uniform_real_distribution<float> dist(0, 1);
|
||||
float zDistribution = dist(gen);
|
||||
transform2->Position = glm::vec3(transform2->Position.x, transform2->Position.y, -9.5f + zDistribution);
|
||||
transform2->Scale = glm::vec3(particleTemplate->Radius * 2.f, particleTemplate->Radius * 2.f, 1);
|
||||
|
||||
|
||||
auto b2Particle = ps->CreateParticle(particleDef);
|
||||
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));
|
||||
auto b2ParticleHandle = ps->GetParticleHandleFromIndex(b2Particle);
|
||||
m_EntitiesToParticleHandle[i].insert(std::make_pair(particle, b2ParticleHandle));
|
||||
m_ParticleHandleToEntities[i].insert(std::make_pair(b2ParticleHandle, particle));
|
||||
}
|
||||
}
|
||||
int stp = 0;
|
||||
for (auto ts : m_EntitiesToParticleHandle)
|
||||
{
|
||||
stp += ts.size();
|
||||
}
|
||||
LOG_INFO("Particles in list: %i", stp);
|
||||
LOG_INFO("Particles: %i", pSizeTest);
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::CreateParticleEmitter(EntityID entity)
|
||||
@@ -443,7 +480,7 @@ void dd::Systems::PhysicsSystem::CreateParticleEmitter(EntityID entity)
|
||||
auto particleTemplate = m_World->GetComponent<Components::Template>(c);
|
||||
if (!p) {
|
||||
continue;
|
||||
LOG_WARNING("--ParticleEmitter's Child is not a particle.");
|
||||
LOG_WARNING("ParticleEmitter's Child is not a particle.");
|
||||
}
|
||||
if(!particleTemplate) {
|
||||
LOG_ERROR("ParticleEmitter's particleChild is not a template.");
|
||||
@@ -476,6 +513,7 @@ b2ParticleSystem* dd::Systems::PhysicsSystem::CreateParticleSystem(float radius,
|
||||
m_ParticleSystemDef.maxCount = maxCount;
|
||||
m_ParticleSystemDef.radius = radius;
|
||||
m_ParticleSystemDef.gravityScale = gravityScale;
|
||||
m_ParticleSystemDef.destroyByAge = true;
|
||||
|
||||
return m_PhysicsWorld->CreateParticleSystem(&m_ParticleSystemDef);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user