Make box on entity commit
This commit is contained in:
@@ -0,0 +1,21 @@
|
|||||||
|
#ifndef DAYDREAM_CBOXSHAPE_H
|
||||||
|
#define DAYDREAM_CBOXSHAPE_H
|
||||||
|
|
||||||
|
#include "Core/Component.h"
|
||||||
|
|
||||||
|
namespace dd
|
||||||
|
{
|
||||||
|
namespace Components
|
||||||
|
{
|
||||||
|
|
||||||
|
struct BoxShape: public Component
|
||||||
|
{
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //DAYDREAM_CBOXSHAPE_H
|
||||||
+50
-29
@@ -34,6 +34,11 @@
|
|||||||
#include "CTemplate.h"
|
#include "CTemplate.h"
|
||||||
#include "Transform/TransformSystem.h"
|
#include "Transform/TransformSystem.h"
|
||||||
|
|
||||||
|
#include "Physics/PhysicsSystem.h"
|
||||||
|
#include "Physics/CPhysics.h"
|
||||||
|
|
||||||
|
#include "CBoxShape.h"
|
||||||
|
|
||||||
namespace dd
|
namespace dd
|
||||||
{
|
{
|
||||||
|
|
||||||
@@ -42,30 +47,46 @@ class Engine
|
|||||||
public:
|
public:
|
||||||
Engine(int argc, char* argv[])
|
Engine(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
// m_EventBroker = std::make_shared<EventBroker>();
|
m_EventBroker = std::make_shared<EventBroker>();
|
||||||
|
|
||||||
m_Renderer = std::make_shared<BGFXRenderer>();
|
m_Renderer = std::make_shared<BGFXRenderer>();
|
||||||
m_Renderer->SetFullscreen(false);
|
m_Renderer->SetFullscreen(false);
|
||||||
m_Renderer->SetResolution(Rectangle(0, 0, 1280, 720));
|
m_Renderer->SetResolution(Rectangle(0, 0, 1280, 720));
|
||||||
m_Renderer->Initialize();
|
m_Renderer->Initialize();
|
||||||
|
|
||||||
// m_InputManager = std::make_shared<InputManager>(m_Renderer->Window(), m_EventBroker);
|
m_InputManager = std::make_shared<InputManager>(m_Renderer->Window(), m_EventBroker);
|
||||||
//
|
|
||||||
// m_World = std::make_shared<World>(m_EventBroker);
|
m_World = std::make_shared<World>(m_EventBroker);
|
||||||
//
|
|
||||||
// //TODO: Move this out of engine.h
|
//TODO: Move this out of engine.h
|
||||||
// m_World->ComponentFactory.Register<Components::Transform>();
|
m_World->ComponentFactory.Register<Components::Transform>();
|
||||||
// m_World->SystemFactory.Register<Systems::TransformSystem>([this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); });
|
m_World->SystemFactory.Register<Systems::TransformSystem>([this]() { return new Systems::TransformSystem(m_World.get(), m_EventBroker); });
|
||||||
// m_World->AddSystem<Systems::TransformSystem>();
|
m_World->AddSystem<Systems::TransformSystem>();
|
||||||
// m_World->ComponentFactory.Register<Components::Model>();
|
|
||||||
// m_World->ComponentFactory.Register<Components::Template>();
|
m_World->ComponentFactory.Register<Components::BoxShape>();
|
||||||
// m_World->Initialize();
|
m_World->ComponentFactory.Register<Components::Physics>();
|
||||||
|
m_World->SystemFactory.Register<Systems::PhysicsSystem>([this]() { return new Systems::PhysicsSystem(m_World.get(), m_EventBroker); });
|
||||||
|
m_World->AddSystem<Systems::PhysicsSystem>();
|
||||||
|
|
||||||
|
m_World->ComponentFactory.Register<Components::Model>();
|
||||||
|
m_World->ComponentFactory.Register<Components::Template>();
|
||||||
|
m_World->Initialize();
|
||||||
|
|
||||||
|
auto ent = m_World->CreateEntity();
|
||||||
|
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::Model> model = m_World->AddComponent<Components::Model>(ent);
|
||||||
|
model->ModelFile = "Models/Core/UnitSphere.obj";
|
||||||
|
|
||||||
|
std::shared_ptr<Components::BoxShape> boxShape = m_World->AddComponent<Components::BoxShape>(ent);
|
||||||
|
boxShape->x = 5.f;
|
||||||
|
boxShape->y = 5.f;
|
||||||
|
|
||||||
|
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
|
||||||
|
physics->Static = true;
|
||||||
|
|
||||||
|
m_World->CommitEntity(ent);
|
||||||
|
|
||||||
// auto ent = m_World->CreateEntity();
|
|
||||||
// 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::Model> model = m_World->AddComponent<Components::Model>(ent);
|
|
||||||
// model->ModelFile = "Models/Core/UnitSphere.obj";
|
|
||||||
|
|
||||||
|
|
||||||
m_LastTime = glfwGetTime();
|
m_LastTime = glfwGetTime();
|
||||||
@@ -79,26 +100,26 @@ public:
|
|||||||
double dt = currentTime - m_LastTime;
|
double dt = currentTime - m_LastTime;
|
||||||
m_LastTime = currentTime;
|
m_LastTime = currentTime;
|
||||||
|
|
||||||
// ResourceManager::Update();
|
ResourceManager::Update();
|
||||||
//
|
|
||||||
// // Update input
|
// // Update input
|
||||||
// m_InputManager->Update(dt);
|
m_InputManager->Update(dt);
|
||||||
//
|
|
||||||
// m_World->Update(dt);
|
m_World->Update(dt);
|
||||||
//
|
//
|
||||||
// if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) {
|
// if (glfwGetKey(m_Renderer->Window(), GLFW_KEY_R)) {
|
||||||
// ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl");
|
// ResourceManager::Reload("Shaders/Deferred/3/Fragment.glsl");
|
||||||
// }
|
// }
|
||||||
//
|
|
||||||
// //TODO Fill up the renderQueue with models (Temp fix)
|
//TODO Fill up the renderQueue with models (Temp fix)
|
||||||
// TEMPAddToRenderQueue();
|
// TEMPAddToRenderQueue();
|
||||||
//
|
|
||||||
// // Render scene
|
// Render scene
|
||||||
// //TODO send renderqueue to draw.
|
//TODO send renderqueue to draw.
|
||||||
m_Renderer->Draw(m_RendererQueue);
|
m_Renderer->Draw(m_RendererQueue);
|
||||||
|
|
||||||
// Swap event queues
|
// Swap event queues
|
||||||
// m_EventBroker->Clear();
|
m_EventBroker->Clear();
|
||||||
|
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
@@ -168,7 +189,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//std::shared_ptr<ResourceManager> m_ResourceManager;
|
std::shared_ptr<ResourceManager> m_ResourceManager;
|
||||||
std::shared_ptr<EventBroker> m_EventBroker;
|
std::shared_ptr<EventBroker> m_EventBroker;
|
||||||
std::shared_ptr<BGFXRenderer> m_Renderer;
|
std::shared_ptr<BGFXRenderer> m_Renderer;
|
||||||
RenderQueueCollection m_RendererQueue;
|
RenderQueueCollection m_RendererQueue;
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef DAYDREAM_CPHYSICS_H
|
||||||
|
#define DAYDREAM_CPHYSICS_H
|
||||||
|
#include "Core/Component.h"
|
||||||
|
|
||||||
|
namespace dd
|
||||||
|
{
|
||||||
|
namespace Components
|
||||||
|
{
|
||||||
|
|
||||||
|
struct Physics: public Component
|
||||||
|
{
|
||||||
|
//idk if anything should be here :)
|
||||||
|
bool Static = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif //DAYDREAM_CPHYSICS_H
|
||||||
@@ -1,39 +1,56 @@
|
|||||||
#ifndef DAYDREAM_PHYSICSSYSTEM_H
|
#ifndef DAYDREAM_PHYSICSSYSTEM_H
|
||||||
#define DAYDREAM_PHYSICSSYSTEM_H
|
#define DAYDREAM_PHYSICSSYSTEM_H
|
||||||
|
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "Core/System.h"
|
#include "Core/System.h"
|
||||||
|
#include "Core/World.h"
|
||||||
|
#include "Core/CBoxShape.h"
|
||||||
|
#include "Core/Physics/CPhysics.h"
|
||||||
#include <Box2D/Box2D.h>
|
#include <Box2D/Box2D.h>
|
||||||
|
#include <Core/CTransform.h>
|
||||||
|
|
||||||
|
|
||||||
namespace dd
|
namespace dd
|
||||||
{
|
{
|
||||||
|
|
||||||
namespace Systems {
|
namespace Systems
|
||||||
|
{
|
||||||
|
|
||||||
|
//KOLLA http://box2d.org/manual.pdf p52 Pre-Solve Event
|
||||||
|
|
||||||
class PhysicsSystem : public System {
|
class PhysicsSystem : public System
|
||||||
|
{
|
||||||
|
public:
|
||||||
PhysicsSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
|
PhysicsSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
|
||||||
: System(world, eventBroker) {}
|
: System(world, eventBroker) {}
|
||||||
|
|
||||||
~PhysicsSystem();
|
void RegisterComponents(ComponentFactory* cf) override;
|
||||||
|
void Initialize() override;
|
||||||
void Initialize();
|
|
||||||
|
|
||||||
// Called once per system every tick
|
// Called once per system every tick
|
||||||
void Update(double dt);
|
void Update(double dt) override;
|
||||||
// Called once for every entity in the world every tick
|
// Called once for every entity in the world every tick
|
||||||
void UpdateEntity(double dt, EntityID entity, EntityID parent);
|
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
||||||
|
|
||||||
// Called when components are committed to an entity
|
// Called when components are committed to an entity
|
||||||
void OnEntityCommit(EntityID entity);
|
void OnEntityCommit(EntityID entity) override;
|
||||||
|
|
||||||
// Called when an entity is removed
|
// Called when an entity is removed
|
||||||
void OnEntityRemoved(EntityID entity);
|
void OnEntityRemoved(EntityID entity) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
b2Vec2 m_Gravity;
|
b2Vec2 m_Gravity;
|
||||||
b2World* m_PhysicsWorld;
|
b2World* m_PhysicsWorld;
|
||||||
float m_TimeStep;
|
float m_TimeStep;
|
||||||
int m_VelocityIterations, m_PositionIterations;
|
int m_VelocityIterations, m_PositionIterations;
|
||||||
|
|
||||||
|
std::unordered_map<EntityID, b2Body*> m_Bodies;
|
||||||
|
|
||||||
|
void CreateBody(EntityID entity);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ set(SOURCE_FILES
|
|||||||
${SOURCE_FILES_Rendering}
|
${SOURCE_FILES_Rendering}
|
||||||
${SOURCE_FILES_Transform}
|
${SOURCE_FILES_Transform}
|
||||||
${SOURCE_FILES_Game}
|
${SOURCE_FILES_Game}
|
||||||
Physics/PhysicsSystem.cpp ../../include/Core/Physics/PhysicsSystem.h)
|
Physics/PhysicsSystem.cpp ../../include/Core/Physics/PhysicsSystem.h ../../include/Core/CBoxShape.h ../../include/Core/Physics/CPhysics.h)
|
||||||
|
|
||||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
|
||||||
|
|||||||
@@ -1,7 +1,15 @@
|
|||||||
#include "PrecompiledHeader.h"
|
#include "PrecompiledHeader.h"
|
||||||
#include "Core/Physics/PhysicsSystem.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_Gravity = b2Vec2(0.f, 9.82f);
|
||||||
m_PhysicsWorld = new b2World(m_Gravity);
|
m_PhysicsWorld = new b2World(m_Gravity);
|
||||||
|
|
||||||
@@ -10,25 +18,78 @@ void dd::Systems::PhysicsSystem::Initialize(){
|
|||||||
m_PositionIterations = 2;
|
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);
|
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));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user