From 4d15d3e02ce1eb40de13c40c234a77b6ba81c09b Mon Sep 17 00:00:00 2001 From: ViktorLjung Date: Sat, 17 May 2014 18:06:56 +0200 Subject: [PATCH] Collision callbacks --- src/Components/Health.h | 21 ++++++++++++++++ src/GameWorld.cpp | 2 +- src/Systems/PhysicsSystem.cpp | 11 +++++++- src/Systems/PhysicsSystem.h | 25 ++++++++++++++++--- vs11/Returngeance/Returngeance.vcxproj | 1 + .../Returngeance/Returngeance.vcxproj.filters | 3 +++ 6 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 src/Components/Health.h diff --git a/src/Components/Health.h b/src/Components/Health.h new file mode 100644 index 0000000..5100aaf --- /dev/null +++ b/src/Components/Health.h @@ -0,0 +1,21 @@ +#ifndef Components_Health_h__ +#define Components_Health_h__ + +#include "Component.h" + +namespace Components +{ + + struct Health : Component + { + Health() + : health(1.0f){ } + + float health; + + virtual Health* Clone() const override { return new Health(*this); } + }; + +} + +#endif // Components_Health_h__ diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 9e48cf7..e9f5327 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -340,7 +340,7 @@ void GameWorld::Initialize() //Create wheels float wheelOffset = 0.4f; float springLength = 0.3f; - float suspensionStrength = 25.f; + float suspensionStrength = 15.f; { auto wheel = CreateEntity(tank); diff --git a/src/Systems/PhysicsSystem.cpp b/src/Systems/PhysicsSystem.cpp index 8efa4bb..51420d3 100644 --- a/src/Systems/PhysicsSystem.cpp +++ b/src/Systems/PhysicsSystem.cpp @@ -27,6 +27,8 @@ void Systems::PhysicsSystem::Initialize() { + + m_Accumulator = 0; // Events @@ -102,6 +104,8 @@ void Systems::PhysicsSystem::Initialize() SetupVisualDebugger(m_Context); m_PhysicsWorld->unmarkForWrite(); + + m_collisionResolution = new MyCollisionResolution; } } @@ -324,9 +328,11 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) m_PhysicsWorld->markForWrite(); vehicleSetup.buildVehicle(m_World, m_PhysicsWorld, *m_Vehicles[entity], entity, m_Wheels); // Add the vehicle's entities and phantoms to the world + rigidBody->addContactListener( m_collisionResolution ); m_Vehicles[entity]->addToWorld(m_PhysicsWorld); - m_RigidBodies[entity] = rigidBody; + m_collisionResolution->m_RigidBodies[rigidBody] = entity; + // The vehicle is an action m_PhysicsWorld->addAction(m_Vehicles[entity]); @@ -340,8 +346,10 @@ void Systems::PhysicsSystem::OnEntityCommit( EntityID entity ) else { m_PhysicsWorld->markForWrite(); + rigidBody->addContactListener( m_collisionResolution ); m_PhysicsWorld->addEntity(rigidBody); m_RigidBodies[entity] = rigidBody; + m_collisionResolution->m_RigidBodies[rigidBody] = entity; m_PhysicsWorld->unmarkForWrite(); shape->removeReference(); @@ -394,6 +402,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_PhysicsWorld->unmarkForWrite(); shape->removeReference(); diff --git a/src/Systems/PhysicsSystem.h b/src/Systems/PhysicsSystem.h index 7b2d259..ef7cb70 100644 --- a/src/Systems/PhysicsSystem.h +++ b/src/Systems/PhysicsSystem.h @@ -60,9 +60,27 @@ #include "Physics/VehicleSetup.h" #include + +#include + +class MyCollisionResolution: public hkReferencedObject, public hkpContactListener +{ +public: + std::unordered_map 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); + + } +}; + + namespace Systems { - class PhysicsSystem : public System { public: @@ -77,7 +95,7 @@ public: void OnComponentCreated(std::string type, std::shared_ptr component) override; void OnComponentRemoved(std::string type, Component* component) override; void OnEntityCommit(EntityID entity) override; - + private: double m_Accumulator; hkpWorld* m_PhysicsWorld; @@ -145,8 +163,9 @@ private: hkpMoppBvTreeShape* MoppShape; }; std::unordered_map m_ExtendedMeshShapes; + + MyCollisionResolution* m_collisionResolution; }; } - #endif // PhysicsSystem_h__ diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 6ccecc9..96c8e85 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -131,6 +131,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 641fa8f..70213c2 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -352,6 +352,9 @@ Input\Events + + Physics\Components +