Water working, might not work for several bodies
This commit is contained in:
+42
-3
@@ -148,14 +148,53 @@ public:
|
|||||||
{
|
{
|
||||||
auto t_waterBody = m_World->CreateEntity();
|
auto t_waterBody = m_World->CreateEntity();
|
||||||
auto transform = m_World->AddComponent<Components::Transform>(t_waterBody);
|
auto transform = m_World->AddComponent<Components::Transform>(t_waterBody);
|
||||||
transform->Position = glm::vec3(1.5f, 1.5f, -10.f);
|
transform->Position = glm::vec3(0.f, -1.f, -10.f);
|
||||||
|
transform->Scale = glm::vec3(2.f, 3.f, 1.f);
|
||||||
auto water = m_World->AddComponent<Components::WaterVolume>(t_waterBody);
|
auto water = m_World->AddComponent<Components::WaterVolume>(t_waterBody);
|
||||||
auto sprite = m_World->AddComponent<Components::Sprite>(t_waterBody);
|
|
||||||
sprite->SpriteFile = "Textures/Test/Brick_Diffuse.png";
|
|
||||||
auto body = m_World->AddComponent<Components::RectangleShape>(t_waterBody);
|
auto body = m_World->AddComponent<Components::RectangleShape>(t_waterBody);
|
||||||
m_World->CommitEntity(t_waterBody);
|
m_World->CommitEntity(t_waterBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//BottomBox
|
||||||
|
{
|
||||||
|
auto topWall = m_World->CreateEntity();
|
||||||
|
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
|
||||||
|
transform->Position = glm::vec3(0.f, -3.f, -10.f);
|
||||||
|
transform->Scale = glm::vec3(3.f, 0.5f, 1.f);
|
||||||
|
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
|
||||||
|
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
|
||||||
|
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(topWall);
|
||||||
|
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
|
||||||
|
physics->Static = true;
|
||||||
|
m_World->CommitEntity(topWall);
|
||||||
|
}
|
||||||
|
//SideBox
|
||||||
|
{
|
||||||
|
auto topWall = m_World->CreateEntity();
|
||||||
|
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
|
||||||
|
transform->Position = glm::vec3(1.5f, -1.5f, -10.f);
|
||||||
|
transform->Scale = glm::vec3(0.5f, 3.f, 1.f);
|
||||||
|
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
|
||||||
|
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
|
||||||
|
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(topWall);
|
||||||
|
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
|
||||||
|
physics->Static = true;
|
||||||
|
m_World->CommitEntity(topWall);
|
||||||
|
}
|
||||||
|
//OtherSideBox
|
||||||
|
{
|
||||||
|
auto topWall = m_World->CreateEntity();
|
||||||
|
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
|
||||||
|
transform->Position = glm::vec3(-1.5f, -1.5f, -10.f);
|
||||||
|
transform->Scale = glm::vec3(0.5f, 3.0f, 1.f);
|
||||||
|
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall);
|
||||||
|
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
|
||||||
|
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(topWall);
|
||||||
|
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall);
|
||||||
|
physics->Static = true;
|
||||||
|
m_World->CommitEntity(topWall);
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto topWall = m_World->CreateEntity();
|
auto topWall = m_World->CreateEntity();
|
||||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
|
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include "Core/EventBroker.h"
|
#include "Core/EventBroker.h"
|
||||||
#include "Game/CPad.h"
|
#include "Game/CPad.h"
|
||||||
#include "Physics/CWaterVolume.h"
|
#include "Physics/CWaterVolume.h"
|
||||||
|
#include "Rendering/CSprite.h"
|
||||||
|
|
||||||
|
|
||||||
namespace dd
|
namespace dd
|
||||||
@@ -65,17 +66,16 @@ private:
|
|||||||
|
|
||||||
void CreateBody(EntityID entity);
|
void CreateBody(EntityID entity);
|
||||||
|
|
||||||
const b2ParticleSystemDef m_ParticleSystemDef;
|
|
||||||
b2ParticleSystem *m_ParticleSystem;
|
b2ParticleSystem *m_ParticleSystem;
|
||||||
b2ParticleGroup* t_watergroup;
|
b2ParticleGroup* t_watergroup;
|
||||||
|
|
||||||
|
std::unordered_map<EntityID, const b2ParticleHandle*> m_EntitiesToParticleHandle;
|
||||||
|
std::unordered_map<const b2ParticleHandle*, EntityID> m_ParticleHandleToEntities;
|
||||||
|
|
||||||
void InitializeWater();
|
void InitializeWater();
|
||||||
void SyncWater(); //TODO: Probably remove this
|
void SyncWater(); //TODO: Probably remove this
|
||||||
void CreateParticleGroup(EntityID entity);
|
void CreateParticleGroup(EntityID entity);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class ContactListener : public b2ContactListener
|
class ContactListener : public b2ContactListener
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ void dd::Systems::PhysicsSystem::Initialize()
|
|||||||
{
|
{
|
||||||
m_ContactListener = new ContactListener(this);
|
m_ContactListener = new ContactListener(this);
|
||||||
|
|
||||||
m_Gravity = b2Vec2(0.f, 0.f);
|
m_Gravity = b2Vec2(0.f, -9.82f);
|
||||||
m_PhysicsWorld = new b2World(m_Gravity);
|
m_PhysicsWorld = new b2World(m_Gravity);
|
||||||
|
|
||||||
m_TimeStep = 1.f/60.f;
|
m_TimeStep = 1.f/60.f;
|
||||||
@@ -29,6 +29,9 @@ void dd::Systems::PhysicsSystem::Initialize()
|
|||||||
|
|
||||||
void dd::Systems::PhysicsSystem::InitializeWater()
|
void dd::Systems::PhysicsSystem::InitializeWater()
|
||||||
{
|
{
|
||||||
|
b2ParticleSystemDef m_ParticleSystemDef;
|
||||||
|
m_ParticleSystemDef.radius = 0.1f;
|
||||||
|
|
||||||
m_ParticleSystem = m_PhysicsWorld->CreateParticleSystem(&m_ParticleSystemDef);
|
m_ParticleSystem = m_PhysicsWorld->CreateParticleSystem(&m_ParticleSystemDef);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,8 +76,6 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
|||||||
|
|
||||||
body->SetTransform(position, angle);
|
body->SetTransform(position, angle);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +98,8 @@ 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;
|
||||||
if (m_World->GetEntityParent(entity) == 0) {
|
auto parent = m_World->GetEntityParent(entity);
|
||||||
|
if (parent == 0) {
|
||||||
b2Vec2 position = body->GetPosition();
|
b2Vec2 position = body->GetPosition();
|
||||||
transformComponent->Position.x = position.x;
|
transformComponent->Position.x = position.x;
|
||||||
transformComponent->Position.y = position.y;
|
transformComponent->Position.y = position.y;
|
||||||
@@ -109,6 +111,22 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b2Vec2* positionBuffer = m_ParticleSystem->GetPositionBuffer();
|
||||||
|
for (auto i : m_EntitiesToParticleHandle) {
|
||||||
|
EntityID entity = i.first;
|
||||||
|
b2ParticleHandle* particleH = i.second;
|
||||||
|
|
||||||
|
b2Vec2 positionB2 = positionBuffer[particleH->GetIndex()];
|
||||||
|
glm::vec2 position = glm::vec2(positionB2.x, positionB2.y);
|
||||||
|
|
||||||
|
|
||||||
|
EntityID entityParent = m_World->GetEntityParent(entity);
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||||
@@ -121,10 +139,13 @@ 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);
|
||||||
|
|
||||||
|
if (physicsComponent && waterComponent) {
|
||||||
|
LOG_ERROR("Entity has both water and physics component, this is illegal. The police has been alerted.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (physicsComponent) {
|
if (physicsComponent) {
|
||||||
CreateBody(entity);
|
CreateBody(entity);
|
||||||
}
|
} else if (waterComponent) {
|
||||||
if (waterComponent) {
|
|
||||||
CreateParticleGroup(entity);
|
CreateParticleGroup(entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -182,7 +203,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
|||||||
auto boxComponent = m_World->GetComponent<Components::RectangleShape>(entity);
|
auto boxComponent = m_World->GetComponent<Components::RectangleShape>(entity);
|
||||||
if (boxComponent) {
|
if (boxComponent) {
|
||||||
b2PolygonShape* bShape = new b2PolygonShape();
|
b2PolygonShape* bShape = new b2PolygonShape();
|
||||||
bShape->SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2); //TODO: THIS SUCKS DUDE 4?!?!?!?
|
bShape->SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2);
|
||||||
pShape = bShape;
|
pShape = bShape;
|
||||||
} else {
|
} else {
|
||||||
auto circleComponent = m_World->GetComponent<Components::CircleShape>(entity);
|
auto circleComponent = m_World->GetComponent<Components::CircleShape>(entity);
|
||||||
@@ -194,7 +215,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
|||||||
if (absoluteTransform.Scale.x != absoluteTransform.Scale.y && absoluteTransform.Scale.y != absoluteTransform.Scale.z) {
|
if (absoluteTransform.Scale.x != absoluteTransform.Scale.y && absoluteTransform.Scale.y != absoluteTransform.Scale.z) {
|
||||||
LOG_WARNING("Circles has to be of uniform scale.");
|
LOG_WARNING("Circles has to be of uniform scale.");
|
||||||
}
|
}
|
||||||
pShape->m_radius = absoluteTransform.Scale.x/2; //TODO: THIS ALSO SUCKS 4 WTH
|
pShape->m_radius = absoluteTransform.Scale.x/2;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -231,19 +252,33 @@ void dd::Systems::PhysicsSystem::CreateParticleGroup(EntityID e)
|
|||||||
b2ParticleGroupDef pd;
|
b2ParticleGroupDef pd;
|
||||||
b2PolygonShape shape;
|
b2PolygonShape shape;
|
||||||
|
|
||||||
shape.SetAsBox(transform->Scale.x, transform->Scale.y);
|
shape.SetAsBox(transform->Scale.x/2.f, transform->Scale.y/2.f);
|
||||||
pd.shape = &shape;
|
pd.shape = &shape;
|
||||||
pd.flags = b2_elasticParticle;
|
pd.flags = b2_tensileParticle;
|
||||||
pd.angle = -0.5f;
|
pd.linearVelocity = b2Vec2(0, 5.f);
|
||||||
pd.angularVelocity = 2.0f;
|
|
||||||
pd.position.Set(transform->Position.x, transform->Position.y);
|
pd.position.Set(transform->Position.x, transform->Position.y);
|
||||||
//pd.particleCount = 10;
|
|
||||||
//TODO: PUT IN LIST
|
//TODO: PUT IN LIST
|
||||||
t_watergroup = m_ParticleSystem->CreateParticleGroup(pd);
|
t_watergroup = m_ParticleSystem->CreateParticleGroup(pd);
|
||||||
LOG_INFO("ParticleCount: %i", t_watergroup->GetParticleCount());
|
b2Vec2* t_ParticlePositions = m_ParticleSystem->GetPositionBuffer();
|
||||||
|
for(int i = 0; i < m_ParticleSystem->GetParticleCount(); i++){
|
||||||
|
{
|
||||||
|
auto t_waterparticle = m_World->CreateEntity(e);
|
||||||
|
auto transformChild = m_World->AddComponent<Components::Transform>(t_waterparticle);
|
||||||
|
auto sprite = m_World->AddComponent<Components::Sprite>(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->GetRadius())/transform->Scale;
|
||||||
|
sprite->SpriteFile = "Textures/Ball.png";
|
||||||
|
m_World->CommitEntity(t_waterparticle);
|
||||||
|
|
||||||
|
m_EntitiesToParticleHandle.insert(std::make_pair(t_waterparticle, m_ParticleSystem->GetParticleHandleFromIndex(i)));
|
||||||
|
m_ParticleHandleToEntities.insert(std::make_pair( m_ParticleSystem->GetParticleHandleFromIndex(i), t_waterparticle));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
LOG_INFO("ParticleCount: %i", m_ParticleSystem->GetParticleCount());
|
||||||
}
|
}
|
||||||
|
|
||||||
dd::Systems::PhysicsSystem::~PhysicsSystem()
|
dd::Systems::PhysicsSystem::~PhysicsSystem()
|
||||||
|
|||||||
Reference in New Issue
Block a user