godot_result_callbacks.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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_3d.h"
  33. #include <BulletCollision/BroadphaseCollision/btBroadphaseProxy.h>
  34. #include <btBulletDynamicsCommon.h>
  35. /**
  36. @author AndreaCatania
  37. */
  38. class RigidBodyBullet;
  39. /// This callback is injected inside bullet server and allow me to smooth contacts against trimesh
  40. bool godotContactAddedCallback(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  41. /// This class is required to implement custom collision behaviour in the broadphase
  42. struct GodotFilterCallback : public btOverlapFilterCallback {
  43. 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);
  44. // return true when pairs need collision
  45. virtual bool needBroadphaseCollision(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1) const;
  46. };
  47. /// It performs an additional check allow exclusions.
  48. struct GodotClosestRayResultCallback : public btCollisionWorld::ClosestRayResultCallback {
  49. const Set<RID> *m_exclude;
  50. bool m_pickRay = false;
  51. int m_shapeId = 0;
  52. bool collide_with_bodies = false;
  53. bool collide_with_areas = false;
  54. public:
  55. GodotClosestRayResultCallback(const btVector3 &rayFromWorld, const btVector3 &rayToWorld, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  56. btCollisionWorld::ClosestRayResultCallback(rayFromWorld, rayToWorld),
  57. m_exclude(p_exclude),
  58. collide_with_bodies(p_collide_with_bodies),
  59. collide_with_areas(p_collide_with_areas) {}
  60. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  61. virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult &rayResult, bool normalInWorldSpace) {
  62. if (rayResult.m_localShapeInfo) {
  63. m_shapeId = rayResult.m_localShapeInfo->m_triangleIndex; // "m_triangleIndex" Is a odd name but contains the compound shape ID
  64. } else {
  65. m_shapeId = 0;
  66. }
  67. return btCollisionWorld::ClosestRayResultCallback::addSingleResult(rayResult, normalInWorldSpace);
  68. }
  69. };
  70. // store all colliding object
  71. struct GodotAllConvexResultCallback : public btCollisionWorld::ConvexResultCallback {
  72. public:
  73. PhysicsDirectSpaceState3D::ShapeResult *m_results = nullptr;
  74. int m_resultMax = 0;
  75. const Set<RID> *m_exclude;
  76. int count = 0;
  77. GodotAllConvexResultCallback(PhysicsDirectSpaceState3D::ShapeResult *p_results, int p_resultMax, const Set<RID> *p_exclude) :
  78. m_results(p_results),
  79. m_resultMax(p_resultMax),
  80. m_exclude(p_exclude) {}
  81. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  82. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace);
  83. };
  84. struct GodotKinClosestConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback {
  85. public:
  86. const RigidBodyBullet *m_self_object;
  87. const bool m_infinite_inertia;
  88. GodotKinClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, const RigidBodyBullet *p_self_object, bool p_infinite_inertia) :
  89. btCollisionWorld::ClosestConvexResultCallback(convexFromWorld, convexToWorld),
  90. m_self_object(p_self_object),
  91. m_infinite_inertia(p_infinite_inertia) {}
  92. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  93. };
  94. struct GodotClosestConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback {
  95. public:
  96. const Set<RID> *m_exclude;
  97. int m_shapeId = 0;
  98. bool collide_with_bodies = false;
  99. bool collide_with_areas = false;
  100. GodotClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  101. btCollisionWorld::ClosestConvexResultCallback(convexFromWorld, convexToWorld),
  102. m_exclude(p_exclude),
  103. collide_with_bodies(p_collide_with_bodies),
  104. collide_with_areas(p_collide_with_areas) {}
  105. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  106. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace);
  107. };
  108. struct GodotAllContactResultCallback : public btCollisionWorld::ContactResultCallback {
  109. public:
  110. const btCollisionObject *m_self_object;
  111. PhysicsDirectSpaceState3D::ShapeResult *m_results = nullptr;
  112. int m_resultMax = 0;
  113. const Set<RID> *m_exclude;
  114. int m_count = 0;
  115. bool collide_with_bodies = false;
  116. bool collide_with_areas = false;
  117. GodotAllContactResultCallback(btCollisionObject *p_self_object, PhysicsDirectSpaceState3D::ShapeResult *p_results, int p_resultMax, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  118. m_self_object(p_self_object),
  119. m_results(p_results),
  120. m_resultMax(p_resultMax),
  121. m_exclude(p_exclude),
  122. collide_with_bodies(p_collide_with_bodies),
  123. collide_with_areas(p_collide_with_areas) {}
  124. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  125. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  126. };
  127. /// Returns the list of contacts pairs in this order: Local contact, other body contact
  128. struct GodotContactPairContactResultCallback : public btCollisionWorld::ContactResultCallback {
  129. public:
  130. const btCollisionObject *m_self_object;
  131. Vector3 *m_results = nullptr;
  132. int m_resultMax = 0;
  133. const Set<RID> *m_exclude;
  134. int m_count = 0;
  135. bool collide_with_bodies = false;
  136. bool collide_with_areas = false;
  137. 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) :
  138. m_self_object(p_self_object),
  139. m_results(p_results),
  140. m_resultMax(p_resultMax),
  141. m_exclude(p_exclude),
  142. collide_with_bodies(p_collide_with_bodies),
  143. collide_with_areas(p_collide_with_areas) {}
  144. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  145. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  146. };
  147. struct GodotRestInfoContactResultCallback : public btCollisionWorld::ContactResultCallback {
  148. public:
  149. const btCollisionObject *m_self_object;
  150. PhysicsDirectSpaceState3D::ShapeRestInfo *m_result = nullptr;
  151. const Set<RID> *m_exclude;
  152. bool m_collided = false;
  153. real_t m_min_distance = 0.0;
  154. const btCollisionObject *m_rest_info_collision_object = nullptr;
  155. btVector3 m_rest_info_bt_point;
  156. bool collide_with_bodies = false;
  157. bool collide_with_areas = false;
  158. GodotRestInfoContactResultCallback(btCollisionObject *p_self_object, PhysicsDirectSpaceState3D::ShapeRestInfo *p_result, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  159. m_self_object(p_self_object),
  160. m_result(p_result),
  161. m_exclude(p_exclude),
  162. collide_with_bodies(p_collide_with_bodies),
  163. collide_with_areas(p_collide_with_areas) {}
  164. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  165. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  166. };
  167. struct GodotDeepPenetrationContactResultCallback : public btManifoldResult {
  168. btVector3 m_pointNormalWorld;
  169. btVector3 m_pointWorld;
  170. btScalar m_penetration_distance = 0;
  171. int m_other_compound_shape_index = 0;
  172. GodotDeepPenetrationContactResultCallback(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap) :
  173. btManifoldResult(body0Wrap, body1Wrap) {}
  174. void reset() {
  175. m_penetration_distance = 0;
  176. }
  177. bool hasHit() {
  178. return m_penetration_distance < 0;
  179. }
  180. virtual void addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorldOnB, btScalar depth);
  181. };
  182. #endif // GODOT_RESULT_CALLBACKS_H