godot_area_2d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*************************************************************************/
  2. /* godot_area_2d.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 "godot_area_2d.h"
  31. #include "godot_body_2d.h"
  32. #include "godot_space_2d.h"
  33. GodotArea2D::BodyKey::BodyKey(GodotBody2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  34. rid = p_body->get_self();
  35. instance_id = p_body->get_instance_id();
  36. body_shape = p_body_shape;
  37. area_shape = p_area_shape;
  38. }
  39. GodotArea2D::BodyKey::BodyKey(GodotArea2D *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
  40. rid = p_body->get_self();
  41. instance_id = p_body->get_instance_id();
  42. body_shape = p_body_shape;
  43. area_shape = p_area_shape;
  44. }
  45. void GodotArea2D::_shapes_changed() {
  46. if (!moved_list.in_list() && get_space()) {
  47. get_space()->area_add_to_moved_list(&moved_list);
  48. }
  49. }
  50. void GodotArea2D::set_transform(const Transform2D &p_transform) {
  51. if (!moved_list.in_list() && get_space()) {
  52. get_space()->area_add_to_moved_list(&moved_list);
  53. }
  54. _set_transform(p_transform);
  55. _set_inv_transform(p_transform.affine_inverse());
  56. }
  57. void GodotArea2D::set_space(GodotSpace2D *p_space) {
  58. if (get_space()) {
  59. if (monitor_query_list.in_list()) {
  60. get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
  61. }
  62. if (moved_list.in_list()) {
  63. get_space()->area_remove_from_moved_list(&moved_list);
  64. }
  65. }
  66. monitored_bodies.clear();
  67. monitored_areas.clear();
  68. _set_space(p_space);
  69. }
  70. void GodotArea2D::set_monitor_callback(const Callable &p_callback) {
  71. ObjectID id = p_callback.get_object_id();
  72. if (id == monitor_callback.get_object_id()) {
  73. monitor_callback = p_callback;
  74. return;
  75. }
  76. _unregister_shapes();
  77. monitor_callback = p_callback;
  78. monitored_bodies.clear();
  79. monitored_areas.clear();
  80. _shape_changed();
  81. if (!moved_list.in_list() && get_space()) {
  82. get_space()->area_add_to_moved_list(&moved_list);
  83. }
  84. }
  85. void GodotArea2D::set_area_monitor_callback(const Callable &p_callback) {
  86. ObjectID id = p_callback.get_object_id();
  87. if (id == area_monitor_callback.get_object_id()) {
  88. area_monitor_callback = p_callback;
  89. return;
  90. }
  91. _unregister_shapes();
  92. area_monitor_callback = p_callback;
  93. monitored_bodies.clear();
  94. monitored_areas.clear();
  95. _shape_changed();
  96. if (!moved_list.in_list() && get_space()) {
  97. get_space()->area_add_to_moved_list(&moved_list);
  98. }
  99. }
  100. void GodotArea2D::_set_space_override_mode(PhysicsServer2D::AreaSpaceOverrideMode &r_mode, PhysicsServer2D::AreaSpaceOverrideMode p_new_mode) {
  101. bool do_override = p_new_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
  102. if (do_override == (r_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED)) {
  103. return;
  104. }
  105. _unregister_shapes();
  106. r_mode = p_new_mode;
  107. _shape_changed();
  108. }
  109. void GodotArea2D::set_param(PhysicsServer2D::AreaParameter p_param, const Variant &p_value) {
  110. switch (p_param) {
  111. case PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE:
  112. _set_space_override_mode(gravity_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
  113. break;
  114. case PhysicsServer2D::AREA_PARAM_GRAVITY:
  115. gravity = p_value;
  116. break;
  117. case PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR:
  118. gravity_vector = p_value;
  119. break;
  120. case PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT:
  121. gravity_is_point = p_value;
  122. break;
  123. case PhysicsServer2D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
  124. gravity_distance_scale = p_value;
  125. break;
  126. case PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
  127. point_attenuation = p_value;
  128. break;
  129. case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE:
  130. _set_space_override_mode(linear_damping_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
  131. break;
  132. case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP:
  133. linear_damp = p_value;
  134. break;
  135. case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE:
  136. _set_space_override_mode(angular_damping_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
  137. break;
  138. case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP:
  139. angular_damp = p_value;
  140. break;
  141. case PhysicsServer2D::AREA_PARAM_PRIORITY:
  142. priority = p_value;
  143. break;
  144. }
  145. }
  146. Variant GodotArea2D::get_param(PhysicsServer2D::AreaParameter p_param) const {
  147. switch (p_param) {
  148. case PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE:
  149. return gravity_override_mode;
  150. case PhysicsServer2D::AREA_PARAM_GRAVITY:
  151. return gravity;
  152. case PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR:
  153. return gravity_vector;
  154. case PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT:
  155. return gravity_is_point;
  156. case PhysicsServer2D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
  157. return gravity_distance_scale;
  158. case PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
  159. return point_attenuation;
  160. case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE:
  161. return linear_damping_override_mode;
  162. case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP:
  163. return linear_damp;
  164. case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE:
  165. return angular_damping_override_mode;
  166. case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP:
  167. return angular_damp;
  168. case PhysicsServer2D::AREA_PARAM_PRIORITY:
  169. return priority;
  170. }
  171. return Variant();
  172. }
  173. void GodotArea2D::_queue_monitor_update() {
  174. ERR_FAIL_COND(!get_space());
  175. if (!monitor_query_list.in_list()) {
  176. get_space()->area_add_to_monitor_query_list(&monitor_query_list);
  177. }
  178. }
  179. void GodotArea2D::set_monitorable(bool p_monitorable) {
  180. if (monitorable == p_monitorable) {
  181. return;
  182. }
  183. monitorable = p_monitorable;
  184. _set_static(!monitorable);
  185. _shapes_changed();
  186. }
  187. void GodotArea2D::call_queries() {
  188. if (!monitor_callback.is_null() && !monitored_bodies.is_empty()) {
  189. if (monitor_callback.is_valid()) {
  190. Variant res[5];
  191. Variant *resptr[5];
  192. for (int i = 0; i < 5; i++) {
  193. resptr[i] = &res[i];
  194. }
  195. for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_bodies.begin(); E;) {
  196. if (E->value.state == 0) { // Nothing happened
  197. HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
  198. ++next;
  199. monitored_bodies.remove(E);
  200. E = next;
  201. continue;
  202. }
  203. res[0] = E->value.state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED;
  204. res[1] = E->key.rid;
  205. res[2] = E->key.instance_id;
  206. res[3] = E->key.body_shape;
  207. res[4] = E->key.area_shape;
  208. HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
  209. ++next;
  210. monitored_bodies.remove(E);
  211. E = next;
  212. Callable::CallError ce;
  213. Variant ret;
  214. monitor_callback.callp((const Variant **)resptr, 5, ret, ce);
  215. if (ce.error != Callable::CallError::CALL_OK) {
  216. ERR_PRINT_ONCE("Error calling event callback method " + Variant::get_callable_error_text(monitor_callback, (const Variant **)resptr, 5, ce));
  217. }
  218. }
  219. } else {
  220. monitored_bodies.clear();
  221. monitor_callback = Callable();
  222. }
  223. }
  224. if (!area_monitor_callback.is_null() && !monitored_areas.is_empty()) {
  225. if (area_monitor_callback.is_valid()) {
  226. Variant res[5];
  227. Variant *resptr[5];
  228. for (int i = 0; i < 5; i++) {
  229. resptr[i] = &res[i];
  230. }
  231. for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_areas.begin(); E;) {
  232. if (E->value.state == 0) { // Nothing happened
  233. HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
  234. ++next;
  235. monitored_areas.remove(E);
  236. E = next;
  237. continue;
  238. }
  239. res[0] = E->value.state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED;
  240. res[1] = E->key.rid;
  241. res[2] = E->key.instance_id;
  242. res[3] = E->key.body_shape;
  243. res[4] = E->key.area_shape;
  244. HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
  245. ++next;
  246. monitored_areas.remove(E);
  247. E = next;
  248. Callable::CallError ce;
  249. Variant ret;
  250. area_monitor_callback.callp((const Variant **)resptr, 5, ret, ce);
  251. if (ce.error != Callable::CallError::CALL_OK) {
  252. ERR_PRINT_ONCE("Error calling event callback method " + Variant::get_callable_error_text(area_monitor_callback, (const Variant **)resptr, 5, ce));
  253. }
  254. }
  255. } else {
  256. monitored_areas.clear();
  257. area_monitor_callback = Callable();
  258. }
  259. }
  260. }
  261. void GodotArea2D::compute_gravity(const Vector2 &p_position, Vector2 &r_gravity) const {
  262. if (is_gravity_point()) {
  263. const real_t gravity_distance_scale = get_gravity_distance_scale();
  264. Vector2 v = get_transform().xform(get_gravity_vector()) - p_position;
  265. if (gravity_distance_scale > 0) {
  266. const real_t v_length = v.length();
  267. if (v_length > 0) {
  268. const real_t v_scaled = v_length * gravity_distance_scale;
  269. r_gravity = (v.normalized() * (get_gravity() / (v_scaled * v_scaled)));
  270. } else {
  271. r_gravity = Vector2();
  272. }
  273. } else {
  274. r_gravity = v.normalized() * get_gravity();
  275. }
  276. } else {
  277. r_gravity = get_gravity_vector() * get_gravity();
  278. }
  279. }
  280. GodotArea2D::GodotArea2D() :
  281. GodotCollisionObject2D(TYPE_AREA),
  282. monitor_query_list(this),
  283. moved_list(this) {
  284. _set_static(true); //areas are not active by default
  285. }
  286. GodotArea2D::~GodotArea2D() {
  287. }