area_sw.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /**************************************************************************/
  2. /* area_sw.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_sw.h"
  31. #include "body_sw.h"
  32. #include "space_sw.h"
  33. AreaSW::BodyKey::BodyKey(BodySW *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. AreaSW::BodyKey::BodyKey(AreaSW *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 AreaSW::_shapes_changed() {
  46. if (!moved_list.in_list() && get_space()) {
  47. get_space()->area_add_to_moved_list(&moved_list);
  48. }
  49. }
  50. void AreaSW::set_transform(const Transform &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 AreaSW::set_space(SpaceSW *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 AreaSW::set_monitor_callback(ObjectID p_id, const StringName &p_method) {
  71. if (p_id == monitor_callback_id) {
  72. monitor_callback_method = p_method;
  73. return;
  74. }
  75. _unregister_shapes();
  76. monitor_callback_id = p_id;
  77. monitor_callback_method = p_method;
  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 AreaSW::set_area_monitor_callback(ObjectID p_id, const StringName &p_method) {
  86. if (p_id == area_monitor_callback_id) {
  87. area_monitor_callback_method = p_method;
  88. return;
  89. }
  90. _unregister_shapes();
  91. area_monitor_callback_id = p_id;
  92. area_monitor_callback_method = p_method;
  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 AreaSW::set_space_override_mode(PhysicsServer::AreaSpaceOverrideMode p_mode) {
  101. bool do_override = p_mode != PhysicsServer::AREA_SPACE_OVERRIDE_DISABLED;
  102. if (do_override == (space_override_mode != PhysicsServer::AREA_SPACE_OVERRIDE_DISABLED)) {
  103. return;
  104. }
  105. _unregister_shapes();
  106. space_override_mode = p_mode;
  107. _shape_changed();
  108. }
  109. void AreaSW::set_param(PhysicsServer::AreaParameter p_param, const Variant &p_value) {
  110. switch (p_param) {
  111. case PhysicsServer::AREA_PARAM_GRAVITY:
  112. gravity = p_value;
  113. break;
  114. case PhysicsServer::AREA_PARAM_GRAVITY_VECTOR:
  115. gravity_vector = p_value;
  116. break;
  117. case PhysicsServer::AREA_PARAM_GRAVITY_IS_POINT:
  118. gravity_is_point = p_value;
  119. break;
  120. case PhysicsServer::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
  121. gravity_distance_scale = p_value;
  122. break;
  123. case PhysicsServer::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
  124. point_attenuation = p_value;
  125. break;
  126. case PhysicsServer::AREA_PARAM_LINEAR_DAMP:
  127. linear_damp = p_value;
  128. break;
  129. case PhysicsServer::AREA_PARAM_ANGULAR_DAMP:
  130. angular_damp = p_value;
  131. break;
  132. case PhysicsServer::AREA_PARAM_PRIORITY:
  133. priority = p_value;
  134. break;
  135. }
  136. }
  137. Variant AreaSW::get_param(PhysicsServer::AreaParameter p_param) const {
  138. switch (p_param) {
  139. case PhysicsServer::AREA_PARAM_GRAVITY:
  140. return gravity;
  141. case PhysicsServer::AREA_PARAM_GRAVITY_VECTOR:
  142. return gravity_vector;
  143. case PhysicsServer::AREA_PARAM_GRAVITY_IS_POINT:
  144. return gravity_is_point;
  145. case PhysicsServer::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
  146. return gravity_distance_scale;
  147. case PhysicsServer::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
  148. return point_attenuation;
  149. case PhysicsServer::AREA_PARAM_LINEAR_DAMP:
  150. return linear_damp;
  151. case PhysicsServer::AREA_PARAM_ANGULAR_DAMP:
  152. return angular_damp;
  153. case PhysicsServer::AREA_PARAM_PRIORITY:
  154. return priority;
  155. }
  156. return Variant();
  157. }
  158. void AreaSW::_queue_monitor_update() {
  159. ERR_FAIL_COND(!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 AreaSW::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 AreaSW::call_queries() {
  173. if (monitor_callback_id && !monitored_bodies.empty()) {
  174. Object *obj = ObjectDB::get_instance(monitor_callback_id);
  175. if (obj) {
  176. Variant res[5];
  177. Variant *resptr[5];
  178. for (int i = 0; i < 5; i++) {
  179. resptr[i] = &res[i];
  180. }
  181. for (Map<BodyKey, BodyState>::Element *E = monitored_bodies.front(); E;) {
  182. if (E->get().state == 0) { // Nothing happened
  183. Map<BodyKey, BodyState>::Element *next = E->next();
  184. monitored_bodies.erase(E);
  185. E = next;
  186. continue;
  187. }
  188. res[0] = E->get().state > 0 ? PhysicsServer::AREA_BODY_ADDED : PhysicsServer::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. Map<BodyKey, BodyState>::Element *next = E->next();
  194. monitored_bodies.erase(E);
  195. E = next;
  196. Variant::CallError ce;
  197. obj->call(monitor_callback_method, (const Variant **)resptr, 5, ce);
  198. }
  199. } else {
  200. monitored_bodies.clear();
  201. monitor_callback_id = 0;
  202. }
  203. }
  204. if (area_monitor_callback_id && !monitored_areas.empty()) {
  205. Object *obj = ObjectDB::get_instance(area_monitor_callback_id);
  206. if (obj) {
  207. Variant res[5];
  208. Variant *resptr[5];
  209. for (int i = 0; i < 5; i++) {
  210. resptr[i] = &res[i];
  211. }
  212. for (Map<BodyKey, BodyState>::Element *E = monitored_areas.front(); E;) {
  213. if (E->get().state == 0) { // Nothing happened
  214. Map<BodyKey, BodyState>::Element *next = E->next();
  215. monitored_areas.erase(E);
  216. E = next;
  217. continue;
  218. }
  219. res[0] = E->get().state > 0 ? PhysicsServer::AREA_BODY_ADDED : PhysicsServer::AREA_BODY_REMOVED;
  220. res[1] = E->key().rid;
  221. res[2] = E->key().instance_id;
  222. res[3] = E->key().body_shape;
  223. res[4] = E->key().area_shape;
  224. Map<BodyKey, BodyState>::Element *next = E->next();
  225. monitored_areas.erase(E);
  226. E = next;
  227. Variant::CallError ce;
  228. obj->call(area_monitor_callback_method, (const Variant **)resptr, 5, ce);
  229. }
  230. } else {
  231. monitored_areas.clear();
  232. area_monitor_callback_id = 0;
  233. }
  234. }
  235. }
  236. AreaSW::AreaSW() :
  237. CollisionObjectSW(TYPE_AREA),
  238. monitor_query_list(this),
  239. moved_list(this) {
  240. _set_static(true); //areas are never active
  241. space_override_mode = PhysicsServer::AREA_SPACE_OVERRIDE_DISABLED;
  242. gravity = 9.80665;
  243. gravity_vector = Vector3(0, -1, 0);
  244. gravity_is_point = false;
  245. gravity_distance_scale = 0;
  246. point_attenuation = 1;
  247. angular_damp = 0.1;
  248. linear_damp = 0.1;
  249. priority = 0;
  250. set_ray_pickable(false);
  251. monitor_callback_id = 0;
  252. area_monitor_callback_id = 0;
  253. monitorable = false;
  254. }
  255. AreaSW::~AreaSW() {
  256. }