collision_object_bullet.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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. #define enableDynamicAabbTree true
  42. #define initialChildCapacity 1
  43. CollisionObjectBullet::ShapeWrapper::~ShapeWrapper() {}
  44. void CollisionObjectBullet::ShapeWrapper::set_transform(const Transform &p_transform) {
  45. G_TO_B(p_transform.get_basis().get_scale_abs(), scale);
  46. G_TO_B(p_transform, transform);
  47. UNSCALE_BT_BASIS(transform);
  48. }
  49. void CollisionObjectBullet::ShapeWrapper::set_transform(const btTransform &p_transform) {
  50. transform = p_transform;
  51. }
  52. void CollisionObjectBullet::ShapeWrapper::claim_bt_shape(const btVector3 &body_scale) {
  53. if (!bt_shape) {
  54. bt_shape = shape->create_bt_shape(scale * body_scale);
  55. }
  56. }
  57. CollisionObjectBullet::CollisionObjectBullet(Type p_type) :
  58. RIDBullet(),
  59. space(NULL),
  60. type(p_type),
  61. collisionsEnabled(true),
  62. m_isStatic(false),
  63. bt_collision_object(NULL),
  64. body_scale(1., 1., 1.),
  65. force_shape_reset(false) {}
  66. CollisionObjectBullet::~CollisionObjectBullet() {
  67. // Remove all overlapping, notify is not required since godot take care of it
  68. for (int i = areasOverlapped.size() - 1; 0 <= i; --i) {
  69. areasOverlapped[i]->remove_overlap(this, /*Notify*/ false);
  70. }
  71. destroyBulletCollisionObject();
  72. }
  73. bool equal(real_t first, real_t second) {
  74. return Math::abs(first - second) <= 0.001f;
  75. }
  76. void CollisionObjectBullet::set_body_scale(const Vector3 &p_new_scale) {
  77. 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])) {
  78. body_scale = p_new_scale;
  79. on_body_scale_changed();
  80. }
  81. }
  82. btVector3 CollisionObjectBullet::get_bt_body_scale() const {
  83. btVector3 s;
  84. G_TO_B(body_scale, s);
  85. return s;
  86. }
  87. void CollisionObjectBullet::on_body_scale_changed() {
  88. force_shape_reset = true;
  89. }
  90. void CollisionObjectBullet::destroyBulletCollisionObject() {
  91. bulletdelete(bt_collision_object);
  92. }
  93. void CollisionObjectBullet::setupBulletCollisionObject(btCollisionObject *p_collisionObject) {
  94. bt_collision_object = p_collisionObject;
  95. bt_collision_object->setUserPointer(this);
  96. bt_collision_object->setUserIndex(type);
  97. // Force the enabling of collision and avoid problems
  98. set_collision_enabled(collisionsEnabled);
  99. p_collisionObject->setCollisionFlags(p_collisionObject->getCollisionFlags() | btCollisionObject::CF_CUSTOM_MATERIAL_CALLBACK);
  100. }
  101. void CollisionObjectBullet::add_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject) {
  102. exceptions.insert(p_ignoreCollisionObject->get_self());
  103. if (!bt_collision_object)
  104. return;
  105. bt_collision_object->setIgnoreCollisionCheck(p_ignoreCollisionObject->bt_collision_object, true);
  106. if (space)
  107. space->get_broadphase()->getOverlappingPairCache()->cleanProxyFromPairs(bt_collision_object->getBroadphaseHandle(), space->get_dispatcher());
  108. }
  109. void CollisionObjectBullet::remove_collision_exception(const CollisionObjectBullet *p_ignoreCollisionObject) {
  110. exceptions.erase(p_ignoreCollisionObject->get_self());
  111. bt_collision_object->setIgnoreCollisionCheck(p_ignoreCollisionObject->bt_collision_object, false);
  112. if (space)
  113. space->get_broadphase()->getOverlappingPairCache()->cleanProxyFromPairs(bt_collision_object->getBroadphaseHandle(), space->get_dispatcher());
  114. }
  115. bool CollisionObjectBullet::has_collision_exception(const CollisionObjectBullet *p_otherCollisionObject) const {
  116. return !bt_collision_object->checkCollideWith(p_otherCollisionObject->bt_collision_object);
  117. }
  118. void CollisionObjectBullet::set_collision_enabled(bool p_enabled) {
  119. collisionsEnabled = p_enabled;
  120. if (collisionsEnabled) {
  121. bt_collision_object->setCollisionFlags(bt_collision_object->getCollisionFlags() & (~btCollisionObject::CF_NO_CONTACT_RESPONSE));
  122. } else {
  123. bt_collision_object->setCollisionFlags(bt_collision_object->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
  124. }
  125. }
  126. bool CollisionObjectBullet::is_collisions_response_enabled() {
  127. return collisionsEnabled;
  128. }
  129. void CollisionObjectBullet::notify_new_overlap(AreaBullet *p_area) {
  130. areasOverlapped.push_back(p_area);
  131. }
  132. void CollisionObjectBullet::on_exit_area(AreaBullet *p_area) {
  133. areasOverlapped.erase(p_area);
  134. }
  135. void CollisionObjectBullet::set_godot_object_flags(int flags) {
  136. bt_collision_object->setUserIndex2(flags);
  137. }
  138. int CollisionObjectBullet::get_godot_object_flags() const {
  139. return bt_collision_object->getUserIndex2();
  140. }
  141. void CollisionObjectBullet::set_transform(const Transform &p_global_transform) {
  142. set_body_scale(p_global_transform.basis.get_scale_abs());
  143. btTransform bt_transform;
  144. G_TO_B(p_global_transform, bt_transform);
  145. UNSCALE_BT_BASIS(bt_transform);
  146. set_transform__bullet(bt_transform);
  147. }
  148. Transform CollisionObjectBullet::get_transform() const {
  149. Transform t;
  150. B_TO_G(get_transform__bullet(), t);
  151. t.basis.scale(body_scale);
  152. return t;
  153. }
  154. void CollisionObjectBullet::set_transform__bullet(const btTransform &p_global_transform) {
  155. bt_collision_object->setWorldTransform(p_global_transform);
  156. }
  157. const btTransform &CollisionObjectBullet::get_transform__bullet() const {
  158. return bt_collision_object->getWorldTransform();
  159. }
  160. RigidCollisionObjectBullet::RigidCollisionObjectBullet(Type p_type) :
  161. CollisionObjectBullet(p_type),
  162. mainShape(NULL) {
  163. }
  164. RigidCollisionObjectBullet::~RigidCollisionObjectBullet() {
  165. remove_all_shapes(true);
  166. if (mainShape && mainShape->isCompound()) {
  167. bulletdelete(mainShape);
  168. }
  169. }
  170. /* Not used
  171. void RigidCollisionObjectBullet::_internal_replaceShape(btCollisionShape *p_old_shape, btCollisionShape *p_new_shape) {
  172. bool at_least_one_was_changed = false;
  173. btTransform old_transf;
  174. // Inverse because I need remove the shapes
  175. // Fetch all shapes to be sure to remove all shapes
  176. for (int i = compoundShape->getNumChildShapes() - 1; 0 <= i; --i) {
  177. if (compoundShape->getChildShape(i) == p_old_shape) {
  178. old_transf = compoundShape->getChildTransform(i);
  179. compoundShape->removeChildShapeByIndex(i);
  180. compoundShape->addChildShape(old_transf, p_new_shape);
  181. at_least_one_was_changed = true;
  182. }
  183. }
  184. if (at_least_one_was_changed) {
  185. on_shapes_changed();
  186. }
  187. }*/
  188. void RigidCollisionObjectBullet::add_shape(ShapeBullet *p_shape, const Transform &p_transform) {
  189. shapes.push_back(ShapeWrapper(p_shape, p_transform, true));
  190. p_shape->add_owner(this);
  191. on_shapes_changed();
  192. }
  193. void RigidCollisionObjectBullet::set_shape(int p_index, ShapeBullet *p_shape) {
  194. ShapeWrapper &shp = shapes.write[p_index];
  195. shp.shape->remove_owner(this);
  196. p_shape->add_owner(this);
  197. shp.shape = p_shape;
  198. on_shapes_changed();
  199. }
  200. void RigidCollisionObjectBullet::set_shape_transform(int p_index, const Transform &p_transform) {
  201. ERR_FAIL_INDEX(p_index, get_shape_count());
  202. shapes.write[p_index].set_transform(p_transform);
  203. on_shape_changed(shapes.write[p_index].shape);
  204. }
  205. void RigidCollisionObjectBullet::remove_shape(ShapeBullet *p_shape) {
  206. // Remove the shape, all the times it appears
  207. // Reverse order required for delete.
  208. for (int i = shapes.size() - 1; 0 <= i; --i) {
  209. if (p_shape == shapes[i].shape) {
  210. internal_shape_destroy(i);
  211. shapes.remove(i);
  212. }
  213. }
  214. on_shapes_changed();
  215. }
  216. void RigidCollisionObjectBullet::remove_shape(int p_index) {
  217. ERR_FAIL_INDEX(p_index, get_shape_count());
  218. internal_shape_destroy(p_index);
  219. shapes.remove(p_index);
  220. on_shapes_changed();
  221. }
  222. void RigidCollisionObjectBullet::remove_all_shapes(bool p_permanentlyFromThisBody) {
  223. // Reverse order required for delete.
  224. for (int i = shapes.size() - 1; 0 <= i; --i) {
  225. internal_shape_destroy(i, p_permanentlyFromThisBody);
  226. }
  227. shapes.clear();
  228. on_shapes_changed();
  229. }
  230. int RigidCollisionObjectBullet::get_shape_count() const {
  231. return shapes.size();
  232. }
  233. ShapeBullet *RigidCollisionObjectBullet::get_shape(int p_index) const {
  234. return shapes[p_index].shape;
  235. }
  236. btCollisionShape *RigidCollisionObjectBullet::get_bt_shape(int p_index) const {
  237. return shapes[p_index].bt_shape;
  238. }
  239. const btTransform &RigidCollisionObjectBullet::get_bt_shape_transform(int p_index) const {
  240. return shapes[p_index].transform;
  241. }
  242. Transform RigidCollisionObjectBullet::get_shape_transform(int p_index) const {
  243. Transform trs;
  244. B_TO_G(shapes[p_index].transform, trs);
  245. return trs;
  246. }
  247. void RigidCollisionObjectBullet::on_shape_changed(const ShapeBullet *const p_shape) {
  248. const int size = shapes.size();
  249. for (int i = 0; i < size; ++i) {
  250. if (shapes[i].shape == p_shape) {
  251. bulletdelete(shapes.write[i].bt_shape);
  252. }
  253. }
  254. on_shapes_changed();
  255. }
  256. void RigidCollisionObjectBullet::on_shapes_changed() {
  257. if (mainShape && mainShape->isCompound()) {
  258. bulletdelete(mainShape);
  259. }
  260. mainShape = NULL;
  261. ShapeWrapper *shpWrapper;
  262. const int shape_count = shapes.size();
  263. // Reset shape if required
  264. if (force_shape_reset) {
  265. for (int i(0); i < shape_count; ++i) {
  266. shpWrapper = &shapes.write[i];
  267. bulletdelete(shpWrapper->bt_shape);
  268. }
  269. force_shape_reset = false;
  270. }
  271. btVector3 body_scale(get_bt_body_scale());
  272. if (!shape_count)
  273. return;
  274. // Try to optimize by not using compound
  275. if (1 == shape_count) {
  276. shpWrapper = &shapes.write[0];
  277. if (shpWrapper->active && shpWrapper->transform.getOrigin().isZero() && shpWrapper->transform.getBasis() == shpWrapper->transform.getBasis().getIdentity()) {
  278. shpWrapper->claim_bt_shape(body_scale);
  279. mainShape = shpWrapper->bt_shape;
  280. main_shape_resetted();
  281. return;
  282. }
  283. }
  284. btCompoundShape *compoundShape = bulletnew(btCompoundShape(enableDynamicAabbTree, initialChildCapacity));
  285. // Insert all shapes into compound
  286. for (int i(0); i < shape_count; ++i) {
  287. shpWrapper = &shapes.write[i];
  288. if (shpWrapper->active) {
  289. shpWrapper->claim_bt_shape(body_scale);
  290. btTransform scaled_shape_transform(shpWrapper->transform);
  291. scaled_shape_transform.getOrigin() *= body_scale;
  292. compoundShape->addChildShape(scaled_shape_transform, shpWrapper->bt_shape);
  293. } else {
  294. compoundShape->addChildShape(btTransform(), BulletPhysicsServer::get_empty_shape());
  295. }
  296. }
  297. compoundShape->recalculateLocalAabb();
  298. mainShape = compoundShape;
  299. main_shape_resetted();
  300. }
  301. void RigidCollisionObjectBullet::set_shape_disabled(int p_index, bool p_disabled) {
  302. shapes.write[p_index].active = !p_disabled;
  303. on_shapes_changed();
  304. }
  305. bool RigidCollisionObjectBullet::is_shape_disabled(int p_index) {
  306. return !shapes[p_index].active;
  307. }
  308. void RigidCollisionObjectBullet::on_body_scale_changed() {
  309. CollisionObjectBullet::on_body_scale_changed();
  310. on_shapes_changed();
  311. }
  312. void RigidCollisionObjectBullet::internal_shape_destroy(int p_index, bool p_permanentlyFromThisBody) {
  313. ShapeWrapper &shp = shapes.write[p_index];
  314. shp.shape->remove_owner(this, p_permanentlyFromThisBody);
  315. bulletdelete(shp.bt_shape);
  316. }