collision_object_bullet.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*************************************************************************/
  2. /* collision_object_bullet.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 "collision_object_bullet.h"
  31. #include "area_bullet.h"
  32. #include "bullet_physics_server.h"
  33. #include "bullet_types_converter.h"
  34. #include "bullet_utilities.h"
  35. #include "shape_bullet.h"
  36. #include "space_bullet.h"
  37. #include <btBulletCollisionCommon.h>
  38. /**
  39. @author AndreaCatania
  40. */
  41. // We enable dynamic AABB tree so that we can actually perform a broadphase on bodies with compound collision shapes.
  42. // This is crucial for the performance of kinematic bodies and for bodies with transforming shapes.
  43. #define enableDynamicAabbTree true
  44. CollisionObjectBullet::ShapeWrapper::~ShapeWrapper() {}
  45. void CollisionObjectBullet::ShapeWrapper::set_transform(const Transform &p_transform) {
  46. G_TO_B(p_transform.get_basis().get_scale_abs(), scale);
  47. G_TO_B(p_transform, transform);
  48. UNSCALE_BT_BASIS(transform);
  49. }
  50. void CollisionObjectBullet::ShapeWrapper::set_transform(const btTransform &p_transform) {
  51. transform = p_transform;
  52. }
  53. btTransform CollisionObjectBullet::ShapeWrapper::get_adjusted_transform() const {
  54. if (shape->get_type() == PhysicsServer::SHAPE_HEIGHTMAP) {
  55. const HeightMapShapeBullet *hm_shape = (const HeightMapShapeBullet *)shape; // should be safe to cast now
  56. btTransform adjusted_transform;
  57. // Bullet centers our heightmap:
  58. // https://github.com/bulletphysics/bullet3/blob/master/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h#L33
  59. // This is really counter intuitive so we're adjusting for it
  60. adjusted_transform.setIdentity();
  61. adjusted_transform.setOrigin(btVector3(0.0, hm_shape->min_height + ((hm_shape->max_height - hm_shape->min_height) * 0.5), 0.0));
  62. adjusted_transform *= transform;
  63. return adjusted_transform;
  64. } else {
  65. return transform;
  66. }
  67. }
  68. void CollisionObjectBullet::ShapeWrapper::claim_bt_shape(const btVector3 &body_scale) {
  69. if (!bt_shape) {
  70. if (active) {
  71. bt_shape = shape->create_bt_shape(scale * body_scale);
  72. } else {
  73. bt_shape = ShapeBullet::create_shape_empty();
  74. }
  75. }
  76. }
  77. CollisionObjectBullet::CollisionObjectBullet(Type p_type) :
  78. RIDBullet(),
  79. type(p_type),
  80. instance_id(0),
  81. collisionLayer(0),
  82. collisionMask(0),
  83. collisionsEnabled(true),
  84. m_isStatic(false),
  85. ray_pickable(false),
  86. bt_collision_object(nullptr),
  87. body_scale(1., 1., 1.),
  88. force_shape_reset(false),
  89. space(nullptr),
  90. isTransformChanged(false) {}
  91. CollisionObjectBullet::~CollisionObjectBullet() {
  92. // Remove all overlapping, notify is not required since godot take care of it
  93. for (int i = areasOverlapped.size() - 1; 0 <= i; --i) {
  94. areasOverlapped[i]->remove_overlap(this, /*Notify*/ false);
  95. }
  96. destroyBulletCollisionObject();
  97. }
  98. bool equal(real_t first, real_t second) {
  99. return Math::abs(first - second) <= 0.001f;
  100. }
  101. void CollisionObjectBullet::set_body_scale(const Vector3 &p_new_scale) {
  102. if (!equal(p_new_scale[0], body_scale[0]) || !equal(p_new_scale[1], body_scale[1]) || !equal(p_new_scale[2], body_scale[2])) {
  103. body_scale = p_new_scale;
  104. body_scale_changed();
  105. }
  106. }
  107. btVector3 CollisionObjectBullet::get_bt_body_scale() const {
  108. btVector3 s;
  109. G_TO_B(body_scale, s);
  110. return s;
  111. }
  112. void CollisionObjectBullet::body_scale_changed() {
  113. force_shape_reset = true;
  114. }
  115. void CollisionObjectBullet::destroyBulletCollisionObject() {
  116. bulletdelete(bt_collision_object);
  117. }
  118. void CollisionObjectBullet::setupBulletCollisionObject(btCollisionObject *p_collisionObject) {
  119. bt_collision_object = p_collisionObject;
  120. bt_collision_object->setUserPointer(this);
  121. bt_collision_object->setUserIndex(type);
  122. // Force the enabling of collision and avoid problems
  123. set_collision_enabled(collisionsEnabled);
  124. p_collisionObject->setCollisionFlags(p_collisionObject->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
  125. }
  126. void CollisionObjectBullet::add_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject) {
  127. exceptions.insert(p_ignoreCollisionObject->get_self());
  128. if (!bt_collision_object) {
  129. return;
  130. }
  131. bt_collision_object->setIgnoreCollisionCheck(p_ignoreCollisionObject->bt_collision_object, true);
  132. if (space) {
  133. space->get_broadphase()->getOverlappingPairCache()->cleanProxyFromPairs(bt_collision_object->getBroadphaseHandle(), space->get_dispatcher());
  134. }
  135. }
  136. void CollisionObjectBullet::remove_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject) {
  137. exceptions.erase(p_ignoreCollisionObject->get_self());
  138. if (!bt_collision_object) {
  139. return;
  140. }
  141. bt_collision_object->setIgnoreCollisionCheck(p_ignoreCollisionObject->bt_collision_object, false);
  142. if (space) {
  143. space->get_broadphase()->getOverlappingPairCache()->cleanProxyFromPairs(bt_collision_object->getBroadphaseHandle(), space->get_dispatcher());
  144. }
  145. }
  146. bool CollisionObjectBullet::has_collision_exception(const CollisionObjectBullet *p_otherCollisionObject) const {
  147. return exceptions.has(p_otherCollisionObject->get_self());
  148. }
  149. void CollisionObjectBullet::set_collision_enabled(bool p_enabled) {
  150. collisionsEnabled = p_enabled;
  151. if (!bt_collision_object) {
  152. return;
  153. }
  154. if (collisionsEnabled) {
  155. bt_collision_object->setCollisionFlags(bt_collision_object->getCollisionFlags() & (~btCollisionObject::CF_NO_CONTACT_RESPONSE));
  156. } else {
  157. bt_collision_object->setCollisionFlags(bt_collision_object->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
  158. }
  159. }
  160. bool CollisionObjectBullet::is_collisions_response_enabled() {
  161. return collisionsEnabled;
  162. }
  163. void CollisionObjectBullet::notify_new_overlap(AreaBullet *p_area) {
  164. areasOverlapped.push_back(p_area);
  165. }
  166. void CollisionObjectBullet::on_exit_area(AreaBullet *p_area) {
  167. areasOverlapped.erase(p_area);
  168. }
  169. void CollisionObjectBullet::set_godot_object_flags(int flags) {
  170. bt_collision_object->setUserIndex2(flags);
  171. }
  172. int CollisionObjectBullet::get_godot_object_flags() const {
  173. return bt_collision_object->getUserIndex2();
  174. }
  175. void CollisionObjectBullet::set_transform(const Transform &p_global_transform) {
  176. set_body_scale(p_global_transform.basis.get_scale_abs());
  177. btTransform bt_transform;
  178. G_TO_B(p_global_transform, bt_transform);
  179. UNSCALE_BT_BASIS(bt_transform);
  180. set_transform__bullet(bt_transform);
  181. }
  182. Transform CollisionObjectBullet::get_transform() const {
  183. Transform t;
  184. B_TO_G(get_transform__bullet(), t);
  185. t.basis.scale(body_scale);
  186. return t;
  187. }
  188. void CollisionObjectBullet::set_transform__bullet(const btTransform &p_global_transform) {
  189. bt_collision_object->setWorldTransform(p_global_transform);
  190. notify_transform_changed();
  191. }
  192. const btTransform &CollisionObjectBullet::get_transform__bullet() const {
  193. return bt_collision_object->getWorldTransform();
  194. }
  195. void CollisionObjectBullet::notify_transform_changed() {
  196. isTransformChanged = true;
  197. }
  198. RigidCollisionObjectBullet::RigidCollisionObjectBullet(Type p_type) :
  199. CollisionObjectBullet(p_type),
  200. mainShape(nullptr) {
  201. }
  202. RigidCollisionObjectBullet::~RigidCollisionObjectBullet() {
  203. remove_all_shapes(true, true);
  204. if (mainShape && mainShape->isCompound()) {
  205. bulletdelete(mainShape);
  206. }
  207. }
  208. void RigidCollisionObjectBullet::add_shape(ShapeBullet *p_shape, const Transform &p_transform, bool p_disabled) {
  209. shapes.push_back(ShapeWrapper(p_shape, p_transform, !p_disabled));
  210. p_shape->add_owner(this);
  211. reload_shapes();
  212. }
  213. void RigidCollisionObjectBullet::set_shape(int p_index, ShapeBullet *p_shape) {
  214. ShapeWrapper &shp = shapes.write[p_index];
  215. shp.shape->remove_owner(this);
  216. p_shape->add_owner(this);
  217. shp.shape = p_shape;
  218. reload_shapes();
  219. }
  220. int RigidCollisionObjectBullet::get_shape_count() const {
  221. return shapes.size();
  222. }
  223. ShapeBullet *RigidCollisionObjectBullet::get_shape(int p_index) const {
  224. return shapes[p_index].shape;
  225. }
  226. btCollisionShape *RigidCollisionObjectBullet::get_bt_shape(int p_index) const {
  227. return shapes[p_index].bt_shape;
  228. }
  229. int RigidCollisionObjectBullet::find_shape(ShapeBullet *p_shape) const {
  230. const int size = shapes.size();
  231. for (int i = 0; i < size; ++i) {
  232. if (shapes[i].shape == p_shape) {
  233. return i;
  234. }
  235. }
  236. return -1;
  237. }
  238. void RigidCollisionObjectBullet::remove_shape_full(ShapeBullet *p_shape) {
  239. // Remove the shape, all the times it appears
  240. // Reverse order required for delete.
  241. for (int i = shapes.size() - 1; 0 <= i; --i) {
  242. if (p_shape == shapes[i].shape) {
  243. internal_shape_destroy(i);
  244. shapes.remove(i);
  245. }
  246. }
  247. reload_shapes();
  248. }
  249. void RigidCollisionObjectBullet::remove_shape_full(int p_index) {
  250. ERR_FAIL_INDEX(p_index, get_shape_count());
  251. internal_shape_destroy(p_index);
  252. shapes.remove(p_index);
  253. reload_shapes();
  254. }
  255. void RigidCollisionObjectBullet::remove_all_shapes(bool p_permanentlyFromThisBody, bool p_force_not_reload) {
  256. // Reverse order required for delete.
  257. for (int i = shapes.size() - 1; 0 <= i; --i) {
  258. internal_shape_destroy(i, p_permanentlyFromThisBody);
  259. }
  260. shapes.clear();
  261. if (!p_force_not_reload) {
  262. reload_shapes();
  263. }
  264. }
  265. void RigidCollisionObjectBullet::set_shape_transform(int p_index, const Transform &p_transform) {
  266. ERR_FAIL_INDEX(p_index, get_shape_count());
  267. shapes.write[p_index].set_transform(p_transform);
  268. shape_changed(p_index);
  269. }
  270. const btTransform &RigidCollisionObjectBullet::get_bt_shape_transform(int p_index) const {
  271. return shapes[p_index].transform;
  272. }
  273. Transform RigidCollisionObjectBullet::get_shape_transform(int p_index) const {
  274. Transform trs;
  275. B_TO_G(shapes[p_index].transform, trs);
  276. return trs;
  277. }
  278. void RigidCollisionObjectBullet::set_shape_disabled(int p_index, bool p_disabled) {
  279. if (shapes[p_index].active != p_disabled) {
  280. return;
  281. }
  282. shapes.write[p_index].active = !p_disabled;
  283. shape_changed(p_index);
  284. }
  285. bool RigidCollisionObjectBullet::is_shape_disabled(int p_index) {
  286. return !shapes[p_index].active;
  287. }
  288. void RigidCollisionObjectBullet::shape_changed(int p_shape_index) {
  289. ShapeWrapper &shp = shapes.write[p_shape_index];
  290. if (shp.bt_shape == mainShape) {
  291. mainShape = nullptr;
  292. }
  293. bulletdelete(shp.bt_shape);
  294. reload_shapes();
  295. }
  296. void RigidCollisionObjectBullet::reload_shapes() {
  297. if (mainShape && mainShape->isCompound()) {
  298. // Destroy compound
  299. bulletdelete(mainShape);
  300. }
  301. mainShape = nullptr;
  302. ShapeWrapper *shpWrapper;
  303. const int shape_count = shapes.size();
  304. // Reset shape if required
  305. if (force_shape_reset) {
  306. for (int i(0); i < shape_count; ++i) {
  307. shpWrapper = &shapes.write[i];
  308. bulletdelete(shpWrapper->bt_shape);
  309. }
  310. force_shape_reset = false;
  311. }
  312. const btVector3 body_scale(get_bt_body_scale());
  313. // Try to optimize by not using compound
  314. if (1 == shape_count) {
  315. shpWrapper = &shapes.write[0];
  316. btTransform transform = shpWrapper->get_adjusted_transform();
  317. if (transform.getOrigin().isZero() && transform.getBasis() == transform.getBasis().getIdentity()) {
  318. shpWrapper->claim_bt_shape(body_scale);
  319. mainShape = shpWrapper->bt_shape;
  320. main_shape_changed();
  321. return;
  322. }
  323. }
  324. // Optimization not possible use a compound shape
  325. btCompoundShape *compoundShape = bulletnew(btCompoundShape(enableDynamicAabbTree, shape_count));
  326. for (int i(0); i < shape_count; ++i) {
  327. shpWrapper = &shapes.write[i];
  328. shpWrapper->claim_bt_shape(body_scale);
  329. btTransform scaled_shape_transform(shpWrapper->get_adjusted_transform());
  330. scaled_shape_transform.getOrigin() *= body_scale;
  331. compoundShape->addChildShape(scaled_shape_transform, shpWrapper->bt_shape);
  332. }
  333. compoundShape->recalculateLocalAabb();
  334. mainShape = compoundShape;
  335. main_shape_changed();
  336. }
  337. void RigidCollisionObjectBullet::body_scale_changed() {
  338. CollisionObjectBullet::body_scale_changed();
  339. reload_shapes();
  340. }
  341. void RigidCollisionObjectBullet::internal_shape_destroy(int p_index, bool p_permanentlyFromThisBody) {
  342. ShapeWrapper &shp = shapes.write[p_index];
  343. shp.shape->remove_owner(this, p_permanentlyFromThisBody);
  344. if (shp.bt_shape == mainShape) {
  345. mainShape = nullptr;
  346. }
  347. bulletdelete(shp.bt_shape);
  348. }