area_bullet.cpp 10.0 KB

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