godot_result_callbacks.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /**************************************************************************/
  2. /* godot_result_callbacks.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. // Triangle index is an odd name but contains the compound shape ID.
  72. // A shape part of -1 indicates the index is a shape index and not a triangle index.
  73. if (rayResult.m_localShapeInfo && rayResult.m_localShapeInfo->m_shapePart == -1) {
  74. m_shapeId = rayResult.m_localShapeInfo->m_triangleIndex;
  75. } else {
  76. m_shapeId = 0;
  77. }
  78. return btCollisionWorld::ClosestRayResultCallback::addSingleResult(rayResult, normalInWorldSpace);
  79. }
  80. };
  81. // store all colliding object
  82. struct GodotAllConvexResultCallback : public btCollisionWorld::ConvexResultCallback {
  83. public:
  84. PhysicsDirectSpaceState::ShapeResult *m_results;
  85. int m_resultMax;
  86. const Set<RID> *m_exclude;
  87. int count;
  88. GodotAllConvexResultCallback(PhysicsDirectSpaceState::ShapeResult *p_results, int p_resultMax, const Set<RID> *p_exclude) :
  89. m_results(p_results),
  90. m_resultMax(p_resultMax),
  91. m_exclude(p_exclude),
  92. count(0) {}
  93. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  94. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace);
  95. };
  96. struct GodotKinClosestConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback {
  97. public:
  98. const RigidBodyBullet *m_self_object;
  99. const Set<RID> *m_exclude;
  100. const bool m_infinite_inertia;
  101. GodotKinClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, const RigidBodyBullet *p_self_object, bool p_infinite_inertia, const Set<RID> *p_exclude) :
  102. btCollisionWorld::ClosestConvexResultCallback(convexFromWorld, convexToWorld),
  103. m_self_object(p_self_object),
  104. m_exclude(p_exclude),
  105. m_infinite_inertia(p_infinite_inertia) {}
  106. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  107. };
  108. struct GodotClosestConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback {
  109. public:
  110. const Set<RID> *m_exclude;
  111. int m_shapeId;
  112. bool collide_with_bodies;
  113. bool collide_with_areas;
  114. GodotClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  115. btCollisionWorld::ClosestConvexResultCallback(convexFromWorld, convexToWorld),
  116. m_exclude(p_exclude),
  117. m_shapeId(0),
  118. collide_with_bodies(p_collide_with_bodies),
  119. collide_with_areas(p_collide_with_areas) {}
  120. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  121. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace);
  122. };
  123. struct GodotAllContactResultCallback : public btCollisionWorld::ContactResultCallback {
  124. public:
  125. const btCollisionObject *m_self_object;
  126. PhysicsDirectSpaceState::ShapeResult *m_results;
  127. int m_resultMax;
  128. const Set<RID> *m_exclude;
  129. int m_count;
  130. bool collide_with_bodies;
  131. bool collide_with_areas;
  132. 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) :
  133. m_self_object(p_self_object),
  134. m_results(p_results),
  135. m_resultMax(p_resultMax),
  136. m_exclude(p_exclude),
  137. m_count(0),
  138. collide_with_bodies(p_collide_with_bodies),
  139. collide_with_areas(p_collide_with_areas) {}
  140. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  141. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  142. };
  143. /// Returns the list of contacts pairs in this order: Local contact, other body contact
  144. struct GodotContactPairContactResultCallback : public btCollisionWorld::ContactResultCallback {
  145. public:
  146. const btCollisionObject *m_self_object;
  147. Vector3 *m_results;
  148. int m_resultMax;
  149. const Set<RID> *m_exclude;
  150. int m_count;
  151. bool collide_with_bodies;
  152. bool collide_with_areas;
  153. 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) :
  154. m_self_object(p_self_object),
  155. m_results(p_results),
  156. m_resultMax(p_resultMax),
  157. m_exclude(p_exclude),
  158. m_count(0),
  159. collide_with_bodies(p_collide_with_bodies),
  160. collide_with_areas(p_collide_with_areas) {}
  161. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  162. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  163. };
  164. struct GodotRestInfoContactResultCallback : public btCollisionWorld::ContactResultCallback {
  165. public:
  166. const btCollisionObject *m_self_object;
  167. PhysicsDirectSpaceState::ShapeRestInfo *m_result;
  168. const Set<RID> *m_exclude;
  169. bool m_collided;
  170. real_t m_min_distance;
  171. const btCollisionObject *m_rest_info_collision_object;
  172. btVector3 m_rest_info_bt_point;
  173. bool collide_with_bodies;
  174. bool collide_with_areas;
  175. GodotRestInfoContactResultCallback(btCollisionObject *p_self_object, PhysicsDirectSpaceState::ShapeRestInfo *p_result, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  176. m_self_object(p_self_object),
  177. m_result(p_result),
  178. m_exclude(p_exclude),
  179. m_collided(false),
  180. m_min_distance(0),
  181. collide_with_bodies(p_collide_with_bodies),
  182. collide_with_areas(p_collide_with_areas) {}
  183. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  184. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  185. };
  186. struct GodotDeepPenetrationContactResultCallback : public btManifoldResult {
  187. btVector3 m_pointNormalWorld;
  188. btVector3 m_pointWorld;
  189. btScalar m_penetration_distance;
  190. int m_other_compound_shape_index;
  191. GodotDeepPenetrationContactResultCallback(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap) :
  192. btManifoldResult(body0Wrap, body1Wrap),
  193. m_penetration_distance(0),
  194. m_other_compound_shape_index(0) {}
  195. void reset() {
  196. m_penetration_distance = 0;
  197. }
  198. bool hasHit() {
  199. return m_penetration_distance < 0;
  200. }
  201. virtual void addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorldOnB, btScalar depth);
  202. };
  203. #endif // GODOT_RESULT_CALLBACKS_H