PhysicsCallback.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 "EventStream.h"
  25. #include "PhysicsTypes.h"
  26. #include "PxActor.h"
  27. #include "PxRigidActor.h"
  28. #include "PxController.h"
  29. #include "PxSimulationEventCallback.h"
  30. using physx::PxActor;
  31. using physx::PxConstraintInfo;
  32. using physx::PxContactPair;
  33. using physx::PxContactPairHeader;
  34. using physx::PxContactPairHeader;
  35. using physx::PxContactPairHeaderFlag;
  36. using physx::PxContactPairPoint;
  37. using physx::PxControllerObstacleHit;
  38. using physx::PxControllerShapeHit;
  39. using physx::PxControllersHit;
  40. using physx::PxSimulationEventCallback;
  41. using physx::PxTriggerPair;
  42. using physx::PxTriggerPairFlag;
  43. using physx::PxU32;
  44. using physx::PxUserControllerHitReport;
  45. using physx::PxVec3;
  46. namespace crown
  47. {
  48. class PhysicsWorld;
  49. //-----------------------------------------------------------------------------
  50. class PhysicsSimulationCallback : public PxSimulationEventCallback
  51. {
  52. public:
  53. //-----------------------------------------------------------------------------
  54. PhysicsSimulationCallback(EventStream& stream)
  55. : m_events(stream)
  56. {
  57. }
  58. //-----------------------------------------------------------------------------
  59. void onConstraintBreak(PxConstraintInfo* /*constraints*/, PxU32 /*count*/)
  60. {
  61. // printf("COSTRAINTBREAK\n");
  62. }
  63. //-----------------------------------------------------------------------------
  64. void onContact(const PxContactPairHeader& pair_header, const PxContactPair* pairs, PxU32 num_pairs)
  65. {
  66. // printf("CONTACT\n");
  67. // Do not report contact if either actor0 or actor1 has been deleted
  68. if (pair_header.flags & PxContactPairHeaderFlag::eDELETED_ACTOR_0 || pair_header.flags & PxContactPairHeaderFlag::eDELETED_ACTOR_1) return;
  69. // printf("ACTOR0 = %.4X\n", pair_header.actors[0]->userData);
  70. // printf("ACTOR1 = %.4X\n", pair_header.actors[1]->userData);
  71. PxVec3 where;
  72. // printf("Num pairs = %d\n", num_pairs);
  73. for (PxU32 pp = 0; pp < num_pairs; pp++)
  74. {
  75. PxContactPairPoint points[8];
  76. const PxU32 num_points = pairs[pp].extractContacts(points, 8);
  77. // printf("Num points = %d\n", num_points);
  78. for (PxU32 i = 0; i < num_points; i++)
  79. {
  80. where = points[i].position;
  81. // printf("where = %.2f %.2f %.2f\n", where.x, where.y, where.z);
  82. }
  83. }
  84. physics_world::CollisionEvent ev;
  85. ev.actors[0] = (Actor*) pair_header.actors[0]->userData;
  86. ev.actors[1] = (Actor*) pair_header.actors[1]->userData;
  87. ev.where = Vector3(where.x, where.y, where.z);
  88. event_stream::write(m_events, physics_world::EventType::COLLISION, ev);
  89. }
  90. //-----------------------------------------------------------------------------
  91. void onTrigger(PxTriggerPair* pairs, PxU32 count)
  92. {
  93. // printf("TRIGGER\n");
  94. // printf("Num pairs = %d\n", count);
  95. for (PxU32 pp = 0; pp < count; pp++)
  96. {
  97. const PxTriggerPair& pair = pairs[pp];
  98. // Do not report event if either trigger ot other shape has been deleted
  99. if (pair.flags & PxTriggerPairFlag::eDELETED_SHAPE_TRIGGER || pair.flags & PxTriggerPairFlag::eDELETED_SHAPE_OTHER) continue;
  100. // TODO
  101. physics_world::TriggerEvent ev;
  102. event_stream::write(m_events, physics_world::EventType::TRIGGER, ev);
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. void onWake(PxActor** /*actors*/, PxU32 /*count*/)
  107. {
  108. // printf("WAKE\n");
  109. }
  110. //-----------------------------------------------------------------------------
  111. void onSleep(PxActor** /*actors*/, PxU32 /*count*/)
  112. {
  113. // printf("SLEEP\n");
  114. }
  115. private:
  116. EventStream& m_events;
  117. };
  118. //-----------------------------------------------------------------------------
  119. class PhysicsControllerCallback : public PxUserControllerHitReport
  120. {
  121. //-----------------------------------------------------------------------------
  122. void onShapeHit(const PxControllerShapeHit& hit)
  123. {
  124. // printf("SHAPE HIT\n");
  125. }
  126. //-----------------------------------------------------------------------------
  127. void onControllerHit(const PxControllersHit& hit)
  128. {
  129. // printf("CONTROLLER HIT\n");
  130. }
  131. //-----------------------------------------------------------------------------
  132. void onObstacleHit(const PxControllerObstacleHit& hit)
  133. {
  134. // printf("OBSTACLE HIT\n");
  135. }
  136. };
  137. } // namespace crown