godot_result_callbacks.cpp 13 KB

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