Merge remote-tracking branch 'origin/master' into LevelSystemAndSuch

This commit is contained in:
PlatinumSkink
2015-09-10 22:57:20 +02:00
8 changed files with 125 additions and 31 deletions
+5 -3
View File
@@ -40,7 +40,8 @@
#include "Physics/PhysicsSystem.h"
#include "Physics/CPhysics.h"
#include "CBoxShape.h"
#include "Physics/CBoxShape.h"
namespace dd
{
@@ -89,10 +90,11 @@ public:
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(0.f, 0.f, -10.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
std::shared_ptr<Components::RectangleShape> boxShape = m_World->AddComponent<Components::RectangleShape>(ent);
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
physics->Static = false;
@@ -106,7 +108,7 @@ public:
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(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<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Core/ErrorTexture.png";
@@ -8,10 +8,10 @@ namespace dd
namespace Components
{
struct RectangleShape : public Component
{
struct RectangleShape : public Component
{
};
};
}
+20
View File
@@ -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
+10 -9
View File
@@ -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__
+22
View File
@@ -0,0 +1,22 @@
#ifndef ESETIMPULSE_H__
#define ESETIMPULSE_H__
#include "Core/EventBroker.h"
#include "Core/Entity.h"
namespace dd
{
namespace Events
{
struct SetImpulse : Event
{
EntityID Entity;
glm::vec2 Impulse;
glm::vec2 Point;
};
}
}
#endif
+7 -1
View File
@@ -5,12 +5,15 @@
#include "Core/System.h"
#include "Core/World.h"
#include "Core/CBoxShape.h"
#include "Physics/CBoxShape.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/CCircleShape.h"
#include "Core/EventBroker.h"
namespace dd
@@ -31,6 +34,9 @@ public:
~PhysicsSystem();
EventRelay<PhysicsSystem, Events::SetImpulse> m_SetImpulse;
bool SetImpulse(const Events::SetImpulse &event);
void RegisterComponents(ComponentFactory* cf) override;
void Initialize() override;
+8 -1
View File
@@ -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")
+50 -14
View File
@@ -5,7 +5,7 @@
void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
{
cf->Register<Components::CircleShape>();
}
void dd::Systems::PhysicsSystem::Initialize()
@@ -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)
@@ -57,11 +76,10 @@ void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, Entity
void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
{
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
if(physicsComponent)
if(physicsComponent) {
CreateBody(entity);
}
}
@@ -94,28 +112,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<Components::RectangleShape>(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<Components::CircleShape>(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 +157,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));
}