area_bullet.cpp 9.9 KB

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