godot_result_callbacks.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*************************************************************************/
  2. /* godot_result_callbacks.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef GODOT_RESULT_CALLBACKS_H
  31. #define GODOT_RESULT_CALLBACKS_H
  32. #include "servers/physics_server.h"
  33. #if defined(__clang__) && (__clang_major__ >= 13)
  34. #pragma clang diagnostic push
  35. #pragma clang diagnostic ignored "-Wdeprecated-copy-with-user-provided-copy"
  36. #endif
  37. #include <BulletCollision/BroadphaseCollision/btBroadphaseProxy.h>
  38. #include <btBulletDynamicsCommon.h>
  39. #if defined(__clang__) && (__clang_major__ >= 13)
  40. #pragma clang diagnostic pop
  41. #endif
  42. /**
  43. @author AndreaCatania
  44. */
  45. class RigidBodyBullet;
  46. /// This callback is injected inside bullet server and allow me to smooth contacts against trimesh
  47. bool godotContactAddedCallback(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  48. /// This class is required to implement custom collision behaviour in the broadphase
  49. struct GodotFilterCallback : public btOverlapFilterCallback {
  50. static bool test_collision_filters(uint32_t body0_collision_layer, uint32_t body0_collision_mask, uint32_t body1_collision_layer, uint32_t body1_collision_mask);
  51. // return true when pairs need collision
  52. virtual bool needBroadphaseCollision(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1) const;
  53. };
  54. /// It performs an additional check allow exclusions.
  55. struct GodotClosestRayResultCallback : public btCollisionWorld::ClosestRayResultCallback {
  56. const Set<RID> *m_exclude;
  57. bool m_pickRay;
  58. int m_shapeId;
  59. bool collide_with_bodies;
  60. bool collide_with_areas;
  61. public:
  62. GodotClosestRayResultCallback(const btVector3 &rayFromWorld, const btVector3 &rayToWorld, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  63. btCollisionWorld::ClosestRayResultCallback(rayFromWorld, rayToWorld),
  64. m_exclude(p_exclude),
  65. m_pickRay(false),
  66. m_shapeId(0),
  67. collide_with_bodies(p_collide_with_bodies),
  68. collide_with_areas(p_collide_with_areas) {}
  69. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  70. virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult &rayResult, bool normalInWorldSpace) {
  71. if (rayResult.m_localShapeInfo) {
  72. m_shapeId = rayResult.m_localShapeInfo->m_triangleIndex; // "m_triangleIndex" Is a odd name but contains the compound shape ID
  73. } else {
  74. m_shapeId = 0;
  75. }
  76. return btCollisionWorld::ClosestRayResultCallback::addSingleResult(rayResult, normalInWorldSpace);
  77. }
  78. };
  79. // store all colliding object
  80. struct GodotAllConvexResultCallback : public btCollisionWorld::ConvexResultCallback {
  81. public:
  82. PhysicsDirectSpaceState::ShapeResult *m_results;
  83. int m_resultMax;
  84. const Set<RID> *m_exclude;
  85. int count;
  86. GodotAllConvexResultCallback(PhysicsDirectSpaceState::ShapeResult *p_results, int p_resultMax, const Set<RID> *p_exclude) :
  87. m_results(p_results),
  88. m_resultMax(p_resultMax),
  89. m_exclude(p_exclude),
  90. count(0) {}
  91. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  92. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace);
  93. };
  94. struct GodotKinClosestConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback {
  95. public:
  96. const RigidBodyBullet *m_self_object;
  97. const Set<RID> *m_exclude;
  98. const bool m_infinite_inertia;
  99. GodotKinClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, const RigidBodyBullet *p_self_object, bool p_infinite_inertia, const Set<RID> *p_exclude) :
  100. btCollisionWorld::ClosestConvexResultCallback(convexFromWorld, convexToWorld),
  101. m_self_object(p_self_object),
  102. m_exclude(p_exclude),
  103. m_infinite_inertia(p_infinite_inertia) {}
  104. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  105. };
  106. struct GodotClosestConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback {
  107. public:
  108. const Set<RID> *m_exclude;
  109. int m_shapeId;
  110. bool collide_with_bodies;
  111. bool collide_with_areas;
  112. GodotClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  113. btCollisionWorld::ClosestConvexResultCallback(convexFromWorld, convexToWorld),
  114. m_exclude(p_exclude),
  115. m_shapeId(0),
  116. collide_with_bodies(p_collide_with_bodies),
  117. collide_with_areas(p_collide_with_areas) {}
  118. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  119. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace);
  120. };
  121. struct GodotAllContactResultCallback : public btCollisionWorld::ContactResultCallback {
  122. public:
  123. const btCollisionObject *m_self_object;
  124. PhysicsDirectSpaceState::ShapeResult *m_results;
  125. int m_resultMax;
  126. const Set<RID> *m_exclude;
  127. int m_count;
  128. bool collide_with_bodies;
  129. bool collide_with_areas;
  130. GodotAllContactResultCallback(btCollisionObject *p_self_object, PhysicsDirectSpaceState::ShapeResult *p_results, int p_resultMax, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  131. m_self_object(p_self_object),
  132. m_results(p_results),
  133. m_resultMax(p_resultMax),
  134. m_exclude(p_exclude),
  135. m_count(0),
  136. collide_with_bodies(p_collide_with_bodies),
  137. collide_with_areas(p_collide_with_areas) {}
  138. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  139. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  140. };
  141. /// Returns the list of contacts pairs in this order: Local contact, other body contact
  142. struct GodotContactPairContactResultCallback : public btCollisionWorld::ContactResultCallback {
  143. public:
  144. const btCollisionObject *m_self_object;
  145. Vector3 *m_results;
  146. int m_resultMax;
  147. const Set<RID> *m_exclude;
  148. int m_count;
  149. bool collide_with_bodies;
  150. bool collide_with_areas;
  151. GodotContactPairContactResultCallback(btCollisionObject *p_self_object, Vector3 *p_results, int p_resultMax, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  152. m_self_object(p_self_object),
  153. m_results(p_results),
  154. m_resultMax(p_resultMax),
  155. m_exclude(p_exclude),
  156. m_count(0),
  157. collide_with_bodies(p_collide_with_bodies),
  158. collide_with_areas(p_collide_with_areas) {}
  159. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  160. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  161. };
  162. struct GodotRestInfoContactResultCallback : public btCollisionWorld::ContactResultCallback {
  163. public:
  164. const btCollisionObject *m_self_object;
  165. PhysicsDirectSpaceState::ShapeRestInfo *m_result;
  166. const Set<RID> *m_exclude;
  167. bool m_collided;
  168. real_t m_min_distance;
  169. const btCollisionObject *m_rest_info_collision_object;
  170. btVector3 m_rest_info_bt_point;
  171. bool collide_with_bodies;
  172. bool collide_with_areas;
  173. GodotRestInfoContactResultCallback(btCollisionObject *p_self_object, PhysicsDirectSpaceState::ShapeRestInfo *p_result, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  174. m_self_object(p_self_object),
  175. m_result(p_result),
  176. m_exclude(p_exclude),
  177. m_collided(false),
  178. m_min_distance(0),
  179. collide_with_bodies(p_collide_with_bodies),
  180. collide_with_areas(p_collide_with_areas) {}
  181. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  182. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  183. };
  184. struct GodotDeepPenetrationContactResultCallback : public btManifoldResult {
  185. btVector3 m_pointNormalWorld;
  186. btVector3 m_pointWorld;
  187. btScalar m_penetration_distance;
  188. int m_other_compound_shape_index;
  189. GodotDeepPenetrationContactResultCallback(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap) :
  190. btManifoldResult(body0Wrap, body1Wrap),
  191. m_penetration_distance(0),
  192. m_other_compound_shape_index(0) {}
  193. void reset() {
  194. m_penetration_distance = 0;
  195. }
  196. bool hasHit() {
  197. return m_penetration_distance < 0;
  198. }
  199. virtual void addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorldOnB, btScalar depth);
  200. };
  201. #endif // GODOT_RESULT_CALLBACKS_H