physics_callback.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "event_stream.h"
  25. #include "physics_types.h"
  26. #include "PxActor.h"
  27. #include "PxRigidActor.h"
  28. #include "PxController.h"
  29. #include "PxSimulationEventCallback.h"
  30. #include "PxQueryReport.h"
  31. #include "PxQueryFiltering.h"
  32. using physx::PxActor;
  33. using physx::PxConstraintInfo;
  34. using physx::PxContactPair;
  35. using physx::PxContactPairHeader;
  36. using physx::PxContactPairHeader;
  37. using physx::PxContactPairHeaderFlag;
  38. using physx::PxContactPairPoint;
  39. using physx::PxControllerObstacleHit;
  40. using physx::PxControllerShapeHit;
  41. using physx::PxControllersHit;
  42. using physx::PxSimulationEventCallback;
  43. using physx::PxTriggerPair;
  44. using physx::PxTriggerPairFlag;
  45. using physx::PxU32;
  46. using physx::PxUserControllerHitReport;
  47. using physx::PxVec3;
  48. using physx::PxRaycastCallback;
  49. using physx::PxAgain;
  50. using physx::PxRaycastHit;
  51. using physx::PxPairFlag;
  52. using physx::PxContactPairFlag;
  53. namespace crown
  54. {
  55. class PhysicsWorld;
  56. //-----------------------------------------------------------------------------
  57. class PhysicsSimulationCallback : public PxSimulationEventCallback
  58. {
  59. public:
  60. //-----------------------------------------------------------------------------
  61. PhysicsSimulationCallback(EventStream& stream)
  62. : m_events(stream)
  63. {
  64. }
  65. //-----------------------------------------------------------------------------
  66. void onConstraintBreak(PxConstraintInfo* /*constraints*/, PxU32 /*count*/)
  67. {
  68. // printf("COSTRAINTBREAK\n");
  69. }
  70. //-----------------------------------------------------------------------------
  71. void onContact(const PxContactPairHeader& pair_header, const PxContactPair* pairs, PxU32 num_pairs)
  72. {
  73. // Do not report contact if either actor0 or actor1 or both have been deleted
  74. if (pair_header.flags & PxContactPairHeaderFlag::eDELETED_ACTOR_0 ||
  75. pair_header.flags & PxContactPairHeaderFlag::eDELETED_ACTOR_1) return;
  76. for (PxU32 pp = 0; pp < num_pairs; pp++)
  77. {
  78. const PxContactPair& cp = pairs[pp];
  79. // We are only interested in touch found or lost
  80. if (!cp.events & PxPairFlag::eNOTIFY_TOUCH_FOUND ||
  81. !cp.events & PxPairFlag::eNOTIFY_TOUCH_LOST) continue;
  82. // Skip if either shape0 or shape1 or both have been deleted
  83. if (cp.flags & PxContactPairFlag::eDELETED_SHAPE_0 ||
  84. cp.flags & PxContactPairFlag::eDELETED_SHAPE_1) continue;
  85. PxContactPairPoint points[8];
  86. const PxU32 num_points = cp.extractContacts(points, 8);
  87. PxVec3 where(0, 0, 0);
  88. PxVec3 normal(0, 0, 0);
  89. for (PxU32 i = 0; i < num_points; i++)
  90. {
  91. where = points[i].position;
  92. normal = points[i].normal;
  93. }
  94. physics_world::CollisionEvent ev;
  95. ev.type = (cp.events & PxPairFlag::eNOTIFY_TOUCH_FOUND) ?
  96. physics_world::CollisionEvent::BEGIN_TOUCH :
  97. physics_world::CollisionEvent::END_TOUCH;
  98. ev.actors[0] = (Actor*) pair_header.actors[0]->userData;
  99. ev.actors[1] = (Actor*) pair_header.actors[1]->userData;
  100. ev.where = Vector3(where.x, where.y, where.z);
  101. ev.normal = Vector3(normal.x, normal.y, normal.z);
  102. event_stream::write(m_events, physics_world::EventType::COLLISION, ev);
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. void onTrigger(PxTriggerPair* pairs, PxU32 count)
  107. {
  108. for (PxU32 pp = 0; pp < count; pp++)
  109. {
  110. const PxTriggerPair& tp = pairs[pp];
  111. // Do not report event if either trigger ot other shape or both have been deleted
  112. if (tp.flags & PxTriggerPairFlag::eDELETED_SHAPE_TRIGGER ||
  113. tp.flags & PxTriggerPairFlag::eDELETED_SHAPE_OTHER) continue;
  114. physics_world::TriggerEvent ev;
  115. ev.type = (tp.status & PxPairFlag::eNOTIFY_TOUCH_FOUND ?
  116. physics_world::TriggerEvent::BEGIN_TOUCH : physics_world::TriggerEvent::END_TOUCH);
  117. ev.trigger = (Actor*) tp.triggerActor->userData;
  118. ev.other = (Actor*) tp.otherActor->userData;
  119. event_stream::write(m_events, physics_world::EventType::TRIGGER, ev);
  120. }
  121. }
  122. //-----------------------------------------------------------------------------
  123. void onWake(PxActor** /*actors*/, PxU32 /*count*/)
  124. {
  125. }
  126. //-----------------------------------------------------------------------------
  127. void onSleep(PxActor** /*actors*/, PxU32 /*count*/)
  128. {
  129. }
  130. private:
  131. EventStream& m_events;
  132. };
  133. //-----------------------------------------------------------------------------
  134. class PhysicsControllerCallback : public PxUserControllerHitReport
  135. {
  136. //-----------------------------------------------------------------------------
  137. void onShapeHit(const PxControllerShapeHit& /*hit*/)
  138. {
  139. }
  140. //-----------------------------------------------------------------------------
  141. void onControllerHit(const PxControllersHit& /*hit*/)
  142. {
  143. }
  144. //-----------------------------------------------------------------------------
  145. void onObstacleHit(const PxControllerObstacleHit& /*hit*/)
  146. {
  147. }
  148. };
  149. } // namespace crown