godot_result_callbacks.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #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;
  51. int m_shapeId;
  52. bool collide_with_bodies;
  53. bool collide_with_areas;
  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. m_pickRay(false),
  59. m_shapeId(0),
  60. collide_with_bodies(p_collide_with_bodies),
  61. collide_with_areas(p_collide_with_areas) {}
  62. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  63. virtual btScalar addSingleResult(btCollisionWorld::LocalRayResult &rayResult, bool normalInWorldSpace) {
  64. if (rayResult.m_localShapeInfo)
  65. m_shapeId = rayResult.m_localShapeInfo->m_triangleIndex; // "m_triangleIndex" Is a odd name but contains the compound shape ID
  66. else
  67. m_shapeId = 0;
  68. return btCollisionWorld::ClosestRayResultCallback::addSingleResult(rayResult, normalInWorldSpace);
  69. }
  70. };
  71. // store all colliding object
  72. struct GodotAllConvexResultCallback : public btCollisionWorld::ConvexResultCallback {
  73. public:
  74. PhysicsDirectSpaceState::ShapeResult *m_results;
  75. int m_resultMax;
  76. const Set<RID> *m_exclude;
  77. int count;
  78. GodotAllConvexResultCallback(PhysicsDirectSpaceState::ShapeResult *p_results, int p_resultMax, const Set<RID> *p_exclude) :
  79. m_results(p_results),
  80. m_resultMax(p_resultMax),
  81. m_exclude(p_exclude),
  82. count(0) {}
  83. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  84. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace);
  85. };
  86. struct GodotKinClosestConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback {
  87. public:
  88. const RigidBodyBullet *m_self_object;
  89. const bool m_infinite_inertia;
  90. GodotKinClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, const RigidBodyBullet *p_self_object, bool p_infinite_inertia) :
  91. btCollisionWorld::ClosestConvexResultCallback(convexFromWorld, convexToWorld),
  92. m_self_object(p_self_object),
  93. m_infinite_inertia(p_infinite_inertia) {}
  94. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  95. };
  96. struct GodotClosestConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback {
  97. public:
  98. const Set<RID> *m_exclude;
  99. int m_shapeId;
  100. bool collide_with_bodies;
  101. bool collide_with_areas;
  102. GodotClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  103. btCollisionWorld::ClosestConvexResultCallback(convexFromWorld, convexToWorld),
  104. m_exclude(p_exclude),
  105. m_shapeId(0),
  106. collide_with_bodies(p_collide_with_bodies),
  107. collide_with_areas(p_collide_with_areas) {}
  108. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  109. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace);
  110. };
  111. struct GodotAllContactResultCallback : public btCollisionWorld::ContactResultCallback {
  112. public:
  113. const btCollisionObject *m_self_object;
  114. PhysicsDirectSpaceState::ShapeResult *m_results;
  115. int m_resultMax;
  116. const Set<RID> *m_exclude;
  117. int m_count;
  118. bool collide_with_bodies;
  119. bool collide_with_areas;
  120. 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) :
  121. m_self_object(p_self_object),
  122. m_results(p_results),
  123. m_resultMax(p_resultMax),
  124. m_exclude(p_exclude),
  125. m_count(0),
  126. collide_with_bodies(p_collide_with_bodies),
  127. collide_with_areas(p_collide_with_areas) {}
  128. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  129. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  130. };
  131. /// Returns the list of contacts pairs in this order: Local contact, other body contact
  132. struct GodotContactPairContactResultCallback : public btCollisionWorld::ContactResultCallback {
  133. public:
  134. const btCollisionObject *m_self_object;
  135. Vector3 *m_results;
  136. int m_resultMax;
  137. const Set<RID> *m_exclude;
  138. int m_count;
  139. bool collide_with_bodies;
  140. bool collide_with_areas;
  141. 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) :
  142. m_self_object(p_self_object),
  143. m_results(p_results),
  144. m_resultMax(p_resultMax),
  145. m_exclude(p_exclude),
  146. m_count(0),
  147. collide_with_bodies(p_collide_with_bodies),
  148. collide_with_areas(p_collide_with_areas) {}
  149. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  150. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  151. };
  152. struct GodotRestInfoContactResultCallback : public btCollisionWorld::ContactResultCallback {
  153. public:
  154. const btCollisionObject *m_self_object;
  155. PhysicsDirectSpaceState::ShapeRestInfo *m_result;
  156. const Set<RID> *m_exclude;
  157. bool m_collided;
  158. real_t m_min_distance;
  159. const btCollisionObject *m_rest_info_collision_object;
  160. btVector3 m_rest_info_bt_point;
  161. bool collide_with_bodies;
  162. bool collide_with_areas;
  163. GodotRestInfoContactResultCallback(btCollisionObject *p_self_object, PhysicsDirectSpaceState::ShapeRestInfo *p_result, const Set<RID> *p_exclude, bool p_collide_with_bodies, bool p_collide_with_areas) :
  164. m_self_object(p_self_object),
  165. m_result(p_result),
  166. m_exclude(p_exclude),
  167. m_collided(false),
  168. m_min_distance(0),
  169. collide_with_bodies(p_collide_with_bodies),
  170. collide_with_areas(p_collide_with_areas) {}
  171. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  172. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  173. };
  174. struct GodotDeepPenetrationContactResultCallback : public btManifoldResult {
  175. btVector3 m_pointNormalWorld;
  176. btVector3 m_pointWorld;
  177. btScalar m_penetration_distance;
  178. int m_other_compound_shape_index;
  179. GodotDeepPenetrationContactResultCallback(const btCollisionObjectWrapper *body0Wrap, const btCollisionObjectWrapper *body1Wrap) :
  180. btManifoldResult(body0Wrap, body1Wrap),
  181. m_penetration_distance(0),
  182. m_other_compound_shape_index(0) {}
  183. void reset() {
  184. m_penetration_distance = 0;
  185. }
  186. bool hasHit() {
  187. return m_penetration_distance < 0;
  188. }
  189. virtual void addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorldOnB, btScalar depth);
  190. };
  191. #endif // GODOT_RESULT_CALLBACKS_H