godot_result_callbacks.cpp 12 KB

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