ContactListener and contact events
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
#ifndef DAYDREAM_PHYSICSSYSTEM_H
|
||||
#define DAYDREAM_PHYSICSSYSTEM_H
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Core/System.h"
|
||||
#include "Core/World.h"
|
||||
#include "Core/CBoxShape.h"
|
||||
#include "Core/Physics/CPhysics.h"
|
||||
#include <Box2D/Box2D.h>
|
||||
#include "Core/CTransform.h"
|
||||
#include "Transform/TransformSystem.h"
|
||||
|
||||
|
||||
namespace dd
|
||||
{
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
|
||||
//KOLLA http://box2d.org/manual.pdf p52 Pre-Solve Event
|
||||
|
||||
class PhysicsSystem : public System
|
||||
{
|
||||
public:
|
||||
PhysicsSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
|
||||
: System(world, eventBroker) {}
|
||||
|
||||
void RegisterComponents(ComponentFactory* cf) override;
|
||||
void Initialize() override;
|
||||
|
||||
// Called once per system every tick
|
||||
void Update(double dt) override;
|
||||
// Called once for every entity in the world every tick
|
||||
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
||||
|
||||
// Called when components are committed to an entity
|
||||
void OnEntityCommit(EntityID entity) override;
|
||||
|
||||
// Called when an entity is removed
|
||||
void OnEntityRemoved(EntityID entity) override;
|
||||
|
||||
private:
|
||||
|
||||
|
||||
|
||||
b2Vec2 m_Gravity;
|
||||
b2World* m_PhysicsWorld;
|
||||
float m_TimeStep;
|
||||
int m_VelocityIterations, m_PositionIterations;
|
||||
|
||||
std::unordered_map<EntityID, b2Body*> m_Bodies;
|
||||
|
||||
void CreateBody(EntityID entity);
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif //DAYDREAM_PHYSICSSYSTEM_H
|
||||
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by Viktor on 10/09/2015.
|
||||
//
|
||||
|
||||
#ifndef Events_ECONTACT_h__
|
||||
#define Events_ECONTACT_h__
|
||||
|
||||
|
||||
#include <glm/detail/type_vec.hpp>
|
||||
#include "Core/EventBroker.h"
|
||||
#include "Core/Entity.h"
|
||||
|
||||
namespace dd
|
||||
{
|
||||
namespace Events
|
||||
{
|
||||
struct Contact : Event
|
||||
{
|
||||
EntityID Entity1;
|
||||
EntityID Entity2;
|
||||
glm::vec2 ContactPoint;
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif //Events_ECONTACT_h__
|
||||
@@ -0,0 +1,94 @@
|
||||
#ifndef DAYDREAM_PHYSICSSYSTEM_H
|
||||
#define DAYDREAM_PHYSICSSYSTEM_H
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "Core/System.h"
|
||||
#include "Core/World.h"
|
||||
#include "Core/CBoxShape.h"
|
||||
#include "Physics/CPhysics.h"
|
||||
#include <Box2D/Box2D.h>
|
||||
#include "Core/CTransform.h"
|
||||
#include "Transform/TransformSystem.h"
|
||||
#include "Physics/EContact.h"
|
||||
|
||||
|
||||
namespace dd
|
||||
{
|
||||
|
||||
namespace Systems
|
||||
{
|
||||
|
||||
//KOLLA http://box2d.org/manual.pdf p52 Pre-Solve Event
|
||||
|
||||
class PhysicsSystem : public System
|
||||
{
|
||||
friend class ContractListener;
|
||||
|
||||
public:
|
||||
PhysicsSystem(World* world, std::shared_ptr<dd::EventBroker> eventBroker)
|
||||
: System(world, eventBroker) {}
|
||||
|
||||
~PhysicsSystem();
|
||||
|
||||
void RegisterComponents(ComponentFactory* cf) override;
|
||||
void Initialize() override;
|
||||
|
||||
// Called once per system every tick
|
||||
void Update(double dt) override;
|
||||
// Called once for every entity in the world every tick
|
||||
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
|
||||
|
||||
// Called when components are committed to an entity
|
||||
void OnEntityCommit(EntityID entity) override;
|
||||
|
||||
// Called when an entity is removed
|
||||
void OnEntityRemoved(EntityID entity) override;
|
||||
|
||||
private:
|
||||
b2Vec2 m_Gravity;
|
||||
b2World* m_PhysicsWorld;
|
||||
float m_TimeStep;
|
||||
int m_VelocityIterations, m_PositionIterations;
|
||||
|
||||
std::unordered_map<EntityID, b2Body*> m_EntitiesToBodies;
|
||||
std::unordered_map<b2Body*, EntityID> m_BodiesToEntities;
|
||||
|
||||
void CreateBody(EntityID entity);
|
||||
|
||||
|
||||
class ContactListener : public b2ContactListener
|
||||
{
|
||||
public:
|
||||
ContactListener(PhysicsSystem* physicsSystem)
|
||||
: m_PhysicsSystem(physicsSystem) { }
|
||||
|
||||
void BeginContact(b2Contact* contact)
|
||||
{
|
||||
b2Vec2 contactPoint = contact->GetManifold()->localPoint;
|
||||
|
||||
Events::Contact e;
|
||||
e.ContactPoint = glm::vec2(contactPoint.x, contactPoint.y);
|
||||
e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
|
||||
e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
|
||||
|
||||
m_PhysicsSystem->EventBroker->Publish(e);
|
||||
}
|
||||
void EndContact(b2Contact* contact)
|
||||
{ /* handle end event */ }
|
||||
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
|
||||
{ /* handle pre-solve event */ }
|
||||
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
|
||||
{ /* handle post-solve event */ }
|
||||
|
||||
private:
|
||||
PhysicsSystem* m_PhysicsSystem;
|
||||
};
|
||||
|
||||
ContactListener* m_ContactListener;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif //DAYDREAM_PHYSICSSYSTEM_H
|
||||
@@ -69,7 +69,7 @@ set(SOURCE_FILES
|
||||
${SOURCE_FILES_Rendering}
|
||||
${SOURCE_FILES_Transform}
|
||||
${SOURCE_FILES_Game}
|
||||
Physics/PhysicsSystem.cpp ../../include/Core/Physics/PhysicsSystem.h ../../include/Core/CBoxShape.h ../../include/Core/Physics/CPhysics.h)
|
||||
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")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Core/Physics/PhysicsSystem.h"
|
||||
#include "Physics/PhysicsSystem.h"
|
||||
|
||||
|
||||
|
||||
@@ -10,19 +10,24 @@ void dd::Systems::PhysicsSystem::RegisterComponents(ComponentFactory* cf)
|
||||
|
||||
void dd::Systems::PhysicsSystem::Initialize()
|
||||
{
|
||||
m_ContactListener = new ContactListener(this);
|
||||
|
||||
m_Gravity = b2Vec2(0.f, -9.82f);
|
||||
m_PhysicsWorld = new b2World(m_Gravity);
|
||||
|
||||
m_TimeStep = 1.f/60.f;
|
||||
m_VelocityIterations = 6;
|
||||
m_PositionIterations = 2;
|
||||
|
||||
|
||||
m_PhysicsWorld->SetContactListener(m_ContactListener);
|
||||
}
|
||||
|
||||
void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
{
|
||||
m_PhysicsWorld->Step(m_TimeStep, m_VelocityIterations, m_PositionIterations);
|
||||
|
||||
for(auto i : m_Bodies)
|
||||
for(auto i : m_EntitiesToBodies)
|
||||
{
|
||||
EntityID entity = i.first;
|
||||
b2Body* body = i.second;
|
||||
@@ -101,7 +106,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
auto boxComponent = m_World->GetComponent<Components::RectangleShape>(entity);
|
||||
if(boxComponent){
|
||||
|
||||
pShape.SetAsBox(absoluteTransform.Scale.x/2, absoluteTransform.Scale.y/2);
|
||||
pShape.SetAsBox(absoluteTransform.Scale.x/4, absoluteTransform.Scale.y/4); //TODO: THIS SUCKS DUDE
|
||||
}
|
||||
|
||||
if(physicsComponent->Static){
|
||||
@@ -112,14 +117,20 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
b2FixtureDef fixtureDef;
|
||||
fixtureDef.shape = &pShape;
|
||||
fixtureDef.density = 1.f;
|
||||
fixtureDef.restitution = 0.2f;
|
||||
fixtureDef.restitution = 1.0f;
|
||||
fixtureDef.friction = 0.3f;
|
||||
body->CreateFixture(&fixtureDef);
|
||||
|
||||
}
|
||||
|
||||
m_Bodies.insert(std::make_pair(entity, body));
|
||||
m_EntitiesToBodies.insert(std::make_pair(entity, body));
|
||||
m_BodiesToEntities.insert(std::make_pair(body, entity));
|
||||
}
|
||||
|
||||
|
||||
|
||||
dd::Systems::PhysicsSystem::~PhysicsSystem()
|
||||
{
|
||||
if (m_ContactListener != nullptr) {
|
||||
delete m_ContactListener;
|
||||
m_ContactListener = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user