area_bullet.cpp 9.7 KB

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