Merge remote-tracking branch 'origin/master' into water
Conflicts: include/Core/Engine.h include/Physics/PhysicsSystem.h include/Sound/SoundSystem.h src/game/Physics/PhysicsSystem.cpp src/game/Sound/SoundSystem.cpp PS:min teori i förra commiten var sann
This commit is contained in:
+130
-156
@@ -2,118 +2,122 @@
|
||||
#define DAYDREAM_PHYSICSSYSTEM_H
|
||||
|
||||
#include <unordered_map>
|
||||
#include <random>
|
||||
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <Box2D/Box2D.h>
|
||||
|
||||
#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/ECreateParticleSequence.h"
|
||||
#include "Physics/CCircleShape.h"
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Game/CPad.h"
|
||||
#include "Physics/CWaterVolume.h"
|
||||
#include "Game/CBall.h"
|
||||
#include "Rendering/CSprite.h"
|
||||
#include "Physics/CParticle.h"
|
||||
#include "Physics/CParticleEmitter.h"
|
||||
#include "Core/CTemplate.h"
|
||||
|
||||
#include "Physics/CRectangleShape.h"
|
||||
#include "Physics/CCircleShape.h"
|
||||
#include "Physics/CPhysics.h"
|
||||
#include "Physics/CWaterVolume.h"
|
||||
#include "Core/CTransform.h"
|
||||
#include "Transform/TransformSystem.h"
|
||||
#include "CRectangleShape.h"
|
||||
#include "Physics/CPhysics.h"
|
||||
#include "Physics/EContact.h"
|
||||
#include "Physics/ESetImpulse.h"
|
||||
#include "Physics/CParticle.h"
|
||||
#include "Physics/ECreateParticleSequence.h"
|
||||
#include "Physics/CCircleShape.h"
|
||||
#include "Physics/CParticleEmitter.h"
|
||||
#include "Game/CPad.h"
|
||||
#include "Game/CPad.h"
|
||||
#include "Game/CBall.h"
|
||||
#include "Transform/TransformSystem.h"
|
||||
#include "Rendering/CSprite.h"
|
||||
#include "Core/CTemplate.h"
|
||||
#include "Game/EPause.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
|
||||
|
||||
class PhysicsSystem : public System
|
||||
{
|
||||
friend class ContractListener;
|
||||
friend class DestructionListener;
|
||||
|
||||
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);
|
||||
|
||||
EventRelay<PhysicsSystem, Events::CreateParticleSequence> m_ECreateParticleSequence;
|
||||
bool CreateParticleSequence(const Events::CreateParticleSequence &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::mt19937 gen;
|
||||
|
||||
std::unordered_map<EntityID, b2Body*> m_EntitiesToBodies;
|
||||
std::unordered_map<b2Body*, EntityID> m_BodiesToEntities;
|
||||
|
||||
void CreateBody(EntityID entity);
|
||||
|
||||
b2ParticleSystem* m_WaterParticleSystem;
|
||||
std::vector<b2ParticleGroup*> t_ParticleGroup;
|
||||
|
||||
std::unordered_map<EntityID, const b2ParticleHandle*> m_EntitiesToWaterParticleHandle;
|
||||
std::unordered_map<const b2ParticleHandle*, EntityID> m_WaterParticleHandleToEntities;
|
||||
|
||||
std::vector<std::unordered_map<EntityID, const b2ParticleHandle*>> m_EntitiesToParticleHandle;
|
||||
std::vector<std::unordered_map<const b2ParticleHandle*, EntityID>> m_ParticleHandleToEntities;
|
||||
|
||||
b2ParticleSystem* CreateParticleSystem(float radius, float gravityScale, int maxCount);
|
||||
void InitializeWater();
|
||||
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
|
||||
namespace Systems
|
||||
{
|
||||
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
|
||||
{
|
||||
b2Body* Body;
|
||||
b2Vec2 Impulse;
|
||||
b2Vec2 Point;
|
||||
};
|
||||
std::list<Impulse> m_Impulses;
|
||||
class PhysicsSystem : public System
|
||||
{
|
||||
friend class ContractListener;
|
||||
friend class DestructionListener;
|
||||
|
||||
class DestructionListener : public b2DestructionListener
|
||||
public:
|
||||
PhysicsSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
|
||||
: System(world, eventBroker) { }
|
||||
|
||||
~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;
|
||||
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
||||
void OnEntityCommit(EntityID entity) override;
|
||||
void OnEntityRemoved(EntityID entity) override;
|
||||
|
||||
private:
|
||||
struct Impulse
|
||||
{
|
||||
b2Body* Body;
|
||||
b2Vec2 Vector;
|
||||
b2Vec2 Point;
|
||||
};
|
||||
bool m_Pause = false;
|
||||
|
||||
std::mt19937 gen;
|
||||
|
||||
|
||||
b2Vec2 m_Gravity = b2Vec2(0.f, -9.82f);
|
||||
b2World* m_PhysicsWorld = nullptr;
|
||||
float m_TimeStep = 1.f/60.f;
|
||||
float m_Accumulator = 0.f;
|
||||
int m_VelocityIterations = 6;
|
||||
int m_PositionIterations = 2;
|
||||
|
||||
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;
|
||||
std::unordered_map<b2Body*, EntityID> m_BodiesToEntities;
|
||||
|
||||
|
||||
|
||||
void CreateBody(EntityID entity);
|
||||
void SyncEntitiesWithBodies();
|
||||
void SyncBodiesWithEntities();
|
||||
void InitializeWater();
|
||||
void CreateParticleGroup(EntityID entity);
|
||||
|
||||
b2ParticleSystem* CreateParticleSystem(float radius, float gravityScale, int maxCount);
|
||||
void CreateParticleEmitter(EntityID entity);
|
||||
void UpdateParticleEmitters(double dt); //TODO: Remove them and particles if needed.
|
||||
|
||||
EventRelay<PhysicsSystem, Events::SetImpulse> m_SetImpulse;
|
||||
bool SetImpulse(const Events::SetImpulse &event);
|
||||
dd::EventRelay<PhysicsSystem, dd::Events::Pause> m_EPause;
|
||||
bool OnPause(const dd::Events::Pause &event);
|
||||
|
||||
//TODO: Fill struct with info needed.
|
||||
struct ParticleEmitter //TODO CHANGE NAMES
|
||||
{
|
||||
std::vector<b2ParticleSystem*> ParticleSystem;
|
||||
std::vector<EntityID> ParticleEmitter;
|
||||
std::vector<EntityID> ParticleTemplate;
|
||||
};
|
||||
ParticleEmitter m_ParticleEmitters;
|
||||
|
||||
std::list<Impulse> m_Impulses;
|
||||
|
||||
|
||||
class DestructionListener : public b2DestructionListener
|
||||
{
|
||||
public:
|
||||
DestructionListener(PhysicsSystem* physicsSystem)
|
||||
@@ -127,7 +131,7 @@ private:
|
||||
LOG_INFO("Particle ded");
|
||||
|
||||
|
||||
b2ParticleHandle* handle = particleSystem->GetParticleHandleFromIndex(index);
|
||||
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);
|
||||
@@ -144,57 +148,7 @@ private:
|
||||
PhysicsSystem* m_PhysicsSystem;
|
||||
};
|
||||
|
||||
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(contact->GetManifold()->localNormal.x, contact->GetManifold()->localNormal.y));
|
||||
e.SignificantNormal = glm::normalize((glm::abs(e.Normal.x) > glm::abs(e.Normal.y)) ? glm::vec2(e.Normal.x, 0) : glm::vec2(0, e.Normal.y));
|
||||
|
||||
m_PhysicsSystem->EventBroker->Publish(e);
|
||||
}
|
||||
|
||||
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->GetFixtureB()->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) {
|
||||
if (physicsComponentA->Calculate || physicsComponentB->Calculate) {
|
||||
// Turn of collisions
|
||||
contact->SetEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
PhysicsSystem* m_PhysicsSystem;
|
||||
};
|
||||
|
||||
class ParticleContactDisabler : public b2ContactFilter
|
||||
class ParticleContactDisabler : public b2ContactFilter
|
||||
{
|
||||
public:
|
||||
ParticleContactDisabler() { };
|
||||
@@ -211,12 +165,32 @@ private:
|
||||
|
||||
};
|
||||
|
||||
DestructionListener* m_DestructionListener;
|
||||
ParticleContactDisabler m_ParticleContactDisabler;
|
||||
ContactListener* m_ContactListener;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
DestructionListener* m_DestructionListener;
|
||||
ParticleContactDisabler m_ParticleContactDisabler;
|
||||
ContactListener* m_ContactListener;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif //DAYDREAM_PHYSICSSYSTEM_H
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user