godot_result_callbacks.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*************************************************************************/
  2. /* godot_result_callbacks.h */
  3. /* Author: AndreaCatania */
  4. /*************************************************************************/
  5. /* This file is part of: */
  6. /* GODOT ENGINE */
  7. /* http://www.godotengine.org */
  8. /*************************************************************************/
  9. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  10. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  11. /* */
  12. /* Permission is hereby granted, free of charge, to any person obtaining */
  13. /* a copy of this software and associated documentation files (the */
  14. /* "Software"), to deal in the Software without restriction, including */
  15. /* without limitation the rights to use, copy, modify, merge, publish, */
  16. /* distribute, sublicense, and/or sell copies of the Software, and to */
  17. /* permit persons to whom the Software is furnished to do so, subject to */
  18. /* the following conditions: */
  19. /* */
  20. /* The above copyright notice and this permission notice shall be */
  21. /* included in all copies or substantial portions of the Software. */
  22. /* */
  23. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  24. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  25. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  26. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  27. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  28. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  29. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  30. /*************************************************************************/
  31. #ifndef GODOT_RESULT_CALLBACKS_H
  32. #define GODOT_RESULT_CALLBACKS_H
  33. #include "BulletCollision/BroadphaseCollision/btBroadphaseProxy.h"
  34. #include "btBulletDynamicsCommon.h"
  35. #include "servers/physics_server.h"
  36. #define MAX_PENETRATION_DEPTH 0.005
  37. class RigidBodyBullet;
  38. /// This class is required to implement custom collision behaviour in the broadphase
  39. struct GodotFilterCallback : public btOverlapFilterCallback {
  40. 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);
  41. // return true when pairs need collision
  42. virtual bool needBroadphaseCollision(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1) const;
  43. };
  44. /// It performs an additional check allow exclusions.
  45. struct GodotClosestRayResultCallback : public btCollisionWorld::ClosestRayResultCallback {
  46. const Set<RID> *m_exclude;
  47. bool m_pickRay;
  48. public:
  49. GodotClosestRayResultCallback(const btVector3 &rayFromWorld, const btVector3 &rayToWorld, const Set<RID> *p_exclude)
  50. : btCollisionWorld::ClosestRayResultCallback(rayFromWorld, rayToWorld), m_exclude(p_exclude), m_pickRay(false) {}
  51. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  52. };
  53. // store all colliding object
  54. struct GodotAllConvexResultCallback : public btCollisionWorld::ConvexResultCallback {
  55. public:
  56. PhysicsDirectSpaceState::ShapeResult *m_results;
  57. int m_resultMax;
  58. int count;
  59. const Set<RID> *m_exclude;
  60. GodotAllConvexResultCallback(PhysicsDirectSpaceState::ShapeResult *p_results, int p_resultMax, const Set<RID> *p_exclude)
  61. : m_results(p_results), m_exclude(p_exclude), m_resultMax(p_resultMax), count(0) {}
  62. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  63. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace);
  64. };
  65. struct GodotKinClosestConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback {
  66. public:
  67. const RigidBodyBullet *m_self_object;
  68. const bool m_ignore_areas;
  69. GodotKinClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, const RigidBodyBullet *p_self_object, bool p_ignore_areas)
  70. : btCollisionWorld::ClosestConvexResultCallback(convexFromWorld, convexToWorld), m_self_object(p_self_object), m_ignore_areas(p_ignore_areas) {}
  71. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  72. };
  73. struct GodotClosestConvexResultCallback : public btCollisionWorld::ClosestConvexResultCallback {
  74. public:
  75. const Set<RID> *m_exclude;
  76. int m_shapePart;
  77. GodotClosestConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, const Set<RID> *p_exclude)
  78. : btCollisionWorld::ClosestConvexResultCallback(convexFromWorld, convexToWorld), m_exclude(p_exclude) {}
  79. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  80. virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace);
  81. };
  82. struct GodotAllContactResultCallback : public btCollisionWorld::ContactResultCallback {
  83. public:
  84. const btCollisionObject *m_self_object;
  85. PhysicsDirectSpaceState::ShapeResult *m_results;
  86. int m_resultMax;
  87. int m_count;
  88. const Set<RID> *m_exclude;
  89. GodotAllContactResultCallback(btCollisionObject *p_self_object, PhysicsDirectSpaceState::ShapeResult *p_results, int p_resultMax, const Set<RID> *p_exclude)
  90. : m_self_object(p_self_object), m_results(p_results), m_exclude(p_exclude), m_resultMax(p_resultMax), m_count(0) {}
  91. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  92. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  93. };
  94. /// Returns the list of contacts pairs in this order: Local contact, other body contact
  95. struct GodotContactPairContactResultCallback : public btCollisionWorld::ContactResultCallback {
  96. public:
  97. const btCollisionObject *m_self_object;
  98. Vector3 *m_results;
  99. int m_resultMax;
  100. int m_count;
  101. const Set<RID> *m_exclude;
  102. GodotContactPairContactResultCallback(btCollisionObject *p_self_object, Vector3 *p_results, int p_resultMax, const Set<RID> *p_exclude)
  103. : m_self_object(p_self_object), m_results(p_results), m_exclude(p_exclude), m_resultMax(p_resultMax), m_count(0) {}
  104. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  105. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  106. };
  107. struct GodotRestInfoContactResultCallback : public btCollisionWorld::ContactResultCallback {
  108. public:
  109. const btCollisionObject *m_self_object;
  110. PhysicsDirectSpaceState::ShapeRestInfo *m_result;
  111. bool m_collided;
  112. real_t m_min_distance;
  113. const btCollisionObject *m_rest_info_collision_object;
  114. btVector3 m_rest_info_bt_point;
  115. const Set<RID> *m_exclude;
  116. GodotRestInfoContactResultCallback(btCollisionObject *p_self_object, PhysicsDirectSpaceState::ShapeRestInfo *p_result, const Set<RID> *p_exclude)
  117. : m_self_object(p_self_object), m_result(p_result), m_exclude(p_exclude), m_collided(false), m_min_distance(0) {}
  118. virtual bool needsCollision(btBroadphaseProxy *proxy0) const;
  119. virtual btScalar addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1);
  120. };
  121. struct GodotRecoverAndClosestContactResultCallback : public btCollisionWorld::ContactResultCallback {
  122. public:
  123. btVector3 m_pointNormalWorld;
  124. btVector3 m_pointWorld;
  125. btScalar m_penetration_distance;
  126. int m_other_compound_shape_index;
  127. const btCollisionObject *m_pointCollisionObject;
  128. const RigidBodyBullet *m_self_object;
  129. bool m_ignore_areas;
  130. btScalar m_most_penetrated_distance;
  131. btVector3 m_recover_penetration;
  132. GodotRecoverAndClosestContactResultCallback()
  133. : m_pointCollisionObject(NULL), m_penetration_distance(0), m_other_compound_shape_index(0), m_self_object(NULL), m_ignore_areas(true), m_most_penetrated_distance(1e20), m_recover_penetration(0, 0, 0) {}
  134. GodotRecoverAndClosestContactResultCallback(const RigidBodyBullet *p_self_object, bool p_ignore_areas)
  135. : m_pointCollisionObject(NULL), m_penetration_distance(0), m_other_compound_shape_index(0), m_self_object(p_self_object), m_ignore_areas(p_ignore_areas), m_most_penetrated_distance(9999999999), m_recover_penetration(0, 0, 0) {}
  136. void reset() {
  137. m_pointCollisionObject = NULL;
  138. m_most_penetrated_distance = 1e20;
  139. m_recover_penetration.setZero();
  140. }
  141. bool hasHit() {
  142. return m_pointCollisionObject;
  143. }
  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. #endif // GODOT_RESULT_CALLBACKS_H