godot_result_callbacks.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*************************************************************************/
  2. /* godot_result_callbacks.cpp */
  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. #include "godot_result_callbacks.h"
  31. #include "area_bullet.h"
  32. #include "bullet_types_converter.h"
  33. #include "collision_object_bullet.h"
  34. #include "rigid_body_bullet.h"
  35. #include <BulletCollision/CollisionDispatch/btInternalEdgeUtility.h>
  36. /**
  37. @author AndreaCatania
  38. */
  39. bool godotContactAddedCallback(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1) {
  40. if (!colObj1Wrap->getCollisionObject()->getCollisionShape()->isCompound()) {
  41. btAdjustInternalEdgeContacts(cp, colObj1Wrap, colObj0Wrap, partId1, index1);
  42. }
  43. return true;
  44. }
  45. bool GodotFilterCallback::test_collision_filters(uint32_t body0_collision_layer, uint32_t body0_collision_mask, uint32_t body1_collision_layer, uint32_t body1_collision_mask) {
  46. return body0_collision_layer & body1_collision_mask || body1_collision_layer & body0_collision_mask;
  47. }
  48. bool GodotFilterCallback::needBroadphaseCollision(btBroadphaseProxy *proxy0, btBroadphaseProxy *proxy1) const {
  49. return GodotFilterCallback::test_collision_filters(proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask, proxy1->m_collisionFilterGroup, proxy1->m_collisionFilterMask);
  50. }
  51. bool GodotClosestRayResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  52. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  53. if (needs) {
  54. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  55. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  56. if (CollisionObjectBullet::TYPE_AREA == gObj->getType()) {
  57. if (!collide_with_areas)
  58. return false;
  59. } else {
  60. if (!collide_with_bodies)
  61. return false;
  62. }
  63. if (m_pickRay && !gObj->is_ray_pickable()) {
  64. return false;
  65. }
  66. if (m_exclude->has(gObj->get_self())) {
  67. return false;
  68. }
  69. return true;
  70. } else {
  71. return false;
  72. }
  73. }
  74. bool GodotAllConvexResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  75. if (count >= m_resultMax)
  76. return false;
  77. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  78. if (needs) {
  79. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  80. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  81. if (m_exclude->has(gObj->get_self())) {
  82. return false;
  83. }
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. }
  89. btScalar GodotAllConvexResultCallback::addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace) {
  90. if (count >= m_resultMax)
  91. return 1; // not used by bullet
  92. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(convexResult.m_hitCollisionObject->getUserPointer());
  93. PhysicsDirectSpaceState::ShapeResult &result = m_results[count];
  94. result.shape = convexResult.m_localShapeInfo->m_triangleIndex; // "m_triangleIndex" Is a odd name but contains the compound shape ID
  95. result.rid = gObj->get_self();
  96. result.collider_id = gObj->get_instance_id();
  97. result.collider = 0 == result.collider_id ? NULL : ObjectDB::get_instance(result.collider_id);
  98. ++count;
  99. return 1; // not used by bullet
  100. }
  101. bool GodotKinClosestConvexResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  102. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  103. if (needs) {
  104. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  105. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  106. if (gObj == m_self_object) {
  107. return false;
  108. } else {
  109. // A kinematic body can't be stopped by a rigid body since the mass of kinematic body is infinite
  110. if (m_infinite_inertia && !btObj->isStaticOrKinematicObject())
  111. return false;
  112. if (gObj->getType() == CollisionObjectBullet::TYPE_AREA)
  113. return false;
  114. if (m_self_object->has_collision_exception(gObj) || gObj->has_collision_exception(m_self_object))
  115. return false;
  116. }
  117. return true;
  118. } else {
  119. return false;
  120. }
  121. }
  122. bool GodotClosestConvexResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  123. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  124. if (needs) {
  125. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  126. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  127. if (CollisionObjectBullet::TYPE_AREA == gObj->getType()) {
  128. if (!collide_with_areas)
  129. return false;
  130. } else {
  131. if (!collide_with_bodies)
  132. return false;
  133. }
  134. if (m_exclude->has(gObj->get_self())) {
  135. return false;
  136. }
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. }
  142. btScalar GodotClosestConvexResultCallback::addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace) {
  143. if (convexResult.m_localShapeInfo)
  144. m_shapeId = convexResult.m_localShapeInfo->m_triangleIndex; // "m_triangleIndex" Is a odd name but contains the compound shape ID
  145. else
  146. m_shapeId = 0;
  147. return btCollisionWorld::ClosestConvexResultCallback::addSingleResult(convexResult, normalInWorldSpace);
  148. }
  149. bool GodotAllContactResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  150. if (m_count >= m_resultMax)
  151. return false;
  152. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  153. if (needs) {
  154. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  155. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  156. if (CollisionObjectBullet::TYPE_AREA == gObj->getType()) {
  157. if (!collide_with_areas)
  158. return false;
  159. } else {
  160. if (!collide_with_bodies)
  161. return false;
  162. }
  163. if (m_exclude->has(gObj->get_self())) {
  164. return false;
  165. }
  166. return true;
  167. } else {
  168. return false;
  169. }
  170. }
  171. btScalar GodotAllContactResultCallback::addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1) {
  172. if (m_count >= m_resultMax)
  173. return cp.getDistance();
  174. if (cp.getDistance() <= 0) {
  175. PhysicsDirectSpaceState::ShapeResult &result = m_results[m_count];
  176. // Penetrated
  177. CollisionObjectBullet *colObj;
  178. if (m_self_object == colObj0Wrap->getCollisionObject()) {
  179. colObj = static_cast<CollisionObjectBullet *>(colObj1Wrap->getCollisionObject()->getUserPointer());
  180. result.shape = cp.m_index1;
  181. } else {
  182. colObj = static_cast<CollisionObjectBullet *>(colObj0Wrap->getCollisionObject()->getUserPointer());
  183. result.shape = cp.m_index0;
  184. }
  185. result.collider_id = colObj->get_instance_id();
  186. result.collider = 0 == result.collider_id ? NULL : ObjectDB::get_instance(result.collider_id);
  187. result.rid = colObj->get_self();
  188. ++m_count;
  189. }
  190. return cp.getDistance();
  191. }
  192. bool GodotContactPairContactResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  193. if (m_count >= m_resultMax)
  194. return false;
  195. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  196. if (needs) {
  197. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  198. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  199. if (CollisionObjectBullet::TYPE_AREA == gObj->getType()) {
  200. if (!collide_with_areas)
  201. return false;
  202. } else {
  203. if (!collide_with_bodies)
  204. return false;
  205. }
  206. if (m_exclude->has(gObj->get_self())) {
  207. return false;
  208. }
  209. return true;
  210. } else {
  211. return false;
  212. }
  213. }
  214. btScalar GodotContactPairContactResultCallback::addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1) {
  215. if (m_count >= m_resultMax)
  216. return 1; // not used by bullet
  217. if (m_self_object == colObj0Wrap->getCollisionObject()) {
  218. B_TO_G(cp.m_localPointA, m_results[m_count * 2 + 0]); // Local contact
  219. B_TO_G(cp.m_localPointB, m_results[m_count * 2 + 1]);
  220. } else {
  221. B_TO_G(cp.m_localPointB, m_results[m_count * 2 + 0]); // Local contact
  222. B_TO_G(cp.m_localPointA, m_results[m_count * 2 + 1]);
  223. }
  224. ++m_count;
  225. return 1; // Not used by bullet
  226. }
  227. bool GodotRestInfoContactResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  228. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  229. if (needs) {
  230. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  231. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  232. if (CollisionObjectBullet::TYPE_AREA == gObj->getType()) {
  233. if (!collide_with_areas)
  234. return false;
  235. } else {
  236. if (!collide_with_bodies)
  237. return false;
  238. }
  239. if (m_exclude->has(gObj->get_self())) {
  240. return false;
  241. }
  242. return true;
  243. } else {
  244. return false;
  245. }
  246. }
  247. btScalar GodotRestInfoContactResultCallback::addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1) {
  248. if (cp.getDistance() <= m_min_distance) {
  249. m_min_distance = cp.getDistance();
  250. CollisionObjectBullet *colObj;
  251. if (m_self_object == colObj0Wrap->getCollisionObject()) {
  252. colObj = static_cast<CollisionObjectBullet *>(colObj1Wrap->getCollisionObject()->getUserPointer());
  253. m_result->shape = cp.m_index1;
  254. B_TO_G(cp.getPositionWorldOnB(), m_result->point);
  255. B_TO_G(cp.m_normalWorldOnB, m_result->normal);
  256. m_rest_info_bt_point = cp.getPositionWorldOnB();
  257. m_rest_info_collision_object = colObj1Wrap->getCollisionObject();
  258. } else {
  259. colObj = static_cast<CollisionObjectBullet *>(colObj0Wrap->getCollisionObject()->getUserPointer());
  260. m_result->shape = cp.m_index0;
  261. B_TO_G(cp.m_normalWorldOnB * -1, m_result->normal);
  262. m_rest_info_bt_point = cp.getPositionWorldOnA();
  263. m_rest_info_collision_object = colObj0Wrap->getCollisionObject();
  264. }
  265. m_result->collider_id = colObj->get_instance_id();
  266. m_result->rid = colObj->get_self();
  267. m_collided = true;
  268. }
  269. return 1; // Not used by bullet
  270. }
  271. void GodotDeepPenetrationContactResultCallback::addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorldOnB, btScalar depth) {
  272. if (m_penetration_distance > depth) { // Has penetration?
  273. const bool isSwapped = m_manifoldPtr->getBody0() != m_body0Wrap->getCollisionObject();
  274. m_penetration_distance = depth;
  275. m_other_compound_shape_index = isSwapped ? m_index0 : m_index1;
  276. m_pointWorld = isSwapped ? (pointInWorldOnB + (normalOnBInWorld * depth)) : pointInWorldOnB;
  277. const btCollisionObjectWrapper *bw0 = m_body0Wrap;
  278. if (isSwapped)
  279. bw0 = m_body1Wrap;
  280. if (bw0->getCollisionShape()->getShapeType() == CUSTOM_CONVEX_SHAPE_TYPE) {
  281. m_pointNormalWorld = bw0->m_worldTransform.getBasis().transpose() * btVector3(0, 0, 1);
  282. } else {
  283. m_pointNormalWorld = isSwapped ? normalOnBInWorld * -1 : normalOnBInWorld;
  284. }
  285. }
  286. }