area_bullet.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*************************************************************************/
  2. /* area_bullet.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 "area_bullet.h"
  31. #include "bullet_physics_server.h"
  32. #include "bullet_types_converter.h"
  33. #include "bullet_utilities.h"
  34. #include "collision_object_bullet.h"
  35. #include "space_bullet.h"
  36. #include <BulletCollision/CollisionDispatch/btGhostObject.h>
  37. #include <btBulletCollisionCommon.h>
  38. /**
  39. @author AndreaCatania
  40. */
  41. AreaBullet::AreaBullet() :
  42. RigidCollisionObjectBullet(CollisionObjectBullet::TYPE_AREA) {
  43. btGhost = bulletnew(btGhostObject);
  44. reload_shapes();
  45. setupBulletCollisionObject(btGhost);
  46. /// Collision objects with a callback still have collision response with dynamic rigid bodies.
  47. /// In order to use collision objects as trigger, you have to disable the collision response.
  48. set_collision_enabled(false);
  49. for (int i = 0; i < 5; ++i) {
  50. call_event_res_ptr[i] = &call_event_res[i];
  51. }
  52. }
  53. AreaBullet::~AreaBullet() {
  54. // signal are handled by godot, so just clear without notify
  55. for (int i = overlappingObjects.size() - 1; 0 <= i; --i) {
  56. overlappingObjects[i].object->on_exit_area(this);
  57. }
  58. }
  59. void AreaBullet::dispatch_callbacks() {
  60. if (!isScratched) {
  61. return;
  62. }
  63. isScratched = false;
  64. // Reverse order because I've to remove EXIT objects
  65. for (int i = overlappingObjects.size() - 1; 0 <= i; --i) {
  66. OverlappingObjectData &otherObj = overlappingObjects.write[i];
  67. switch (otherObj.state) {
  68. case OVERLAP_STATE_ENTER:
  69. otherObj.state = OVERLAP_STATE_INSIDE;
  70. call_event(otherObj.object, PhysicsServer3D::AREA_BODY_ADDED);
  71. otherObj.object->on_enter_area(this);
  72. break;
  73. case OVERLAP_STATE_EXIT:
  74. call_event(otherObj.object, PhysicsServer3D::AREA_BODY_REMOVED);
  75. otherObj.object->on_exit_area(this);
  76. overlappingObjects.remove(i); // Remove after callback
  77. break;
  78. case OVERLAP_STATE_DIRTY:
  79. case OVERLAP_STATE_INSIDE:
  80. break;
  81. }
  82. }
  83. }
  84. void AreaBullet::call_event(CollisionObjectBullet *p_otherObject, PhysicsServer3D::AreaBodyStatus p_status) {
  85. InOutEventCallback &event = eventsCallbacks[static_cast<int>(p_otherObject->getType())];
  86. Object *areaGodoObject = ObjectDB::get_instance(event.event_callback_id);
  87. if (!areaGodoObject) {
  88. event.event_callback_id = ObjectID();
  89. return;
  90. }
  91. call_event_res[0] = p_status;
  92. call_event_res[1] = p_otherObject->get_self(); // Other body
  93. call_event_res[2] = p_otherObject->get_instance_id(); // instance ID
  94. call_event_res[3] = 0; // other_body_shape ID
  95. call_event_res[4] = 0; // self_shape ID
  96. Callable::CallError outResp;
  97. areaGodoObject->call(event.event_callback_method, (const Variant **)call_event_res_ptr, 5, outResp);
  98. }
  99. void AreaBullet::scratch() {
  100. if (isScratched) {
  101. return;
  102. }
  103. isScratched = true;
  104. }
  105. void AreaBullet::clear_overlaps(bool p_notify) {
  106. for (int i = overlappingObjects.size() - 1; 0 <= i; --i) {
  107. if (p_notify) {
  108. call_event(overlappingObjects[i].object, PhysicsServer3D::AREA_BODY_REMOVED);
  109. }
  110. overlappingObjects[i].object->on_exit_area(this);
  111. }
  112. overlappingObjects.clear();
  113. }
  114. void AreaBullet::remove_overlap(CollisionObjectBullet *p_object, bool p_notify) {
  115. for (int i = overlappingObjects.size() - 1; 0 <= i; --i) {
  116. if (overlappingObjects[i].object == p_object) {
  117. if (p_notify) {
  118. call_event(overlappingObjects[i].object, PhysicsServer3D::AREA_BODY_REMOVED);
  119. }
  120. overlappingObjects[i].object->on_exit_area(this);
  121. overlappingObjects.remove(i);
  122. break;
  123. }
  124. }
  125. }
  126. int AreaBullet::find_overlapping_object(CollisionObjectBullet *p_colObj) {
  127. const int size = overlappingObjects.size();
  128. for (int i = 0; i < size; ++i) {
  129. if (overlappingObjects[i].object == p_colObj) {
  130. return i;
  131. }
  132. }
  133. return -1;
  134. }
  135. void AreaBullet::set_monitorable(bool p_monitorable) {
  136. monitorable = p_monitorable;
  137. }
  138. bool AreaBullet::is_monitoring() const {
  139. return get_godot_object_flags() & GOF_IS_MONITORING_AREA;
  140. }
  141. void AreaBullet::main_shape_changed() {
  142. CRASH_COND(!get_main_shape());
  143. btGhost->setCollisionShape(get_main_shape());
  144. }
  145. void AreaBullet::reload_body() {
  146. if (space) {
  147. space->remove_area(this);
  148. space->add_area(this);
  149. }
  150. }
  151. void AreaBullet::set_space(SpaceBullet *p_space) {
  152. // Clear the old space if there is one
  153. if (space) {
  154. clear_overlaps(false);
  155. isScratched = false;
  156. // Remove this object form the physics world
  157. space->remove_area(this);
  158. }
  159. space = p_space;
  160. if (space) {
  161. space->add_area(this);
  162. }
  163. }
  164. void AreaBullet::on_collision_filters_change() {
  165. if (space) {
  166. space->reload_collision_filters(this);
  167. }
  168. }
  169. void AreaBullet::add_overlap(CollisionObjectBullet *p_otherObject) {
  170. scratch();
  171. overlappingObjects.push_back(OverlappingObjectData(p_otherObject, OVERLAP_STATE_ENTER));
  172. p_otherObject->notify_new_overlap(this);
  173. }
  174. void AreaBullet::put_overlap_as_exit(int p_index) {
  175. scratch();
  176. overlappingObjects.write[p_index].state = OVERLAP_STATE_EXIT;
  177. }
  178. void AreaBullet::put_overlap_as_inside(int p_index) {
  179. // This check is required to be sure this body was inside
  180. if (OVERLAP_STATE_DIRTY == overlappingObjects[p_index].state) {
  181. overlappingObjects.write[p_index].state = OVERLAP_STATE_INSIDE;
  182. }
  183. }
  184. void AreaBullet::set_param(PhysicsServer3D::AreaParameter p_param, const Variant &p_value) {
  185. switch (p_param) {
  186. case PhysicsServer3D::AREA_PARAM_GRAVITY:
  187. set_spOv_gravityMag(p_value);
  188. break;
  189. case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR:
  190. set_spOv_gravityVec(p_value);
  191. break;
  192. case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP:
  193. set_spOv_linearDump(p_value);
  194. break;
  195. case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP:
  196. set_spOv_angularDump(p_value);
  197. break;
  198. case PhysicsServer3D::AREA_PARAM_PRIORITY:
  199. set_spOv_priority(p_value);
  200. break;
  201. case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT:
  202. set_spOv_gravityPoint(p_value);
  203. break;
  204. case PhysicsServer3D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
  205. set_spOv_gravityPointDistanceScale(p_value);
  206. break;
  207. case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
  208. set_spOv_gravityPointAttenuation(p_value);
  209. break;
  210. default:
  211. WARN_PRINT("Area doesn't support this parameter in the Bullet backend: " + itos(p_param));
  212. }
  213. }
  214. Variant AreaBullet::get_param(PhysicsServer3D::AreaParameter p_param) const {
  215. switch (p_param) {
  216. case PhysicsServer3D::AREA_PARAM_GRAVITY:
  217. return spOv_gravityMag;
  218. case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR:
  219. return spOv_gravityVec;
  220. case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP:
  221. return spOv_linearDump;
  222. case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP:
  223. return spOv_angularDump;
  224. case PhysicsServer3D::AREA_PARAM_PRIORITY:
  225. return spOv_priority;
  226. case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT:
  227. return spOv_gravityPoint;
  228. case PhysicsServer3D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
  229. return spOv_gravityPointDistanceScale;
  230. case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
  231. return spOv_gravityPointAttenuation;
  232. default:
  233. WARN_PRINT("Area doesn't support this parameter in the Bullet backend: " + itos(p_param));
  234. return Variant();
  235. }
  236. }
  237. void AreaBullet::set_event_callback(Type p_callbackObjectType, ObjectID p_id, const StringName &p_method) {
  238. InOutEventCallback &ev = eventsCallbacks[static_cast<int>(p_callbackObjectType)];
  239. ev.event_callback_id = p_id;
  240. ev.event_callback_method = p_method;
  241. /// Set if monitoring
  242. if (eventsCallbacks[0].event_callback_id.is_valid() || eventsCallbacks[1].event_callback_id.is_valid()) {
  243. set_godot_object_flags(get_godot_object_flags() | GOF_IS_MONITORING_AREA);
  244. } else {
  245. set_godot_object_flags(get_godot_object_flags() & (~GOF_IS_MONITORING_AREA));
  246. }
  247. }
  248. bool AreaBullet::has_event_callback(Type p_callbackObjectType) {
  249. return eventsCallbacks[static_cast<int>(p_callbackObjectType)].event_callback_id.is_valid();
  250. }
  251. void AreaBullet::on_enter_area(AreaBullet *p_area) {
  252. }
  253. void AreaBullet::on_exit_area(AreaBullet *p_area) {
  254. CollisionObjectBullet::on_exit_area(p_area);
  255. }