Bodies are now removed properly

This commit is contained in:
viktorljung
2015-10-06 18:32:22 +02:00
parent fb8d6f210a
commit a2dc76051f
3 changed files with 29 additions and 56 deletions
+10 -39
View File
@@ -237,51 +237,22 @@ public:
//TODO: Why does the ball not collide with these bricks? //TODO: Why does the ball not collide with these bricks?
//BottomBox //BottomBox
{ {
auto topWall = m_World->CreateEntity(); auto BottomWall = m_World->CreateEntity();
auto transform = m_World->AddComponent<Components::Transform>(topWall); auto transform = m_World->AddComponent<Components::Transform>(BottomWall);
transform->Position = glm::vec3(0.f, -6.f, -9.9f); transform->Position = glm::vec3(0.f, -6.f, -10.f);
transform->Scale = glm::vec3(10.f, 0.5f, 1.f); transform->Scale = glm::vec3(20.f, 0.5f, 1.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(topWall); std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(BottomWall);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> rectangleShape = m_World->AddComponent<Components::RectangleShape>(topWall); std::shared_ptr<Components::RectangleShape> rectangleShape = m_World->AddComponent<Components::RectangleShape>(BottomWall);
rectangleShape->Dimensions = glm::vec2(10.f, 0.5f); rectangleShape->Dimensions = glm::vec2(20.f, 0.5f);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(topWall); std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(BottomWall);
physics->CollisionType = CollisionType::Type::Static; physics->CollisionType = CollisionType::Type::Static;
m_World->CommitEntity(topWall); m_World->CommitEntity(BottomWall);
} }
//SideBox
// {
// auto topWall = m_World->CreateEntity();
// std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
// transform->Position = glm::vec3(3.f, -3.0f, -9.9f);
// 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(-4.f, -3.0f, -9.9f);
// 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>( std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(topWall);
topWall);
transform->Position = glm::vec3(0.f, 6.f, -10.f); transform->Position = glm::vec3(0.f, 6.f, -10.f);
transform->Scale = glm::vec3(20.f, 0.5f, 1.f); transform->Scale = glm::vec3(20.f, 0.5f, 1.f);
+9 -6
View File
@@ -72,6 +72,8 @@ void dd::Systems::BallSystem::Update(double dt)
void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{ {
//TODO: When the ball collides with 2 bricks on the same frame it is reflected and then reflected again, make it reflect only once like a list or something?
auto ballComponent = m_World->GetComponent<Components::Ball>(entity); auto ballComponent = m_World->GetComponent<Components::Ball>(entity);
if (IsPaused()) { if (IsPaused()) {
if (ballComponent != nullptr) { if (ballComponent != nullptr) {
@@ -128,7 +130,8 @@ void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID
EventBroker->Publish(e); EventBroker->Publish(e);
return; return;
} }
} else if (transformBall->Position.x > EdgeX()) { } //Removing this made the wall collisions work again
/*else if (transformBall->Position.x > EdgeX()) {
if (transformBall->Velocity.x > 0) { if (transformBall->Velocity.x > 0) {
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(1, 0)); glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(1, 0));
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f); transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
@@ -143,7 +146,7 @@ void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID
glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(0, 1)); glm::vec2 reflectedVelocity = glm::reflect(glm::vec2(transformBall->Velocity.x, transformBall->Velocity.y), glm::vec2(0, 1));
transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f); transformBall->Velocity = glm::vec3(reflectedVelocity, 0.f);
} }
} }*/
} }
if (Lives() != PastLives()) { if (Lives() != PastLives()) {
@@ -250,10 +253,10 @@ bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
if (m_World->GetProperty<std::string>(otherEntitiy, "Name") == "Pad"){ if (m_World->GetProperty<std::string>(otherEntitiy, "Name") == "Pad"){
Events::HitPad e; Events::HitPad e;
EventBroker->Publish(e); EventBroker->Publish(e);
Events::HitLag el; // Events::HitLag el;
el.Time = 0.1f; // el.Time = 0.1f;
el.Type = "All"; //el.Type = "All";
EventBroker->Publish(el); // EventBroker->Publish(el);
auto padTransform = m_World->GetComponent<Components::Transform>(otherEntitiy); auto padTransform = m_World->GetComponent<Components::Transform>(otherEntitiy);
float x = (ballTransform->Position.x - padTransform->Position.x) * XMovementMultiplier(); float x = (ballTransform->Position.x - padTransform->Position.x) * XMovementMultiplier();
+10 -11
View File
@@ -27,9 +27,8 @@ void dd::Systems::PhysicsSystem::InitializeWater()
bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event) bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
{ {
b2Body* body = m_EntitiesToBodies[event.Entity]; auto it = m_EntitiesToBodies.find(event.Entity);
if(it == m_EntitiesToBodies.end()) {
if (body == nullptr) {
LOG_ERROR("Entity: %i, Tried to set an impulse on a body that does not exsist", event.Entity); LOG_ERROR("Entity: %i, Tried to set an impulse on a body that does not exsist", event.Entity);
return false; return false;
} }
@@ -43,7 +42,7 @@ bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event)
point.y = event.Point.y; point.y = event.Point.y;
Impulse i; Impulse i;
i.Body = body; i.Body = it->second;
i.Impulse = impulse; i.Impulse = impulse;
i.Point = point; i.Point = point;
@@ -244,21 +243,21 @@ void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity) void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity)
{ {
auto physics = m_World->GetComponent<Components::Physics>(entity); /* auto physics = m_World->GetComponent<Components::Physics>(entity);
if (physics == nullptr) { if (physics == nullptr) {
return; return;
} }*/
b2Body* body = m_EntitiesToBodies[entity]; auto it = m_EntitiesToBodies.find(entity);
if (body == nullptr) { if(it == m_EntitiesToBodies.end()) {
LOG_ERROR("Trying to remove non-exsisting body, Entity: %i", entity); LOG_ERROR("Trying to remove non-exsisting body, Entity: %i", entity);
return; return;
} }
LOG_INFO("Removing entity %i", entity);
it->second->GetWorld()->DestroyBody(it->second);
m_BodiesToEntities.erase(it->second);
m_EntitiesToBodies.erase(entity); m_EntitiesToBodies.erase(entity);
m_BodiesToEntities.erase(body);
body->GetWorld()->DestroyBody(body);
//delete body; //delete body;
} }