godot_area_2d.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**************************************************************************/
  2. /* godot_area_2d.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 "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. _unregister_shapes();
  72. monitor_callback = p_callback;
  73. monitored_bodies.clear();
  74. monitored_areas.clear();
  75. _shape_changed();
  76. if (!moved_list.in_list() && get_space()) {
  77. get_space()->area_add_to_moved_list(&moved_list);
  78. }
  79. }
  80. void GodotArea2D::set_area_monitor_callback(const Callable &p_callback) {
  81. _unregister_shapes();
  82. area_monitor_callback = p_callback;
  83. monitored_bodies.clear();
  84. monitored_areas.clear();
  85. _shape_changed();
  86. if (!moved_list.in_list() && get_space()) {
  87. get_space()->area_add_to_moved_list(&moved_list);
  88. }
  89. }
  90. void GodotArea2D::_set_space_override_mode(PhysicsServer2D::AreaSpaceOverrideMode &r_mode, PhysicsServer2D::AreaSpaceOverrideMode p_new_mode) {
  91. bool do_override = p_new_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED;
  92. if (do_override == (r_mode != PhysicsServer2D::AREA_SPACE_OVERRIDE_DISABLED)) {
  93. return;
  94. }
  95. _unregister_shapes();
  96. r_mode = p_new_mode;
  97. _shape_changed();
  98. }
  99. void GodotArea2D::set_param(PhysicsServer2D::AreaParameter p_param, const Variant &p_value) {
  100. switch (p_param) {
  101. case PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE:
  102. _set_space_override_mode(gravity_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
  103. break;
  104. case PhysicsServer2D::AREA_PARAM_GRAVITY:
  105. gravity = p_value;
  106. break;
  107. case PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR:
  108. gravity_vector = p_value;
  109. break;
  110. case PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT:
  111. gravity_is_point = p_value;
  112. break;
  113. case PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE:
  114. gravity_point_unit_distance = p_value;
  115. break;
  116. case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE:
  117. _set_space_override_mode(linear_damping_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
  118. break;
  119. case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP:
  120. linear_damp = p_value;
  121. break;
  122. case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE:
  123. _set_space_override_mode(angular_damping_override_mode, (PhysicsServer2D::AreaSpaceOverrideMode)(int)p_value);
  124. break;
  125. case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP:
  126. angular_damp = p_value;
  127. break;
  128. case PhysicsServer2D::AREA_PARAM_PRIORITY:
  129. priority = p_value;
  130. break;
  131. }
  132. }
  133. Variant GodotArea2D::get_param(PhysicsServer2D::AreaParameter p_param) const {
  134. switch (p_param) {
  135. case PhysicsServer2D::AREA_PARAM_GRAVITY_OVERRIDE_MODE:
  136. return gravity_override_mode;
  137. case PhysicsServer2D::AREA_PARAM_GRAVITY:
  138. return gravity;
  139. case PhysicsServer2D::AREA_PARAM_GRAVITY_VECTOR:
  140. return gravity_vector;
  141. case PhysicsServer2D::AREA_PARAM_GRAVITY_IS_POINT:
  142. return gravity_is_point;
  143. case PhysicsServer2D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE:
  144. return gravity_point_unit_distance;
  145. case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE:
  146. return linear_damping_override_mode;
  147. case PhysicsServer2D::AREA_PARAM_LINEAR_DAMP:
  148. return linear_damp;
  149. case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE:
  150. return angular_damping_override_mode;
  151. case PhysicsServer2D::AREA_PARAM_ANGULAR_DAMP:
  152. return angular_damp;
  153. case PhysicsServer2D::AREA_PARAM_PRIORITY:
  154. return priority;
  155. }
  156. return Variant();
  157. }
  158. void GodotArea2D::_queue_monitor_update() {
  159. ERR_FAIL_NULL(get_space());
  160. if (!monitor_query_list.in_list()) {
  161. get_space()->area_add_to_monitor_query_list(&monitor_query_list);
  162. }
  163. }
  164. void GodotArea2D::set_monitorable(bool p_monitorable) {
  165. if (monitorable == p_monitorable) {
  166. return;
  167. }
  168. monitorable = p_monitorable;
  169. _set_static(!monitorable);
  170. _shapes_changed();
  171. }
  172. void GodotArea2D::call_queries() {
  173. if (!monitor_callback.is_null() && !monitored_bodies.is_empty()) {
  174. if (monitor_callback.is_valid()) {
  175. Variant res[5];
  176. Variant *resptr[5];
  177. for (int i = 0; i < 5; i++) {
  178. resptr[i] = &res[i];
  179. }
  180. for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_bodies.begin(); E;) {
  181. if (E->value.state == 0) { // Nothing happened
  182. HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
  183. ++next;
  184. monitored_bodies.remove(E);
  185. E = next;
  186. continue;
  187. }
  188. res[0] = E->value.state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED;
  189. res[1] = E->key.rid;
  190. res[2] = E->key.instance_id;
  191. res[3] = E->key.body_shape;
  192. res[4] = E->key.area_shape;
  193. HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
  194. ++next;
  195. monitored_bodies.remove(E);
  196. E = next;
  197. Callable::CallError ce;
  198. Variant ret;
  199. monitor_callback.callp((const Variant **)resptr, 5, ret, ce);
  200. if (ce.error != Callable::CallError::CALL_OK) {
  201. ERR_PRINT_ONCE("Error calling event callback method " + Variant::get_callable_error_text(monitor_callback, (const Variant **)resptr, 5, ce));
  202. }
  203. }
  204. } else {
  205. monitored_bodies.clear();
  206. monitor_callback = Callable();
  207. }
  208. }
  209. if (!area_monitor_callback.is_null() && !monitored_areas.is_empty()) {
  210. if (area_monitor_callback.is_valid()) {
  211. Variant res[5];
  212. Variant *resptr[5];
  213. for (int i = 0; i < 5; i++) {
  214. resptr[i] = &res[i];
  215. }
  216. for (HashMap<BodyKey, BodyState, BodyKey>::Iterator E = monitored_areas.begin(); E;) {
  217. if (E->value.state == 0) { // Nothing happened
  218. HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
  219. ++next;
  220. monitored_areas.remove(E);
  221. E = next;
  222. continue;
  223. }
  224. res[0] = E->value.state > 0 ? PhysicsServer2D::AREA_BODY_ADDED : PhysicsServer2D::AREA_BODY_REMOVED;
  225. res[1] = E->key.rid;
  226. res[2] = E->key.instance_id;
  227. res[3] = E->key.body_shape;
  228. res[4] = E->key.area_shape;
  229. HashMap<BodyKey, BodyState, BodyKey>::Iterator next = E;
  230. ++next;
  231. monitored_areas.remove(E);
  232. E = next;
  233. Callable::CallError ce;
  234. Variant ret;
  235. area_monitor_callback.callp((const Variant **)resptr, 5, ret, ce);
  236. if (ce.error != Callable::CallError::CALL_OK) {
  237. ERR_PRINT_ONCE("Error calling event callback method " + Variant::get_callable_error_text(area_monitor_callback, (const Variant **)resptr, 5, ce));
  238. }
  239. }
  240. } else {
  241. monitored_areas.clear();
  242. area_monitor_callback = Callable();
  243. }
  244. }
  245. }
  246. void GodotArea2D::compute_gravity(const Vector2 &p_position, Vector2 &r_gravity) const {
  247. if (is_gravity_point()) {
  248. const real_t gr_unit_dist = get_gravity_point_unit_distance();
  249. Vector2 v = get_transform().xform(get_gravity_vector()) - p_position;
  250. if (gr_unit_dist > 0) {
  251. const real_t v_length_sq = v.length_squared();
  252. if (v_length_sq > 0) {
  253. const real_t gravity_strength = get_gravity() * gr_unit_dist * gr_unit_dist / v_length_sq;
  254. r_gravity = v.normalized() * gravity_strength;
  255. } else {
  256. r_gravity = Vector2();
  257. }
  258. } else {
  259. r_gravity = v.normalized() * get_gravity();
  260. }
  261. } else {
  262. r_gravity = get_gravity_vector() * get_gravity();
  263. }
  264. }
  265. GodotArea2D::GodotArea2D() :
  266. GodotCollisionObject2D(TYPE_AREA),
  267. monitor_query_list(this),
  268. moved_list(this) {
  269. _set_static(true); //areas are not active by default
  270. }
  271. GodotArea2D::~GodotArea2D() {
  272. }