area_bullet.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /**************************************************************************/
  2. /* area_bullet.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 = 0; i < overlapping_shapes.size(); i++) {
  67. overlapping_shapes[i].other_object->on_exit_area(this);
  68. }
  69. }
  70. void AreaBullet::dispatch_callbacks() {
  71. if (!isScratched) {
  72. return;
  73. }
  74. isScratched = false;
  75. // Reverse order so items can be removed.
  76. for (int i = overlapping_shapes.size() - 1; i >= 0; i--) {
  77. OverlappingShapeData &overlapping_shape = overlapping_shapes.write[i];
  78. switch (overlapping_shape.state) {
  79. case OVERLAP_STATE_ENTER:
  80. overlapping_shape.state = OVERLAP_STATE_INSIDE;
  81. call_event(overlapping_shape, PhysicsServer::AREA_BODY_ADDED);
  82. if (_overlapping_shape_count(overlapping_shape.other_object) == 1) {
  83. // This object's first shape being added.
  84. overlapping_shape.other_object->on_enter_area(this);
  85. }
  86. break;
  87. case OVERLAP_STATE_EXIT:
  88. call_event(overlapping_shape, PhysicsServer::AREA_BODY_REMOVED);
  89. if (_overlapping_shape_count(overlapping_shape.other_object) == 1) {
  90. // This object's last shape being removed.
  91. overlapping_shape.other_object->on_exit_area(this);
  92. }
  93. overlapping_shapes.remove(i); // Remove after callback
  94. break;
  95. case OVERLAP_STATE_INSIDE: {
  96. if (overlapping_shape.other_object->getType() == TYPE_RIGID_BODY) {
  97. RigidBodyBullet *body = static_cast<RigidBodyBullet *>(overlapping_shape.other_object);
  98. body->scratch_space_override_modificator();
  99. }
  100. break;
  101. }
  102. case OVERLAP_STATE_DIRTY:
  103. break;
  104. }
  105. }
  106. }
  107. void AreaBullet::call_event(const OverlappingShapeData &p_overlapping_shape, PhysicsServer::AreaBodyStatus p_status) {
  108. InOutEventCallback &event = eventsCallbacks[static_cast<int>(p_overlapping_shape.other_object->getType())];
  109. Object *areaGodoObject = ObjectDB::get_instance(event.event_callback_id);
  110. if (!areaGodoObject) {
  111. event.event_callback_id = 0;
  112. return;
  113. }
  114. call_event_res[0] = p_status;
  115. call_event_res[1] = p_overlapping_shape.other_object->get_self(); // RID
  116. call_event_res[2] = p_overlapping_shape.other_object->get_instance_id(); // Object ID
  117. call_event_res[3] = p_overlapping_shape.other_shape_id; // Other object's shape ID
  118. call_event_res[4] = p_overlapping_shape.our_shape_id; // This area's shape ID
  119. Variant::CallError outResp;
  120. areaGodoObject->call(event.event_callback_method, (const Variant **)call_event_res_ptr, 5, outResp);
  121. if (outResp.error != Variant::CallError::CALL_OK) {
  122. ERR_PRINT_ONCE("Error calling event callback method " + Variant::get_call_error_text(areaGodoObject, event.event_callback_method, (const Variant **)call_event_res_ptr, 5, outResp));
  123. }
  124. }
  125. int AreaBullet::_overlapping_shape_count(CollisionObjectBullet *p_other_object) {
  126. int count = 0;
  127. for (int i = 0; i < overlapping_shapes.size(); i++) {
  128. if (overlapping_shapes[i].other_object == p_other_object) {
  129. count++;
  130. }
  131. }
  132. return count;
  133. }
  134. int AreaBullet::_find_overlapping_shape(CollisionObjectBullet *p_other_object, uint32_t p_other_shape_id, uint32_t p_our_shape_id) {
  135. for (int i = 0; i < overlapping_shapes.size(); i++) {
  136. const OverlappingShapeData &overlapping_shape = overlapping_shapes[i];
  137. if (overlapping_shape.other_object == p_other_object && overlapping_shape.other_shape_id == p_other_shape_id && overlapping_shape.our_shape_id == p_our_shape_id) {
  138. return i;
  139. }
  140. }
  141. return -1;
  142. }
  143. void AreaBullet::mark_all_overlaps_dirty() {
  144. OverlappingShapeData *overlapping_shapes_w = overlapping_shapes.ptrw();
  145. for (int i = 0; i < overlapping_shapes.size(); i++) {
  146. // Don't overwrite OVERLAP_STATE_ENTER state.
  147. if (overlapping_shapes_w[i].state != OVERLAP_STATE_ENTER) {
  148. overlapping_shapes_w[i].state = OVERLAP_STATE_DIRTY;
  149. }
  150. }
  151. }
  152. void AreaBullet::mark_object_overlaps_inside(CollisionObjectBullet *p_other_object) {
  153. OverlappingShapeData *overlapping_shapes_w = overlapping_shapes.ptrw();
  154. for (int i = 0; i < overlapping_shapes.size(); i++) {
  155. if (overlapping_shapes_w[i].other_object == p_other_object && overlapping_shapes_w[i].state == OVERLAP_STATE_DIRTY) {
  156. overlapping_shapes_w[i].state = OVERLAP_STATE_INSIDE;
  157. }
  158. }
  159. }
  160. void AreaBullet::set_overlap(CollisionObjectBullet *p_other_object, uint32_t p_other_shape_id, uint32_t p_our_shape_id) {
  161. int i = _find_overlapping_shape(p_other_object, p_other_shape_id, p_our_shape_id);
  162. if (i == -1) { // Not found, create new one.
  163. OverlappingShapeData overlapping_shape(p_other_object, OVERLAP_STATE_ENTER, p_other_shape_id, p_our_shape_id);
  164. overlapping_shapes.push_back(overlapping_shape);
  165. p_other_object->notify_new_overlap(this);
  166. isScratched = true;
  167. } else {
  168. overlapping_shapes.ptrw()[i].state = OVERLAP_STATE_INSIDE;
  169. }
  170. }
  171. void AreaBullet::mark_all_dirty_overlaps_as_exit() {
  172. OverlappingShapeData *overlapping_shapes_w = overlapping_shapes.ptrw();
  173. for (int i = 0; i < overlapping_shapes.size(); i++) {
  174. if (overlapping_shapes[i].state == OVERLAP_STATE_DIRTY) {
  175. overlapping_shapes_w[i].state = OVERLAP_STATE_EXIT;
  176. isScratched = true;
  177. }
  178. }
  179. }
  180. void AreaBullet::remove_object_overlaps(CollisionObjectBullet *p_object) {
  181. // Reverse order so items can be removed.
  182. for (int i = overlapping_shapes.size() - 1; i >= 0; i--) {
  183. if (overlapping_shapes[i].other_object == p_object) {
  184. overlapping_shapes.remove(i);
  185. }
  186. }
  187. }
  188. void AreaBullet::clear_overlaps() {
  189. for (int i = 0; i < overlapping_shapes.size(); i++) {
  190. call_event(overlapping_shapes[i], PhysicsServer::AREA_BODY_REMOVED);
  191. overlapping_shapes[i].other_object->on_exit_area(this);
  192. }
  193. overlapping_shapes.clear();
  194. }
  195. void AreaBullet::set_monitorable(bool p_monitorable) {
  196. monitorable = p_monitorable;
  197. updated = true;
  198. }
  199. bool AreaBullet::is_monitoring() const {
  200. return get_godot_object_flags() & GOF_IS_MONITORING_AREA;
  201. }
  202. void AreaBullet::main_shape_changed() {
  203. CRASH_COND(!get_main_shape());
  204. btGhost->setCollisionShape(get_main_shape());
  205. updated = true;
  206. }
  207. void AreaBullet::reload_body() {
  208. if (space) {
  209. space->remove_area(this);
  210. space->add_area(this);
  211. }
  212. }
  213. void AreaBullet::set_space(SpaceBullet *p_space) {
  214. // Clear the old space if there is one
  215. if (space) {
  216. clear_overlaps();
  217. isScratched = false;
  218. // Remove this object form the physics world
  219. space->remove_area(this);
  220. }
  221. space = p_space;
  222. if (space) {
  223. space->add_area(this);
  224. }
  225. }
  226. void AreaBullet::on_collision_filters_change() {
  227. if (space) {
  228. space->reload_collision_filters(this);
  229. }
  230. updated = true;
  231. }
  232. void AreaBullet::set_param(PhysicsServer::AreaParameter p_param, const Variant &p_value) {
  233. switch (p_param) {
  234. case PhysicsServer::AREA_PARAM_GRAVITY:
  235. set_spOv_gravityMag(p_value);
  236. break;
  237. case PhysicsServer::AREA_PARAM_GRAVITY_VECTOR:
  238. set_spOv_gravityVec(p_value);
  239. break;
  240. case PhysicsServer::AREA_PARAM_LINEAR_DAMP:
  241. set_spOv_linearDump(p_value);
  242. break;
  243. case PhysicsServer::AREA_PARAM_ANGULAR_DAMP:
  244. set_spOv_angularDump(p_value);
  245. break;
  246. case PhysicsServer::AREA_PARAM_PRIORITY:
  247. set_spOv_priority(p_value);
  248. break;
  249. case PhysicsServer::AREA_PARAM_GRAVITY_IS_POINT:
  250. set_spOv_gravityPoint(p_value);
  251. break;
  252. case PhysicsServer::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
  253. set_spOv_gravityPointDistanceScale(p_value);
  254. break;
  255. case PhysicsServer::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
  256. set_spOv_gravityPointAttenuation(p_value);
  257. break;
  258. default:
  259. WARN_PRINT("Area doesn't support this parameter in the Bullet backend: " + itos(p_param));
  260. }
  261. isScratched = true;
  262. }
  263. Variant AreaBullet::get_param(PhysicsServer::AreaParameter p_param) const {
  264. switch (p_param) {
  265. case PhysicsServer::AREA_PARAM_GRAVITY:
  266. return spOv_gravityMag;
  267. case PhysicsServer::AREA_PARAM_GRAVITY_VECTOR:
  268. return spOv_gravityVec;
  269. case PhysicsServer::AREA_PARAM_LINEAR_DAMP:
  270. return spOv_linearDump;
  271. case PhysicsServer::AREA_PARAM_ANGULAR_DAMP:
  272. return spOv_angularDump;
  273. case PhysicsServer::AREA_PARAM_PRIORITY:
  274. return spOv_priority;
  275. case PhysicsServer::AREA_PARAM_GRAVITY_IS_POINT:
  276. return spOv_gravityPoint;
  277. case PhysicsServer::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
  278. return spOv_gravityPointDistanceScale;
  279. case PhysicsServer::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
  280. return spOv_gravityPointAttenuation;
  281. default:
  282. WARN_PRINT("Area doesn't support this parameter in the Bullet backend: " + itos(p_param));
  283. return Variant();
  284. }
  285. }
  286. void AreaBullet::set_event_callback(Type p_callbackObjectType, ObjectID p_id, const StringName &p_method) {
  287. InOutEventCallback &ev = eventsCallbacks[static_cast<int>(p_callbackObjectType)];
  288. ev.event_callback_id = p_id;
  289. ev.event_callback_method = p_method;
  290. /// Set if monitoring
  291. if (eventsCallbacks[0].event_callback_id || eventsCallbacks[1].event_callback_id) {
  292. set_godot_object_flags(get_godot_object_flags() | GOF_IS_MONITORING_AREA);
  293. } else {
  294. set_godot_object_flags(get_godot_object_flags() & (~GOF_IS_MONITORING_AREA));
  295. clear_overlaps();
  296. }
  297. }
  298. bool AreaBullet::has_event_callback(Type p_callbackObjectType) {
  299. return eventsCallbacks[static_cast<int>(p_callbackObjectType)].event_callback_id;
  300. }
  301. void AreaBullet::on_enter_area(AreaBullet *p_area) {
  302. }
  303. void AreaBullet::on_exit_area(AreaBullet *p_area) {
  304. CollisionObjectBullet::on_exit_area(p_area);
  305. }