521 lines
18 KiB
C++
Executable File
521 lines
18 KiB
C++
Executable File
#include "PrecompiledHeader.h"
|
|
#include "Physics/PhysicsSystem.h"
|
|
|
|
|
|
void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
|
|
{
|
|
cf->Register<Components::CircleShape>();
|
|
//TODO: REGISTER PHYSICS COMPONENTS HERE
|
|
}
|
|
|
|
void dd::Systems::PhysicsSystem::Initialize()
|
|
{
|
|
m_ContactListener = new ContactListener(this);
|
|
m_PhysicsWorld = new b2World(m_Gravity);
|
|
m_PhysicsWorld->SetContactListener(m_ContactListener);
|
|
InitializeWater();
|
|
EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, &PhysicsSystem::SetImpulse);
|
|
EVENT_SUBSCRIBE_MEMBER(m_EPause, &PhysicsSystem::OnPause);
|
|
EVENT_SUBSCRIBE_MEMBER(m_EStageCleared, &PhysicsSystem::OnStageCleared);
|
|
}
|
|
|
|
void dd::Systems::PhysicsSystem::InitializeWater()
|
|
{
|
|
float radius = 0.13f;
|
|
float gravityScale = 1.0f;
|
|
CreateParticleSystem(radius, gravityScale);
|
|
}
|
|
|
|
bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
|
|
{
|
|
auto it = m_EntitiesToBodies.find(event.Entity);
|
|
if(it == m_EntitiesToBodies.end()) {
|
|
LOG_ERROR("Entity: %i, Tried to set an impulse on a body that does not exsist", event.Entity);
|
|
return false;
|
|
}
|
|
|
|
b2Vec2 impulse;
|
|
impulse.x = event.Impulse.x;
|
|
impulse.y = event.Impulse.y;
|
|
|
|
b2Vec2 point;
|
|
point.x = event.Point.x;
|
|
point.y = event.Point.y;
|
|
|
|
Impulse i;
|
|
i.Body = it->second;
|
|
i.Vector = impulse;
|
|
i.Point = point;
|
|
|
|
m_Impulses.push_back(i);
|
|
return true;
|
|
}
|
|
|
|
void dd::Systems::PhysicsSystem::SyncEntitiesWithBodies()
|
|
{
|
|
|
|
for (auto i : m_EntitiesToBodies) {
|
|
EntityID entity = i.first;
|
|
b2Body* body = i.second;
|
|
|
|
if (body == nullptr) {
|
|
LOG_ERROR("This body should not exist, please fix this");
|
|
continue;
|
|
}
|
|
|
|
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
|
if (! transformComponent) {
|
|
continue;
|
|
LOG_ERROR("RigidBody with no TransformComponent");
|
|
}
|
|
|
|
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
|
if (! physicsComponent) {
|
|
continue;
|
|
LOG_ERROR("RigidBody with no PhysicsComponent");
|
|
}
|
|
|
|
|
|
|
|
if (m_World->GetEntityParent(entity) == 0) { //TODO: Make this work with childs too
|
|
b2Vec2 position;
|
|
position.x = transformComponent->Position.x;
|
|
position.y = transformComponent->Position.y;
|
|
float angle = glm::eulerAngles(transformComponent->Orientation).z;
|
|
body->SetTransform(position, angle);
|
|
body->SetLinearVelocity(b2Vec2(transformComponent->Velocity.x, transformComponent->Velocity.y));
|
|
body->SetGravityScale(physicsComponent->GravityScale);
|
|
|
|
b2Filter filter;
|
|
filter.categoryBits = physicsComponent->Category;
|
|
filter.maskBits = physicsComponent->Mask;
|
|
body->GetFixtureList()->SetFilterData(filter);
|
|
|
|
if (physicsComponent->CollisionType == CollisionType::Type::Static) {
|
|
body->SetType(b2_staticBody);
|
|
}
|
|
else if (physicsComponent->CollisionType == CollisionType::Type::Dynamic) {
|
|
body->SetType(b2_dynamicBody);
|
|
}
|
|
else if (physicsComponent->CollisionType == CollisionType::Type::Kinematic) {
|
|
body->SetType(b2_dynamicBody);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void dd::Systems::PhysicsSystem::SyncBodiesWithEntities()
|
|
{
|
|
for (auto i : m_BodiesToEntities) {
|
|
b2Body* body = i.first;
|
|
EntityID entity = i.second;
|
|
|
|
if (body == nullptr) {
|
|
LOG_ERROR("This body should not exist, please fix this");
|
|
continue;
|
|
}
|
|
|
|
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
|
if (! transformComponent) {
|
|
continue;
|
|
LOG_ERROR("RigidBody with no TransformComponent");
|
|
}
|
|
|
|
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
|
if (! physicsComponent) {
|
|
continue;
|
|
LOG_ERROR("RigidBody with no PhysicsComponent");
|
|
}
|
|
|
|
auto parent = m_World->GetEntityParent(entity);
|
|
if (parent == 0) {
|
|
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
|
|
|
if (physicsComponent->Calculate) { //TODO: REPLACE THIS WITH PARTICLE COLLISION FILTERS
|
|
transformComponent->Position.x = transformComponent->Position.x + (transformComponent->Velocity.x * m_TimeStep);
|
|
transformComponent->Position.y = transformComponent->Position.y + (transformComponent->Velocity.y * m_TimeStep);
|
|
} else {
|
|
b2Vec2 position = body->GetPosition();
|
|
transformComponent->Position.x = position.x;
|
|
transformComponent->Position.y = position.y;
|
|
|
|
float angle = body->GetAngle();
|
|
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, angle));
|
|
|
|
b2Vec2 velocity = body->GetLinearVelocity();
|
|
transformComponent->Velocity.x = velocity.x;
|
|
transformComponent->Velocity.y = velocity.y;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void dd::Systems::PhysicsSystem::Update(double dt)
|
|
{
|
|
if (m_Pause) {
|
|
return;
|
|
}
|
|
|
|
if (m_Travelling) {
|
|
if (m_DistanceTravelled > 12.f) {
|
|
m_DistanceTravelled = 0;
|
|
m_Travelling = false;
|
|
}
|
|
|
|
m_DistanceTravelled += 6.0f * dt;
|
|
}
|
|
m_Accumulator += dt;
|
|
|
|
while(m_Accumulator >= m_TimeStep)
|
|
{
|
|
|
|
UpdateParticleEmitters(dt);
|
|
SyncEntitiesWithBodies();
|
|
|
|
//Apply Impulses Must be done after SyncEntitiesWithBodies
|
|
for (auto i : m_Impulses) {
|
|
i.Body->ApplyLinearImpulse(i.Vector, i.Point, true);
|
|
}
|
|
m_Impulses.clear();
|
|
|
|
|
|
//Update the PhysicsWorld
|
|
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
|
|
|
|
SyncBodiesWithEntities();
|
|
|
|
|
|
for ( int e = 0; e < m_EntitiesToParticleHandle.size(); e++) {
|
|
b2Vec2 *positionBuffer = m_ParticleSystem[e]->GetPositionBuffer();
|
|
for (auto i : m_EntitiesToParticleHandle[e]) {
|
|
EntityID entity = i.first;
|
|
const b2ParticleHandle *particleH = i.second;
|
|
|
|
b2Vec2 positionB2 = positionBuffer[particleH->GetIndex()];
|
|
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);
|
|
|
|
transform->Position = glm::vec3(position.x, position.y, -10) - parentTransform;
|
|
|
|
}
|
|
}
|
|
|
|
m_Accumulator -= dt;
|
|
}
|
|
}
|
|
|
|
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
|
{
|
|
if (m_Travelling) {
|
|
auto transform = m_World->GetComponent<Components::Transform>(entity);
|
|
if (transform != nullptr) {
|
|
if (transform->Sticky == true) {
|
|
return;
|
|
}
|
|
transform->Position.y -= 6.0f * dt;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool dd::Systems::PhysicsSystem::OnPause(const dd::Events::Pause &event)
|
|
{
|
|
if (event.Type != "PhysicsSystem" && event.Type != "All") {
|
|
return false;
|
|
}
|
|
|
|
if (m_Pause) {
|
|
m_Pause = false;
|
|
} else {
|
|
m_Pause = true;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
|
|
{
|
|
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
|
|
auto waterComponent = m_World->GetComponent<Components::WaterVolume>(entity);
|
|
auto particleEmitterComponent = m_World->GetComponent<Components::ParticleEmitter>(entity);
|
|
|
|
if (physicsComponent && waterComponent) {
|
|
LOG_ERROR("Entity has both water and physics component, this is illegal. The police has been alerted.");
|
|
return;
|
|
}
|
|
if (physicsComponent && particleEmitterComponent) {
|
|
LOG_ERROR("Entity has both particle and physics component, this is illegal. The police has been alerted.");
|
|
return;
|
|
}
|
|
|
|
if (physicsComponent) {
|
|
auto it = m_EntitiesToBodies.find(entity);
|
|
if(it == m_EntitiesToBodies.end()) {
|
|
CreateBody(entity);
|
|
} else {
|
|
LOG_ERROR("Tried to commit a body that already exists");
|
|
}
|
|
} else if (waterComponent) {
|
|
CreateParticleGroup(entity);
|
|
} else if (particleEmitterComponent) {
|
|
CreateParticleEmitter(entity);
|
|
}
|
|
}
|
|
|
|
|
|
void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity)
|
|
{
|
|
/* auto physics = m_World->GetComponent<Components::Physics>(entity);
|
|
if (physics == nullptr) {
|
|
return;
|
|
}*/
|
|
|
|
auto it = m_EntitiesToBodies.find(entity);
|
|
if(it == m_EntitiesToBodies.end()) {
|
|
LOG_ERROR("Trying to remove non-exsisting body, Entity: %i", entity);
|
|
return;
|
|
}
|
|
|
|
//It told me I was removing a lot of objects, and I didn't need that information, so I commented it out.
|
|
//LOG_INFO("Removing entity %i", entity);
|
|
it->second->GetWorld()->DestroyBody(it->second);
|
|
m_BodiesToEntities.erase(it->second);
|
|
m_EntitiesToBodies.erase(entity);
|
|
//delete body;
|
|
}
|
|
|
|
|
|
bool dd::Systems::PhysicsSystem::OnStageCleared(const dd::Events::StageCleared &event)
|
|
{
|
|
if (event.ClearedStage < 5) {
|
|
m_Travelling = true;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
|
|
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->CollisionType == CollisionType::Type::Static) {
|
|
bodyDef.type = b2_staticBody;
|
|
} else if (physicsComponent->CollisionType == CollisionType::Type::Dynamic) {
|
|
bodyDef.type = b2_dynamicBody;
|
|
} else if (physicsComponent->CollisionType == CollisionType::Type::Kinematic) {
|
|
bodyDef.type = b2_dynamicBody;
|
|
}
|
|
|
|
b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef);
|
|
|
|
b2Shape* pShape;
|
|
|
|
auto rectangleComponent = m_World->GetComponent<Components::RectangleShape>(entity);
|
|
if (rectangleComponent) {
|
|
b2PolygonShape* bShape = new b2PolygonShape();
|
|
bShape->SetAsBox(rectangleComponent->Dimensions.x/2.f, rectangleComponent->Dimensions.y/2.f);
|
|
pShape = bShape;
|
|
} else {
|
|
auto circleComponent = m_World->GetComponent<Components::CircleShape>(entity);
|
|
if (circleComponent) {
|
|
pShape = new b2CircleShape();
|
|
pShape->m_radius = circleComponent->Radius;
|
|
}
|
|
}
|
|
|
|
b2FixtureDef fixtureDef;
|
|
fixtureDef.shape = pShape;
|
|
fixtureDef.filter.categoryBits = physicsComponent->Category;
|
|
fixtureDef.filter.maskBits = physicsComponent->Mask;
|
|
|
|
|
|
|
|
fixtureDef.shape = pShape;
|
|
fixtureDef.density = physicsComponent->Density;
|
|
fixtureDef.restitution = 1.0f;
|
|
fixtureDef.friction = 0.0f;
|
|
body->CreateFixture(&fixtureDef);
|
|
|
|
|
|
delete pShape;
|
|
|
|
body->SetGravityScale(physicsComponent->GravityScale);
|
|
m_EntitiesToBodies.insert(std::make_pair(entity, body));
|
|
m_BodiesToEntities.insert(std::make_pair(body, entity));
|
|
}
|
|
void dd::Systems::PhysicsSystem::CreateParticleGroup(EntityID e)
|
|
{
|
|
auto transform = m_World->GetComponent<Components::Transform>(e);
|
|
if (!transform) {
|
|
LOG_ERROR("No Transform component in CreateParticleGroup");
|
|
}
|
|
b2ParticleGroupDef pd;
|
|
b2PolygonShape shape;
|
|
auto water = m_World->GetComponent<Components::WaterVolume>(e);
|
|
if (water) {
|
|
|
|
shape.SetAsBox(transform->Scale.x/2.f, transform->Scale.y/2.f);
|
|
pd.shape = &shape;
|
|
pd.flags = b2_tensileParticle;
|
|
pd.position.Set(transform->Position.x, transform->Position.y);
|
|
t_ParticleGroup.push_back(m_ParticleSystem[0]->CreateParticleGroup(pd));
|
|
b2Vec2* t_ParticlePositions = m_ParticleSystem[0]->GetPositionBuffer();
|
|
for(int i = 0; i < m_ParticleSystem[0]->GetParticleCount(); i++){
|
|
{
|
|
auto t_waterparticle = m_World->CreateEntity(e);
|
|
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->Scale = glm::vec3(m_ParticleSystem[0]->GetRadius())/transform->Scale;
|
|
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));
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
void dd::Systems::PhysicsSystem::UpdateParticleEmitters(double dt)
|
|
{
|
|
for (int i = 0; i < m_ParticleEmitters.System.size(); i++) {
|
|
auto e = m_ParticleEmitters.Emitter[i];
|
|
auto pt = m_ParticleEmitters.Template[i];
|
|
auto ps = m_ParticleEmitters.System[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("--Emitter's Child is not a particle.");
|
|
}
|
|
if(!particleTemplate) {
|
|
LOG_ERROR("Emitter's particleChild is not a template.");
|
|
continue;
|
|
}
|
|
if (pf != 0) {
|
|
LOG_WARNING("Emitter 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.System.push_back(CreateParticleSystem(particle->Radius, 0.f));
|
|
m_ParticleEmitters.Emitter.push_back(entity);
|
|
m_ParticleEmitters.Template.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;
|
|
m_ParticleSystemDef.radius = radius;
|
|
m_ParticleSystemDef.gravityScale = gravityScale;
|
|
|
|
m_ParticleSystem.push_back(m_PhysicsWorld->CreateParticleSystem(&m_ParticleSystemDef));
|
|
|
|
return m_ParticleSystem.back();
|
|
}
|
|
|
|
|
|
|
|
dd::Systems::PhysicsSystem::~PhysicsSystem()
|
|
{
|
|
for (auto i = m_BodiesToEntities.begin(); i != m_BodiesToEntities.end(); i++) {
|
|
b2Body* body = i->first;
|
|
body->GetWorld()->DestroyBody(body);
|
|
//delete body;
|
|
}
|
|
m_BodiesToEntities.clear();
|
|
m_EntitiesToBodies.clear();
|
|
m_Impulses.clear();
|
|
|
|
//TODO:REMOVE PARTICLE SYSTEMS
|
|
|
|
if (m_ContactListener != nullptr) {
|
|
delete m_ContactListener;
|
|
m_ContactListener = nullptr;
|
|
}
|
|
|
|
if (m_PhysicsWorld != nullptr) {
|
|
delete m_PhysicsWorld;
|
|
m_PhysicsWorld = nullptr;
|
|
}
|
|
}
|