Merge remote-tracking branch 'origin/master' into Physics
Conflicts: src/game/Physics/PhysicsSystem.cpp
This commit is contained in:
+38
-41
@@ -204,56 +204,55 @@ public:
|
||||
m_World->CommitEntity(background);
|
||||
}
|
||||
|
||||
//ParticleTest
|
||||
// {
|
||||
// auto Pe = m_World->CreateEntity();
|
||||
// auto transform = m_World->AddComponent<Components::Transform>(Pe);
|
||||
// auto particleEmitter= m_World->AddComponent<Components::Emitter>(Pe);
|
||||
//
|
||||
// transform->Position = glm::vec3(0.f, -5.f, -10.f);
|
||||
// particleEmitter->GravityScale = 0.0f;
|
||||
// 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
|
||||
{
|
||||
auto t_waterBody = m_World->CreateEntity();
|
||||
auto transform = m_World->AddComponent<Components::Transform>(t_waterBody);
|
||||
transform->Position = glm::vec3(0.f, -5.5f, -10.f);
|
||||
transform->Scale = glm::vec3(7.f, 1.5f, 1.f);
|
||||
transform->Position = glm::vec3(0.f, -3.5f, -10.f);
|
||||
transform->Scale = glm::vec3(7.0f, 1.5f, 1.f);
|
||||
transform->Sticky = true;
|
||||
auto water = m_World->AddComponent<Components::WaterVolume>(t_waterBody);
|
||||
auto body = m_World->AddComponent<Components::RectangleShape>(t_waterBody);
|
||||
m_World->CommitEntity(t_waterBody);
|
||||
}
|
||||
|
||||
//ParticleTest
|
||||
|
||||
|
||||
/*{
|
||||
auto Pe = m_World->CreateEntity();
|
||||
auto transform = m_World->AddComponent<Components::Transform>(Pe);
|
||||
auto particleEmitter= m_World->AddComponent<Components::ParticleEmitter>(Pe);
|
||||
auto emitteSprite = m_World->AddComponent<Components::Sprite>(Pe);
|
||||
|
||||
transform->Position = glm::vec3(0.f, 0.f, -10.f);
|
||||
emitteSprite->SpriteFile = "Textures/Test/Brick_Normal.png";
|
||||
particleEmitter->GravityScale = 0.0f;
|
||||
particleEmitter->SpawnRate = 1.f;
|
||||
particleEmitter->NumberOfTicks = 2;
|
||||
particleEmitter->Speed = 1.f;
|
||||
particleEmitter->ParticlesPerTick = 5;
|
||||
particleEmitter->Spread = glm::pi<float>()*2;
|
||||
particleEmitter->EmittingAngle = glm::pi<float>();
|
||||
particleEmitter->LifeTime = 50;
|
||||
|
||||
{
|
||||
auto Pt = m_World->CreateEntity(Pe);
|
||||
auto PtTransform = m_World->AddComponent<Components::Transform>(Pt);
|
||||
auto PtSprite = m_World->AddComponent<Components::Sprite>(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/Background.png";
|
||||
PtParticle->LifeTime = 3.f;
|
||||
PtParticle->Flags = static_cast<ParticleFlags::Type>(ParticleFlags::Type::Powder | ParticleFlags::Type::ParticleContactFilter | ParticleFlags::Type::FixtureContactFilter);
|
||||
}
|
||||
m_World->CommitEntity(Pe);
|
||||
}*/
|
||||
|
||||
//TODO: Why does the ball not collide with these bricks?
|
||||
//BottomBox
|
||||
{
|
||||
@@ -338,8 +337,6 @@ public:
|
||||
m_World->CommitEntity(rightWall);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//EVENT_SUBSCRIBE_MEMBER(m_EGameStart, &Engine::OnGameStart);
|
||||
m_EGameStart = decltype(m_EGameStart)(std::bind(&Engine::OnGameStart, this, std::placeholders::_1));
|
||||
m_EventBroker->Subscribe(m_EGameStart);
|
||||
|
||||
@@ -9,10 +9,16 @@ namespace Components
|
||||
|
||||
struct ParticleEmitter : public Component
|
||||
{
|
||||
int Amount = 10.f;
|
||||
int NumberOfTicks = 10.f;
|
||||
float InitialSpeed = 1.f;
|
||||
double SpawnRate = 1.f;
|
||||
double SpawnRate = 1;
|
||||
int ParticlesPerTick = 1;
|
||||
double TimeSinceLastSpawn = 0;
|
||||
int MaxCount = 0;
|
||||
float EmittingAngle = 0.f;
|
||||
float Spread = 0.f;
|
||||
float Speed = 2.f;
|
||||
double LifeTime = 100;
|
||||
|
||||
//System variables
|
||||
float GravityScale = 1.0f;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef EVENTS_ECREATEPARTICLESEQUENCE_H__
|
||||
#define EVENTS_ECREATEPARTICLESEQUENCE_H__
|
||||
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Physics/CParticle.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
namespace Events
|
||||
{
|
||||
|
||||
struct CreateParticleSequence : public Event
|
||||
{
|
||||
//Emitter
|
||||
double EmitterLifeTime = 100;
|
||||
glm::vec3 Position = glm::vec3(0);
|
||||
float GravityScale = 0.f;
|
||||
float SpawnRate = 1.f;
|
||||
int NumberOfTicks = 100;
|
||||
float Speed = 1.f;
|
||||
int ParticlesPerTick = 1.f;
|
||||
float Spread = 1.f;
|
||||
float EmittingAngle = 0.f;
|
||||
float MaxCount = 0;
|
||||
|
||||
//Particle
|
||||
std::string SpriteFile = "Texures/Core/ErrorTexture.png";
|
||||
double ParticleLifeTime = 3.f;
|
||||
ParticleFlags::Type Flags = static_cast<ParticleFlags::Type>(ParticleFlags::Powder | ParticleFlags::ParticleContactFilter | ParticleFlags::FixtureContactFilter);
|
||||
float Radius = 1.f;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define DAYDREAM_PHYSICSSYSTEM_H
|
||||
|
||||
#include <unordered_map>
|
||||
#include <random>
|
||||
|
||||
#include <Box2D/Box2D.h>
|
||||
|
||||
@@ -20,6 +21,7 @@
|
||||
#include "Physics/CWaterVolume.h"
|
||||
#include "Physics/CPhysics.h"
|
||||
#include "Physics/CParticle.h"
|
||||
#include "Physics/ECreateParticleSequence.h"
|
||||
#include "Physics/CCircleShape.h"
|
||||
#include "Physics/CParticleEmitter.h"
|
||||
|
||||
@@ -46,6 +48,7 @@ namespace dd
|
||||
class PhysicsSystem : public System
|
||||
{
|
||||
friend class ContractListener;
|
||||
friend class DestructionListener;
|
||||
|
||||
public:
|
||||
PhysicsSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
|
||||
@@ -53,6 +56,9 @@ namespace dd
|
||||
|
||||
~PhysicsSystem();
|
||||
|
||||
EventRelay<PhysicsSystem, Events::CreateParticleSequence> m_ECreateParticleSequence;
|
||||
bool CreateParticleSequence(const Events::CreateParticleSequence &event);
|
||||
|
||||
void RegisterComponents(ComponentFactory* cf) override;
|
||||
void Initialize() override;
|
||||
void Update(double dt) override;
|
||||
@@ -68,6 +74,8 @@ namespace dd
|
||||
b2Vec2 Point;
|
||||
};
|
||||
bool m_Pause = false;
|
||||
|
||||
std::mt19937 gen;
|
||||
|
||||
|
||||
b2Vec2 m_Gravity = b2Vec2(0.f, -9.82f);
|
||||
@@ -77,9 +85,11 @@ namespace dd
|
||||
int m_VelocityIterations = 6;
|
||||
int m_PositionIterations = 2;
|
||||
|
||||
std::vector<b2ParticleSystem*> m_ParticleSystem;
|
||||
b2ParticleSystem* m_WaterParticleSystem;
|
||||
std::vector<b2ParticleGroup*> t_ParticleGroup;
|
||||
|
||||
std::unordered_map<const b2ParticleHandle*, EntityID> m_WaterParticleHandleToEntities;
|
||||
std::unordered_map<EntityID, const b2ParticleHandle*> m_EntitiesToWaterParticleHandle;
|
||||
std::vector<std::unordered_map<EntityID, const b2ParticleHandle*>> m_EntitiesToParticleHandle;
|
||||
std::vector<std::unordered_map<const b2ParticleHandle*, EntityID>> m_ParticleHandleToEntities;
|
||||
std::unordered_map<EntityID, b2Body*> m_EntitiesToBodies;
|
||||
@@ -93,7 +103,7 @@ namespace dd
|
||||
void InitializeWater();
|
||||
void CreateParticleGroup(EntityID entity);
|
||||
|
||||
b2ParticleSystem* CreateParticleSystem(float radius, float gravityScale);
|
||||
b2ParticleSystem* CreateParticleSystem(float radius, float gravityScale, int maxCount);
|
||||
void CreateParticleEmitter(EntityID entity);
|
||||
void UpdateParticleEmitters(double dt); //TODO: Remove them and particles if needed.
|
||||
|
||||
@@ -107,34 +117,87 @@ namespace dd
|
||||
float m_DistanceTravelled = 0;
|
||||
|
||||
//TODO: Fill struct with info needed.
|
||||
struct ParticleEmitter
|
||||
{
|
||||
std::vector<b2ParticleSystem*> System;
|
||||
std::vector<EntityID> Emitter;
|
||||
std::vector<EntityID> Template;
|
||||
};
|
||||
ParticleEmitter m_ParticleEmitters;
|
||||
struct EmitterHandler //TODO CHANGE NAMES
|
||||
{
|
||||
std::vector<b2ParticleSystem*> ParticleSystem;
|
||||
std::vector<EntityID> ParticleEmitter;
|
||||
std::vector<EntityID> ParticleTemplate;
|
||||
};
|
||||
EmitterHandler m_ParticleEmitters;
|
||||
|
||||
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");
|
||||
|
||||
|
||||
const 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 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;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
class ContactListener : public b2ContactListener
|
||||
{
|
||||
public:
|
||||
ContactListener(PhysicsSystem* physicsSystem)
|
||||
: m_PhysicsSystem(physicsSystem) { }
|
||||
|
||||
void BeginContact(b2Contact* contact);
|
||||
void EndContact(b2Contact* contact);
|
||||
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
|
||||
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
|
||||
|
||||
private:
|
||||
PhysicsSystem* m_PhysicsSystem;
|
||||
};
|
||||
class ContactListener : public b2ContactListener
|
||||
{
|
||||
public:
|
||||
ContactListener(PhysicsSystem* physicsSystem)
|
||||
: m_PhysicsSystem(physicsSystem) { }
|
||||
|
||||
ContactListener* m_ContactListener;
|
||||
void BeginContact(b2Contact* contact);
|
||||
void EndContact(b2Contact* contact);
|
||||
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold);
|
||||
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse);
|
||||
|
||||
private:
|
||||
PhysicsSystem* m_PhysicsSystem;
|
||||
};
|
||||
|
||||
DestructionListener* m_DestructionListener;
|
||||
ParticleContactDisabler m_ParticleContactDisabler;
|
||||
ContactListener* m_ContactListener;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "Game/CBall.h"
|
||||
#include "Game/CBrick.h"
|
||||
#include "Game/CPad.h"
|
||||
#include "Physics/ECreateParticleSequence.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user