Make box on entity commit

This commit is contained in:
viktorljung
2015-09-10 16:27:14 +01:00
parent ce342f695f
commit a98806d551
6 changed files with 186 additions and 46 deletions
+68 -7
View File
@@ -1,7 +1,15 @@
#include "PrecompiledHeader.h"
#include "Core/Physics/PhysicsSystem.h"
void dd::Systems::PhysicsSystem::Initialize(){
void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
{
}
void dd::Systems::PhysicsSystem::Initialize()
{
m_Gravity = b2Vec2(0.f, 9.82f);
m_PhysicsWorld = new b2World(m_Gravity);
@@ -10,25 +18,78 @@ void dd::Systems::PhysicsSystem::Initialize(){
m_PositionIterations = 2;
}
void dd::Systems::PhysicsSystem::Update(double dt){
void dd::Systems::PhysicsSystem::Update(double dt)
{
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
}
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent){
void dd::Systems::PhysicsSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
{
}
void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity){
void dd::Systems::PhysicsSystem::OnEntityCommit(EntityID entity)
{
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
if(physicsComponent)
CreateBody(entity);
}
void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity){
void dd::Systems::PhysicsSystem::OnEntityRemoved(EntityID entity)
{
}
dd::Systems::PhysicsSystem::~PhysicsSystem(){
delete m_PhysicsWorld;
void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
{
auto physicsComponent = m_World->GetComponent<Components::Physics>(entity);
if(!physicsComponent)
return;
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
if(!transformComponent)
return;
b2BodyDef bodyDef;
bodyDef.position.Set(transformComponent->Position.x, transformComponent->Position.y);
bodyDef.angle = glm::eulerAngles(transformComponent->Orientation).z; //TODO: CHECK IF THIS IS CORRECT
if(physicsComponent->Static)
bodyDef.type = b2_staticBody;
else
bodyDef.type = b2_dynamicBody;
b2Body* body = m_PhysicsWorld->CreateBody(&bodyDef);
b2PolygonShape pShape;
auto boxComponent = m_World->GetComponent<Components::BoxShape>(entity);
if(boxComponent){
pShape.SetAsBox(boxComponent->x, boxComponent->y);
}
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.density = 1.f;
fixtureDef.restitution = 1.f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
}
m_Bodies.insert(std::make_pair(entity, body));
}