From 963e492f9cab36cfb92625af3f3590ce669b6e41 Mon Sep 17 00:00:00 2001 From: viktorljung Date: Thu, 10 Sep 2015 22:24:19 +0100 Subject: [PATCH 1/2] CircleShape --- include/Core/Engine.h | 8 +++-- include/{Core => Physics}/CBoxShape.h | 6 ++-- include/Physics/CCircleShape.h | 20 ++++++++++++ include/Physics/EContact.h | 19 +++++------ include/Physics/ESetImpulse.h | 22 +++++++++++++ include/Physics/PhysicsSystem.h | 3 +- src/game/CMakeLists.txt | 9 +++++- src/game/Physics/PhysicsSystem.cpp | 45 ++++++++++++++++++--------- 8 files changed, 101 insertions(+), 31 deletions(-) rename include/{Core => Physics}/CBoxShape.h (70%) create mode 100644 include/Physics/CCircleShape.h create mode 100644 include/Physics/ESetImpulse.h diff --git a/include/Core/Engine.h b/include/Core/Engine.h index a367fc6..4a8efbc 100644 --- a/include/Core/Engine.h +++ b/include/Core/Engine.h @@ -38,7 +38,8 @@ #include "Physics/PhysicsSystem.h" #include "Physics/CPhysics.h" -#include "CBoxShape.h" + +#include "Physics/CBoxShape.h" namespace dd { @@ -83,10 +84,11 @@ public: std::shared_ptr transform = m_World->AddComponent(ent); transform->Position = glm::vec3(0.f, 0.f, -10.f); + std::shared_ptr sprite = m_World->AddComponent(ent); sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; - std::shared_ptr boxShape = m_World->AddComponent(ent); + std::shared_ptr circleShape = m_World->AddComponent(ent); std::shared_ptr physics = m_World->AddComponent(ent); physics->Static = false; @@ -100,7 +102,7 @@ public: std::shared_ptr transform = m_World->AddComponent(ent); transform->Position = glm::vec3(0.f, -3.f, -9.f); transform->Scale = glm::vec3(2.f, 0.5f, 1.f); - //transform->Orientation = glm::rotate(transform->Orientation, glm::radians(45.f), glm::vec3(0, 0, -1)); + //transform->Orientation = glm::rotate(transform->Orientation, glm::radians(35.f), glm::vec3(0, 0, -1)); std::shared_ptr sprite = m_World->AddComponent(ent); sprite->SpriteFile = "Textures/Core/ErrorTexture.png"; diff --git a/include/Core/CBoxShape.h b/include/Physics/CBoxShape.h similarity index 70% rename from include/Core/CBoxShape.h rename to include/Physics/CBoxShape.h index cc05ca4..fcdda7a 100644 --- a/include/Core/CBoxShape.h +++ b/include/Physics/CBoxShape.h @@ -8,10 +8,10 @@ namespace dd namespace Components { - struct RectangleShape : public Component - { +struct RectangleShape : public Component +{ - }; +}; } diff --git a/include/Physics/CCircleShape.h b/include/Physics/CCircleShape.h new file mode 100644 index 0000000..a7a477b --- /dev/null +++ b/include/Physics/CCircleShape.h @@ -0,0 +1,20 @@ +#ifndef CCIRCLESHAPE_H__ +#define CCIRCLESHAPE_H__ + +#include "Core/Component.h" + +namespace dd +{ +namespace Components +{ + +struct CircleShape : public Component +{ + // Circles are circles +}; + +} + +} + +#endif diff --git a/include/Physics/EContact.h b/include/Physics/EContact.h index b2d5f57..2969a0c 100644 --- a/include/Physics/EContact.h +++ b/include/Physics/EContact.h @@ -12,16 +12,17 @@ namespace dd { - namespace Events - { - struct Contact : Event - { - EntityID Entity1; - EntityID Entity2; - glm::vec2 ContactPoint; - }; - } +namespace Events +{ +struct Contact : Event +{ + EntityID Entity1; + EntityID Entity2; + glm::vec2 ContactPoint; +}; + +} } #endif //Events_ECONTACT_h__ diff --git a/include/Physics/ESetImpulse.h b/include/Physics/ESetImpulse.h new file mode 100644 index 0000000..b66746d --- /dev/null +++ b/include/Physics/ESetImpulse.h @@ -0,0 +1,22 @@ +#ifndef ESETIMPULSE_H__ +#define ESETIMPULSE_H__ + +#include "Core/EventBroker.h" +#include "Core/Entity.h" + +namespace dd +{ +namespace Events +{ + +struct Contact : Event +{ + EntityID Entity; + glm::vec2 Direction; + float Strength; +}; + +} +} + +#endif diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index add84dc..b7c91e4 100644 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -5,12 +5,13 @@ #include "Core/System.h" #include "Core/World.h" -#include "Core/CBoxShape.h" +#include "Physics/CBoxShape.h" #include "Physics/CPhysics.h" #include #include "Core/CTransform.h" #include "Transform/TransformSystem.h" #include "Physics/EContact.h" +#include "Physics/CCircleShape.h" namespace dd diff --git a/src/game/CMakeLists.txt b/src/game/CMakeLists.txt index c1c8611..0577ab1 100755 --- a/src/game/CMakeLists.txt +++ b/src/game/CMakeLists.txt @@ -56,6 +56,12 @@ file(GLOB SOURCE_FILES_Transform ) source_group(Transform FILES ${SOURCE_FILES_Transform}) +file(GLOB SOURCE_FILES_Physics + "${INCLUDE_PATH}/Physics/*.h" + "Physics/*.cpp" +) +source_group(Physics FILES ${SOURCE_FILES_Physics}) + file(GLOB SOURCE_FILES_Game "${INCLUDE_PATH}/Game/*.h" "Game/*.cpp" @@ -68,8 +74,9 @@ set(SOURCE_FILES ${SOURCE_FILES_Input} ${SOURCE_FILES_Rendering} ${SOURCE_FILES_Transform} + ${SOURCE_FILES_Physics} ${SOURCE_FILES_Game} - Physics/PhysicsSystem.cpp ../../include/Physics/PhysicsSystem.h ../../include/Core/CBoxShape.h ../../include/Physics/CPhysics.h ../../include/Physics/EContact.h) +) if(CMAKE_COMPILER_IS_GNUCXX) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index b9e2421..b202abd 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -5,7 +5,7 @@ void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf) { - + cf->Register(); } void dd::Systems::PhysicsSystem::Initialize() @@ -57,11 +57,10 @@ void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, Entity void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity) { - auto physicsComponent = m_World->GetComponent(entity); - if(physicsComponent) + if(physicsComponent) { CreateBody(entity); - + } } @@ -94,28 +93,44 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) bodyDef.angle = glm::degrees(-glm::eulerAngles(absoluteTransform.Orientation).z); //TODO: CHECK IF THIS IS CORRECT - if(physicsComponent->Static) + if (physicsComponent->Static) { bodyDef.type = b2_staticBody; - else + } else { bodyDef.type = b2_dynamicBody; + } + b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef); - b2PolygonShape pShape; + b2Shape* pShape; auto boxComponent = m_World->GetComponent(entity); - if(boxComponent){ + if (boxComponent) { + b2PolygonShape* bShape = new b2PolygonShape(); + bShape->SetAsBox(absoluteTransform.Scale.x/4, absoluteTransform.Scale.y/4); //TODO: THIS SUCKS DUDE + pShape = bShape; + } else { + auto circleComponent = m_World->GetComponent(entity); + if (circleComponent) { + pShape = new b2CircleShape(); + pShape->m_radius = absoluteTransform.Scale.x; + + + if (absoluteTransform.Scale.x != absoluteTransform.Scale.y && absoluteTransform.Scale.y != absoluteTransform.Scale.z) { + LOG_WARNING("Circles has to be of uniform scale."); + } + pShape->m_radius = absoluteTransform.Scale.x/4; - pShape.SetAsBox(absoluteTransform.Scale.x/4, absoluteTransform.Scale.y/4); //TODO: THIS SUCKS DUDE } - - if(physicsComponent->Static){ - body->CreateFixture(&pShape, 0); //Density kanske ska vara 0 på statiska kroppar } - else{ + + if(physicsComponent->Static) { + body->CreateFixture(pShape, 0); //Density kanske ska vara 0 på statiska kroppar + } + else { //TODO: FIX THIS SHIT INTO COMPONENTS b2FixtureDef fixtureDef; - fixtureDef.shape = &pShape; + fixtureDef.shape = pShape; fixtureDef.density = 1.f; fixtureDef.restitution = 1.0f; fixtureDef.friction = 0.3f; @@ -123,6 +138,8 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity) } + delete pShape; + m_EntitiesToBodies.insert(std::make_pair(entity, body)); m_BodiesToEntities.insert(std::make_pair(body, entity)); } From 71cdd1f795275c574dba7c21e413ee406c62d47f Mon Sep 17 00:00:00 2001 From: viktorljung Date: Thu, 10 Sep 2015 22:43:25 +0100 Subject: [PATCH 2/2] SetImpulse event --- include/Physics/ESetImpulse.h | 6 +++--- include/Physics/PhysicsSystem.h | 5 +++++ src/game/Physics/PhysicsSystem.cpp | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/include/Physics/ESetImpulse.h b/include/Physics/ESetImpulse.h index b66746d..eff3f15 100644 --- a/include/Physics/ESetImpulse.h +++ b/include/Physics/ESetImpulse.h @@ -9,11 +9,11 @@ namespace dd namespace Events { -struct Contact : Event +struct SetImpulse : Event { EntityID Entity; - glm::vec2 Direction; - float Strength; + glm::vec2 Impulse; + glm::vec2 Point; }; } diff --git a/include/Physics/PhysicsSystem.h b/include/Physics/PhysicsSystem.h index b7c91e4..265e511 100644 --- a/include/Physics/PhysicsSystem.h +++ b/include/Physics/PhysicsSystem.h @@ -11,7 +11,9 @@ #include "Core/CTransform.h" #include "Transform/TransformSystem.h" #include "Physics/EContact.h" +#include "Physics/ESetImpulse.h" #include "Physics/CCircleShape.h" +#include "Core/EventBroker.h" namespace dd @@ -32,6 +34,9 @@ public: ~PhysicsSystem(); + EventRelay m_SetImpulse; + bool SetImpulse(const Events::SetImpulse &event); + void RegisterComponents(ComponentFactory* cf) override; void Initialize() override; diff --git a/src/game/Physics/PhysicsSystem.cpp b/src/game/Physics/PhysicsSystem.cpp index b202abd..17fd361 100644 --- a/src/game/Physics/PhysicsSystem.cpp +++ b/src/game/Physics/PhysicsSystem.cpp @@ -21,6 +21,25 @@ void dd::Systems::PhysicsSystem::Initialize() m_PhysicsWorld->SetContactListener(m_ContactListener); + + EVENT_SUBSCRIBE_MEMBER(m_SetImpulse, PhysicsSystem::SetImpulse); +} + +bool dd::Systems::PhysicsSystem::SetImpulse(const Events::SetImpulse &event) +{ + b2Body* body = m_EntitiesToBodies[event.Entity]; + + b2Vec2 impulse; + impulse.x = event.Impulse.x; + impulse.y = event.Impulse.y; + + b2Vec2 point; + point.x = event.Point.x; + point.y = event.Point.y; + + body->ApplyLinearImpulse(impulse, point, true); + + return true; } void dd::Systems::PhysicsSystem::Update(double dt)