2
0

collision_object_bullet.cpp 12 KB

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