Преглед изворни кода

Add very basic event management to PhysicsWorld

Daniele Bartolini пре 12 година
родитељ
комит
8e76f71d98

+ 55 - 11
engine/physics/PhysicsCallback.h

@@ -26,8 +26,11 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 
 #pragma once
 #pragma once
 
 
-#include "PxSimulationEventCallback.h"
+#include "EventStream.h"
+#include "PhysicsTypes.h"
+#include "PxActor.h"
 #include "PxController.h"
 #include "PxController.h"
+#include "PxSimulationEventCallback.h"
 
 
 using physx::PxSimulationEventCallback;
 using physx::PxSimulationEventCallback;
 using physx::PxContactPairHeader;
 using physx::PxContactPairHeader;
@@ -41,47 +44,88 @@ using physx::PxUserControllerHitReport;
 using physx::PxControllerShapeHit;
 using physx::PxControllerShapeHit;
 using physx::PxControllersHit;
 using physx::PxControllersHit;
 using physx::PxControllerObstacleHit;
 using physx::PxControllerObstacleHit;
+using physx::PxContactPairHeader;
+using physx::PxContactPairHeaderFlag;
+using physx::PxContactPairPoint;
+using physx::PxVec3;
 
 
 namespace crown
 namespace crown
 {
 {
 
 
+class PhysicsWorld;
+
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 class PhysicsSimulationCallback : public PxSimulationEventCallback
 class PhysicsSimulationCallback : public PxSimulationEventCallback
 {
 {
 public:
 public:
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
-	PhysicsSimulationCallback() {}
+	PhysicsSimulationCallback(EventStream& stream)
+		: m_events(stream)
+	{
+	}
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	void onConstraintBreak(PxConstraintInfo* /*constraints*/, PxU32 /*count*/)
 	void onConstraintBreak(PxConstraintInfo* /*constraints*/, PxU32 /*count*/)
 	{
 	{
-		// Log::i("COSTRAINTBREAK");
+		// printf("COSTRAINTBREAK\n");
 	}
 	}
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
-	void onContact(const PxContactPairHeader& /*pairHeader*/, const PxContactPair* /*pairs*/, PxU32 /*nbPairs*/)
+	void onContact(const PxContactPairHeader& pair_header, const PxContactPair* pairs, PxU32 num_pairs)
 	{
 	{
-		// Log::i("CONTACT");
+		// printf("CONTACT\n");
+
+		// Do not report contact if either actor0 or actor1 has been deleted
+		if (pair_header.flags & PxContactPairHeaderFlag::eDELETED_ACTOR_0 || pair_header.flags & PxContactPairHeaderFlag::eDELETED_ACTOR_1) return;
+
+		// printf("ACTOR0 = %.4X\n", pair_header.actors[0]->userData);
+		// printf("ACTOR1 = %.4X\n", pair_header.actors[1]->userData);
+
+		PxVec3 where;
+
+		// printf("Num pairs = %d\n", num_pairs);
+		for (PxU32 pp = 0; pp < num_pairs; pp++)
+		{
+			PxContactPairPoint points[8];
+			const PxU32 num_points = pairs[pp].extractContacts(points, 8);
+			// printf("Num points = %d\n", num_points);
+
+			for (PxU32 i = 0; i < num_points; i++)
+			{
+				where = points[i].position;
+				// printf("where = %.2f %.2f %.2f\n", where.x, where.y, where.z);
+			}
+		}
+
+		physics_world::CollisionEvent ev;
+		ev.actors[0] = (Actor*) pair_header.actors[0]->userData;
+		ev.actors[1] = (Actor*) pair_header.actors[1]->userData;
+		ev.where = Vector3(where.x, where.y, where.z);
+		event_stream::write(m_events, physics_world::EventType::COLLISION, ev);
 	}
 	}
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	void onTrigger(PxTriggerPair* /*pairs*/, PxU32 /*count*/)
 	void onTrigger(PxTriggerPair* /*pairs*/, PxU32 /*count*/)
 	{
 	{
-		// Log::i("TRIGGER");
+		// printf("TRIGGER\n");
 	}
 	}
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	void onWake(PxActor** /*actors*/, PxU32 /*count*/)
 	void onWake(PxActor** /*actors*/, PxU32 /*count*/)
 	{
 	{
-		// Log::i("WAKE");
+		// printf("WAKE\n");
 	}
 	}
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	void onSleep(PxActor** /*actors*/, PxU32 /*count*/)
 	void onSleep(PxActor** /*actors*/, PxU32 /*count*/)
 	{
 	{
-		// Log::i("SLEEP");
+		// printf("SLEEP\n");
 	}
 	}
+
+private:
+
+	EventStream& m_events;
 };
 };
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -90,19 +134,19 @@ class PhysicsControllerCallback : public PxUserControllerHitReport
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	void onShapeHit(const PxControllerShapeHit& hit)
 	void onShapeHit(const PxControllerShapeHit& hit)
 	{
 	{
-		// Log::i("SHAPE HIT");
+		// printf("SHAPE HIT\n");
 	}
 	}
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	void onControllerHit(const PxControllersHit& hit)
 	void onControllerHit(const PxControllersHit& hit)
 	{
 	{
-		// Log::i("CONTROLLER HIT");
+		// printf("CONTROLLER HIT\n");
 	}
 	}
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	void onObstacleHit(const PxControllerObstacleHit& hit)
 	void onObstacleHit(const PxControllerObstacleHit& hit)
 	{
 	{
-		// Log::i("OBSTACLE HIT");
+		// printf("OBSTACLE HIT\n");
 	}
 	}
 };
 };
 
 

+ 21 - 0
engine/physics/PhysicsTypes.h

@@ -26,6 +26,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 
 #pragma once
 #pragma once
 
 
+#include "Vector3.h"
+
 namespace crown
 namespace crown
 {
 {
 
 
@@ -34,6 +36,8 @@ typedef Id ControllerId;
 typedef Id TriggerId;
 typedef Id TriggerId;
 typedef Id JointId;
 typedef Id JointId;
 
 
+struct Actor;
+
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 struct ActorType
 struct ActorType
 {
 {
@@ -110,4 +114,21 @@ struct CollisionGroup
 	};
 	};
 };
 };
 
 
+namespace physics_world
+{
+	struct EventType
+	{
+		enum Enum
+		{
+			COLLISION
+		};
+	};
+
+	struct CollisionEvent
+	{
+		Actor* actors[2];
+		Vector3 where;
+	};
+}
+
 } // namespace crown
 } // namespace crown

+ 3 - 1
engine/physics/PhysicsWorld.cpp

@@ -156,7 +156,7 @@ namespace physics_system
 		// the filtermask of A contains the ID of B and vice versa.
 		// the filtermask of A contains the ID of B and vice versa.
 		if((filterData0.word0 & filterData1.word1) && (filterData1.word0 & filterData0.word1))
 		if((filterData0.word0 & filterData1.word1) && (filterData1.word0 & filterData0.word1))
 		{
 		{
-			pairFlags |= PxPairFlag::eNOTIFY_TOUCH_FOUND;
+			pairFlags |= PxPairFlag::eNOTIFY_TOUCH_FOUND | PxPairFlag::eNOTIFY_CONTACT_POINTS;
 			return PxFilterFlag::eDEFAULT;
 			return PxFilterFlag::eDEFAULT;
 		}
 		}
 
 
@@ -202,6 +202,8 @@ PhysicsWorld::PhysicsWorld()
 	, m_controllers_pool(default_allocator(), MAX_CONTROLLERS, sizeof(Controller), CE_ALIGNOF(Controller))
 	, m_controllers_pool(default_allocator(), MAX_CONTROLLERS, sizeof(Controller), CE_ALIGNOF(Controller))
 	, m_triggers_pool(default_allocator(), MAX_TRIGGERS, sizeof(Trigger), CE_ALIGNOF(Trigger))
 	, m_triggers_pool(default_allocator(), MAX_TRIGGERS, sizeof(Trigger), CE_ALIGNOF(Trigger))
 	, m_joints_pool(default_allocator(), MAX_JOINTS, sizeof(Joint), CE_ALIGNOF(Joint))
 	, m_joints_pool(default_allocator(), MAX_JOINTS, sizeof(Joint), CE_ALIGNOF(Joint))
+	, m_events(default_allocator())
+	, m_callback(m_events)
 {
 {
 	// Create the scene
 	// Create the scene
 	PxSceneLimits scene_limits;
 	PxSceneLimits scene_limits;

+ 13 - 8
engine/physics/PhysicsWorld.h

@@ -30,6 +30,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "PoolAllocator.h"
 #include "PoolAllocator.h"
 #include "PhysicsTypes.h"
 #include "PhysicsTypes.h"
 #include "PhysicsCallback.h"
 #include "PhysicsCallback.h"
+#include "EventStream.h"
 
 
 #include "PxScene.h"
 #include "PxScene.h"
 #include "PxDefaultCpuDispatcher.h"
 #include "PxDefaultCpuDispatcher.h"
@@ -111,19 +112,23 @@ public:
 	PxScene*					m_scene;
 	PxScene*					m_scene;
 	PxDefaultCpuDispatcher*		m_cpu_dispatcher;
 	PxDefaultCpuDispatcher*		m_cpu_dispatcher;
 
 
-	PhysicsSimulationCallback	m_callback;
-
 	PoolAllocator				m_actors_pool;
 	PoolAllocator				m_actors_pool;
-	IdArray<MAX_ACTORS, Actor*>	m_actors;
-
 	PoolAllocator				m_controllers_pool;
 	PoolAllocator				m_controllers_pool;
-	IdArray<MAX_CONTROLLERS, Controller*> m_controllers;
-
 	PoolAllocator				m_triggers_pool;
 	PoolAllocator				m_triggers_pool;
-	IdArray<MAX_TRIGGERS, Trigger*> m_triggers;
-
 	PoolAllocator				m_joints_pool;
 	PoolAllocator				m_joints_pool;
+
+	IdArray<MAX_ACTORS, Actor*>	m_actors;
+	IdArray<MAX_CONTROLLERS, Controller*> m_controllers;
+	IdArray<MAX_TRIGGERS, Trigger*> m_triggers;
 	IdArray<MAX_JOINTS, Joint*> m_joints;
 	IdArray<MAX_JOINTS, Joint*> m_joints;
+
+	// Events management
+	EventStream m_events;
+	PhysicsSimulationCallback m_callback;
+
+public:
+
+	friend class PhysicsSimulationCallback;
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 21 - 0
engine/world/WorldTypes.h

@@ -35,4 +35,25 @@ typedef Id UnitId;
 typedef Id WorldId;
 typedef Id WorldId;
 typedef Id CameraId;
 typedef Id CameraId;
 
 
+struct EventType
+{
+	enum Enum
+	{
+		SPAWN,
+		DESTROY
+	};
+};
+
+struct SpawnUnitEvent
+{
+	/// The unit being spawned
+	UnitId unit;
+};
+
+struct DestroyUnitEvent
+{
+	/// The unit being unspawned
+	UnitId unit;
+};
+
 } // namespace crown
 } // namespace crown