This commit is contained in:
ViktorLjung
2014-05-20 03:41:10 +02:00
parent fe400e9af8
commit 7e5cf8efaa
13 changed files with 139 additions and 40 deletions
+19 -5
View File
@@ -78,7 +78,7 @@ void Systems::PhysicsSystem::Initialize()
worldInfo.m_broadPhaseBorderBehaviour = hkpWorldCinfo::BROADPHASE_BORDER_DO_NOTHING;
// You must specify the size of the broad phase - objects should not be simulated outside this region
worldInfo.setBroadPhaseWorldSize(1000.0f);
worldInfo.setBroadPhaseWorldSize(1500.0f);
m_PhysicsWorld = new hkpWorld(worldInfo);
// When the simulation type is SIMULATION_TYPE_MULTITHREADED, in the debug build, the sdk performs checks
@@ -107,7 +107,7 @@ void Systems::PhysicsSystem::Initialize()
m_PhysicsWorld->unmarkForWrite();
m_collisionResolution = new MyCollisionResolution;
m_collisionResolution = new MyCollisionResolution(this);
}
}
@@ -326,7 +326,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
rigidBody->addContactListener( m_collisionResolution );
m_Vehicles[entity]->addToWorld(m_PhysicsWorld);
m_RigidBodies[entity] = rigidBody;
m_collisionResolution->m_RigidBodies[rigidBody] = entity;
m_RigidBodyEntities[rigidBody] = entity;
// The vehicle is an action
@@ -344,7 +344,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
rigidBody->addContactListener( m_collisionResolution );
m_PhysicsWorld->addEntity(rigidBody);
m_RigidBodies[entity] = rigidBody;
m_collisionResolution->m_RigidBodies[rigidBody] = entity;
m_RigidBodyEntities[rigidBody] = entity;
m_PhysicsWorld->unmarkForWrite();
shape->removeReference();
@@ -397,7 +397,7 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity )
m_PhysicsWorld->markForWrite();
m_PhysicsWorld->addEntity(rigidBody);
m_RigidBodies[entity] = rigidBody;
m_collisionResolution->m_RigidBodies[rigidBody] = entity;
m_RigidBodyEntities[rigidBody] = entity;
m_PhysicsWorld->unmarkForWrite();
shape->removeReference();
@@ -577,3 +577,17 @@ bool Systems::PhysicsSystem::OnApplyPointImpulse( const Events::ApplyPointImpuls
m_PhysicsWorld->unmarkForWrite();
return true;
}
void Systems::PhysicsSystem::OnEntityRemoved( EntityID entity )
{
// TODO:
/*auto rigidBodyIt = m_RigidBodies.find(entity);
if (rigidBodyIt == m_RigidBodies.end())
return;
auto rigidBody = rigidBodyIt->second;
m_RigidBodies.erase(entity);
m_RigidBodyEntities.erase(rigidBody);
rigidBody->removeReference();*/
}
+38 -17
View File
@@ -27,6 +27,7 @@
#include "Events/SetVelocity.h"
#include "Events/ApplyForce.h"
#include "Events/ApplyPointImpulse.h"
#include "Events/Collision.h"
#include "OBJ.h"
// Math and base include
@@ -75,27 +76,45 @@
#include <Physics2012/Dynamics/Collide/ContactListener/hkpContactListener.h>
class MyCollisionResolution: public hkReferencedObject, public hkpContactListener
{
public:
std::unordered_map<hkpRigidBody*, EntityID> m_RigidBodies;
virtual void contactPointCallback( const hkpContactPointEvent& event )
{
EntityID entity1 = m_RigidBodies[event.getBody(0)];
EntityID entity2 = m_RigidBodies[event.getBody(1)];
//LOG_INFO("Entities colliding: %i, %i ", entity1, entity2);
}
};
#include "Components/Model.h"
namespace Systems
{
class PhysicsSystem : public System
{
public:
class MyCollisionResolution: public hkReferencedObject, public hkpContactListener
{
public:
MyCollisionResolution(Systems::PhysicsSystem* physicsSystem)
: m_PhysicsSystem(physicsSystem) { }
virtual void contactPointCallback( const hkpContactPointEvent& event )
{
EntityID entity1 = m_PhysicsSystem->m_RigidBodyEntities[event.getBody(0)];
EntityID entity2 = m_PhysicsSystem->m_RigidBodyEntities[event.getBody(1)];
Events::Collision e;
e.Entity1 = entity1;
e.Entity2 = entity2;
m_PhysicsSystem->EventBroker->Publish(e);
auto modelComponent = m_PhysicsSystem->m_World->GetComponent<Components::Model>(entity1);
if (modelComponent && modelComponent->ModelFile == "Models/Placeholders/rocket/Rocket.obj" && entity2 == 6)
{
m_PhysicsSystem->m_PhysicsWorld->markForWrite();
m_PhysicsSystem->m_RigidBodies[entity1]->setPosition(hkVector4(2000, -2000, 2000));
m_PhysicsSystem->m_PhysicsWorld->unmarkForWrite();
}
}
private:
Systems::PhysicsSystem* m_PhysicsSystem;
};
friend class MyCollisionResolution;
PhysicsSystem(World* world, std::shared_ptr<::EventBroker> eventBroker)
: System(world, eventBroker) { }
@@ -107,7 +126,8 @@ public:
void OnComponentCreated(std::string type, std::shared_ptr<Component> component) override;
void OnComponentRemoved(std::string type, Component* component) override;
void OnEntityCommit(EntityID entity) override;
void OnEntityRemoved(EntityID entity) override;
private:
double m_Accumulator;
hkpWorld* m_PhysicsWorld;
@@ -132,6 +152,7 @@ private:
void SetupPhysics(hkpWorld* physicsWorld);
std::unordered_map<EntityID, hkpRigidBody*> m_RigidBodies;
std::unordered_map<hkpRigidBody*, EntityID> m_RigidBodyEntities;
hkJobThreadPool* m_ThreadPool;
hkJobQueue* m_JobQueue;
+28
View File
@@ -11,6 +11,8 @@ void Systems::TankSteeringSystem::RegisterComponents( ComponentFactory* cf )
void Systems::TankSteeringSystem::Initialize()
{
EVENT_SUBSCRIBE_MEMBER(m_ECollision, &Systems::TankSteeringSystem::OnCollision);
for (int i = 0; i < 4; i++)
{
m_TankInputControllers[i] = std::shared_ptr<TankSteeringInputController>(new TankSteeringInputController(EventBroker, i + 1));
@@ -93,6 +95,32 @@ void Systems::TankSteeringSystem::UpdateEntity(double dt, EntityID entity, Entit
}
}
bool Systems::TankSteeringSystem::OnCollision( const Events::Collision &e )
{
// TODO:
auto modelComponent = m_World->GetComponent<Components::Model>(e.Entity1);
if (modelComponent && modelComponent->ModelFile == "Models/Placeholders/rocket/Rocket.obj")
{
auto transform = m_World->GetComponent<Components::Transform>(e.Entity1);
m_World->GetSystem<Systems::ParticleSystem>()->CreateExplosion(
transform->Position,
1,
60,
"Textures/Sprites/NewtonTreeDeleteASAPPlease.png",
glm::angleAxis(glm::pi<float>()/2, glm::vec3(1,0,0)),
40,
glm::pi<float>(),
0.5f
);
//m_World->RemoveEntity(e.Entity1);
}
//LOG_INFO("Ent1 %i Ent2 %i", e.Entity1, e.Entity2);
return true;
}
void Systems::TankSteeringSystem::TankSteeringInputController::Update( double dt )
{
PositionX = m_Horizontal;
+6
View File
@@ -5,6 +5,7 @@
#include "Events/SetVelocity.h"
#include "Events/ApplyForce.h"
#include "Events/ApplyPointImpulse.h"
#include "Events/Collision.h"
#include "Components/Transform.h"
#include "Components/TankSteering.h"
#include "Components/TowerSteering.h"
@@ -12,7 +13,9 @@
#include "Components/Physics.h"
#include "Components/Vehicle.h"
#include "Components/Player.h"
#include "Components/Model.h"
#include "Systems/TransformSystem.h"
#include "Systems/ParticleSystem.h"
#include "InputController.h"
namespace Systems
@@ -31,6 +34,9 @@ namespace Systems
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
private:
EventRelay<Events::Collision> m_ECollision;
bool OnCollision(const Events::Collision &e);
class TankSteeringInputController;
std::array<std::shared_ptr<TankSteeringInputController>, 4> m_TankInputControllers;