godot_result_callbacks.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /**************************************************************************/
  2. /* godot_result_callbacks.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. // Triangle index is an odd name but contains the compound shape ID.
  99. // A shape part of -1 indicates the index is a shape index and not a triangle index.
  100. if (convexResult.m_localShapeInfo && convexResult.m_localShapeInfo->m_shapePart == -1) {
  101. result.shape = convexResult.m_localShapeInfo->m_triangleIndex;
  102. } else {
  103. result.shape = 0;
  104. }
  105. result.rid = gObj->get_self();
  106. result.collider_id = gObj->get_instance_id();
  107. result.collider = 0 == result.collider_id ? nullptr : ObjectDB::get_instance(result.collider_id);
  108. ++count;
  109. return 1; // not used by bullet
  110. }
  111. bool GodotKinClosestConvexResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  112. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  113. if (needs) {
  114. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  115. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  116. if (gObj == m_self_object) {
  117. return false;
  118. } else {
  119. // A kinematic body can't be stopped by a rigid body since the mass of kinematic body is infinite
  120. if (m_infinite_inertia && !btObj->isStaticOrKinematicObject()) {
  121. return false;
  122. }
  123. if (gObj->getType() == CollisionObjectBullet::TYPE_AREA) {
  124. return false;
  125. }
  126. if (m_self_object->has_collision_exception(gObj) || gObj->has_collision_exception(m_self_object)) {
  127. return false;
  128. }
  129. if (m_exclude->has(gObj->get_self())) {
  130. return false;
  131. }
  132. }
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. }
  138. bool GodotClosestConvexResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  139. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  140. if (needs) {
  141. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  142. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  143. if (CollisionObjectBullet::TYPE_AREA == gObj->getType()) {
  144. if (!collide_with_areas) {
  145. return false;
  146. }
  147. } else {
  148. if (!collide_with_bodies) {
  149. return false;
  150. }
  151. }
  152. if (m_exclude->has(gObj->get_self())) {
  153. return false;
  154. }
  155. return true;
  156. } else {
  157. return false;
  158. }
  159. }
  160. btScalar GodotClosestConvexResultCallback::addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace) {
  161. // Triangle index is an odd name but contains the compound shape ID.
  162. // A shape part of -1 indicates the index is a shape index and not a triangle index.
  163. if (convexResult.m_localShapeInfo && convexResult.m_localShapeInfo->m_shapePart == -1) {
  164. m_shapeId = convexResult.m_localShapeInfo->m_triangleIndex;
  165. } else {
  166. m_shapeId = 0;
  167. }
  168. return btCollisionWorld::ClosestConvexResultCallback::addSingleResult(convexResult, normalInWorldSpace);
  169. }
  170. bool GodotAllContactResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  171. if (m_count >= m_resultMax) {
  172. return false;
  173. }
  174. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  175. if (needs) {
  176. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  177. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  178. if (CollisionObjectBullet::TYPE_AREA == gObj->getType()) {
  179. if (!collide_with_areas) {
  180. return false;
  181. }
  182. } else {
  183. if (!collide_with_bodies) {
  184. return false;
  185. }
  186. }
  187. if (m_exclude->has(gObj->get_self())) {
  188. return false;
  189. }
  190. return true;
  191. } else {
  192. return false;
  193. }
  194. }
  195. btScalar GodotAllContactResultCallback::addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1) {
  196. if (m_count >= m_resultMax) {
  197. return cp.getDistance();
  198. }
  199. if (cp.getDistance() <= 0) {
  200. PhysicsDirectSpaceState::ShapeResult &result = m_results[m_count];
  201. // Penetrated
  202. CollisionObjectBullet *colObj;
  203. if (m_self_object == colObj0Wrap->getCollisionObject()) {
  204. colObj = static_cast<CollisionObjectBullet *>(colObj1Wrap->getCollisionObject()->getUserPointer());
  205. // Checking for compound shape because the index might be uninitialized otherwise.
  206. // A partId of -1 indicates the index is a shape index and not a triangle index.
  207. if (colObj1Wrap->getCollisionObject()->getCollisionShape()->isCompound() && cp.m_partId1 == -1) {
  208. result.shape = cp.m_index1;
  209. } else {
  210. result.shape = 0;
  211. }
  212. } else {
  213. colObj = static_cast<CollisionObjectBullet *>(colObj0Wrap->getCollisionObject()->getUserPointer());
  214. // Checking for compound shape because the index might be uninitialized otherwise.
  215. // A partId of -1 indicates the index is a shape index and not a triangle index.
  216. if (colObj0Wrap->getCollisionObject()->getCollisionShape()->isCompound() && cp.m_partId0 == -1) {
  217. result.shape = cp.m_index0;
  218. } else {
  219. result.shape = 0;
  220. }
  221. }
  222. result.collider_id = colObj->get_instance_id();
  223. result.collider = 0 == result.collider_id ? nullptr : ObjectDB::get_instance(result.collider_id);
  224. result.rid = colObj->get_self();
  225. ++m_count;
  226. }
  227. return cp.getDistance();
  228. }
  229. bool GodotContactPairContactResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  230. if (m_count >= m_resultMax) {
  231. return false;
  232. }
  233. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  234. if (needs) {
  235. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  236. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  237. if (CollisionObjectBullet::TYPE_AREA == gObj->getType()) {
  238. if (!collide_with_areas) {
  239. return false;
  240. }
  241. } else {
  242. if (!collide_with_bodies) {
  243. return false;
  244. }
  245. }
  246. if (m_exclude->has(gObj->get_self())) {
  247. return false;
  248. }
  249. return true;
  250. } else {
  251. return false;
  252. }
  253. }
  254. btScalar GodotContactPairContactResultCallback::addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1) {
  255. if (m_count >= m_resultMax) {
  256. return 1; // not used by bullet
  257. }
  258. // In each contact pair, the contact on the shape which was passed to collide_shape (where this callback is used) is put first.
  259. if (m_self_object == colObj0Wrap->getCollisionObject()) {
  260. B_TO_G(cp.getPositionWorldOnA(), m_results[m_count * 2 + 0]);
  261. B_TO_G(cp.getPositionWorldOnB(), m_results[m_count * 2 + 1]);
  262. } else {
  263. B_TO_G(cp.getPositionWorldOnB(), m_results[m_count * 2 + 0]);
  264. B_TO_G(cp.getPositionWorldOnA(), m_results[m_count * 2 + 1]);
  265. }
  266. ++m_count;
  267. return 1; // Not used by bullet
  268. }
  269. bool GodotRestInfoContactResultCallback::needsCollision(btBroadphaseProxy *proxy0) const {
  270. const bool needs = GodotFilterCallback::test_collision_filters(m_collisionFilterGroup, m_collisionFilterMask, proxy0->m_collisionFilterGroup, proxy0->m_collisionFilterMask);
  271. if (needs) {
  272. btCollisionObject *btObj = static_cast<btCollisionObject *>(proxy0->m_clientObject);
  273. CollisionObjectBullet *gObj = static_cast<CollisionObjectBullet *>(btObj->getUserPointer());
  274. if (CollisionObjectBullet::TYPE_AREA == gObj->getType()) {
  275. if (!collide_with_areas) {
  276. return false;
  277. }
  278. } else {
  279. if (!collide_with_bodies) {
  280. return false;
  281. }
  282. }
  283. if (m_exclude->has(gObj->get_self())) {
  284. return false;
  285. }
  286. return true;
  287. } else {
  288. return false;
  289. }
  290. }
  291. btScalar GodotRestInfoContactResultCallback::addSingleResult(btManifoldPoint &cp, const btCollisionObjectWrapper *colObj0Wrap, int partId0, int index0, const btCollisionObjectWrapper *colObj1Wrap, int partId1, int index1) {
  292. if (cp.getDistance() <= m_min_distance) {
  293. m_min_distance = cp.getDistance();
  294. CollisionObjectBullet *colObj;
  295. if (m_self_object == colObj0Wrap->getCollisionObject()) {
  296. colObj = static_cast<CollisionObjectBullet *>(colObj1Wrap->getCollisionObject()->getUserPointer());
  297. // Checking for compound shape because the index might be uninitialized otherwise.
  298. // A partId of -1 indicates the index is a shape index and not a triangle index.
  299. if (colObj1Wrap->getCollisionObject()->getCollisionShape()->isCompound() && cp.m_partId1 == -1) {
  300. m_result->shape = cp.m_index1;
  301. } else {
  302. m_result->shape = 0;
  303. }
  304. B_TO_G(cp.getPositionWorldOnB(), m_result->point);
  305. B_TO_G(cp.m_normalWorldOnB, m_result->normal);
  306. m_rest_info_bt_point = cp.getPositionWorldOnB();
  307. m_rest_info_collision_object = colObj1Wrap->getCollisionObject();
  308. } else {
  309. colObj = static_cast<CollisionObjectBullet *>(colObj0Wrap->getCollisionObject()->getUserPointer());
  310. // Checking for compound shape because the index might be uninitialized otherwise.
  311. // A partId of -1 indicates the index is a shape index and not a triangle index.
  312. if (colObj0Wrap->getCollisionObject()->getCollisionShape()->isCompound() && cp.m_partId0 == -1) {
  313. m_result->shape = cp.m_index0;
  314. } else {
  315. m_result->shape = 0;
  316. }
  317. B_TO_G(cp.m_normalWorldOnB * -1, m_result->normal);
  318. m_rest_info_bt_point = cp.getPositionWorldOnA();
  319. m_rest_info_collision_object = colObj0Wrap->getCollisionObject();
  320. }
  321. m_result->collider_id = colObj->get_instance_id();
  322. m_result->rid = colObj->get_self();
  323. m_collided = true;
  324. }
  325. return 1; // Not used by bullet
  326. }
  327. void GodotDeepPenetrationContactResultCallback::addContactPoint(const btVector3 &normalOnBInWorld, const btVector3 &pointInWorldOnB, btScalar depth) {
  328. if (m_penetration_distance > depth) { // Has penetration?
  329. const bool isSwapped = m_manifoldPtr->getBody0() != m_body0Wrap->getCollisionObject();
  330. m_penetration_distance = depth;
  331. m_other_compound_shape_index = isSwapped ? m_index0 : m_index1;
  332. m_pointWorld = isSwapped ? (pointInWorldOnB + (normalOnBInWorld * depth)) : pointInWorldOnB;
  333. m_pointNormalWorld = isSwapped ? normalOnBInWorld * -1 : normalOnBInWorld;
  334. }
  335. }