area_sw.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*************************************************************************/
  2. /* area_sw.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_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. }
  47. void AreaSW::set_transform(const Transform &p_transform) {
  48. if (!moved_list.in_list() && get_space())
  49. get_space()->area_add_to_moved_list(&moved_list);
  50. _set_transform(p_transform);
  51. _set_inv_transform(p_transform.affine_inverse());
  52. }
  53. void AreaSW::set_space(SpaceSW *p_space) {
  54. if (get_space()) {
  55. if (monitor_query_list.in_list())
  56. get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
  57. if (moved_list.in_list())
  58. get_space()->area_remove_from_moved_list(&moved_list);
  59. }
  60. monitored_bodies.clear();
  61. monitored_areas.clear();
  62. _set_space(p_space);
  63. }
  64. void AreaSW::set_monitor_callback(ObjectID p_id, const StringName &p_method) {
  65. if (p_id == monitor_callback_id) {
  66. monitor_callback_method = p_method;
  67. return;
  68. }
  69. _unregister_shapes();
  70. monitor_callback_id = p_id;
  71. monitor_callback_method = p_method;
  72. monitored_bodies.clear();
  73. monitored_areas.clear();
  74. _shape_changed();
  75. if (!moved_list.in_list() && get_space())
  76. get_space()->area_add_to_moved_list(&moved_list);
  77. }
  78. void AreaSW::set_area_monitor_callback(ObjectID p_id, const StringName &p_method) {
  79. if (p_id == area_monitor_callback_id) {
  80. area_monitor_callback_method = p_method;
  81. return;
  82. }
  83. _unregister_shapes();
  84. area_monitor_callback_id = p_id;
  85. area_monitor_callback_method = p_method;
  86. monitored_bodies.clear();
  87. monitored_areas.clear();
  88. _shape_changed();
  89. if (!moved_list.in_list() && get_space())
  90. get_space()->area_add_to_moved_list(&moved_list);
  91. }
  92. void AreaSW::set_space_override_mode(PhysicsServer::AreaSpaceOverrideMode p_mode) {
  93. bool do_override = p_mode != PhysicsServer::AREA_SPACE_OVERRIDE_DISABLED;
  94. if (do_override == (space_override_mode != PhysicsServer::AREA_SPACE_OVERRIDE_DISABLED))
  95. return;
  96. _unregister_shapes();
  97. space_override_mode = p_mode;
  98. _shape_changed();
  99. }
  100. void AreaSW::set_param(PhysicsServer::AreaParameter p_param, const Variant &p_value) {
  101. switch (p_param) {
  102. case PhysicsServer::AREA_PARAM_GRAVITY: gravity = p_value; break;
  103. case PhysicsServer::AREA_PARAM_GRAVITY_VECTOR: gravity_vector = p_value; break;
  104. case PhysicsServer::AREA_PARAM_GRAVITY_IS_POINT: gravity_is_point = p_value; break;
  105. case PhysicsServer::AREA_PARAM_GRAVITY_DISTANCE_SCALE: gravity_distance_scale = p_value; break;
  106. case PhysicsServer::AREA_PARAM_GRAVITY_POINT_ATTENUATION: point_attenuation = p_value; break;
  107. case PhysicsServer::AREA_PARAM_LINEAR_DAMP: linear_damp = p_value; break;
  108. case PhysicsServer::AREA_PARAM_ANGULAR_DAMP: angular_damp = p_value; break;
  109. case PhysicsServer::AREA_PARAM_PRIORITY: priority = p_value; break;
  110. }
  111. }
  112. Variant AreaSW::get_param(PhysicsServer::AreaParameter p_param) const {
  113. switch (p_param) {
  114. case PhysicsServer::AREA_PARAM_GRAVITY: return gravity;
  115. case PhysicsServer::AREA_PARAM_GRAVITY_VECTOR: return gravity_vector;
  116. case PhysicsServer::AREA_PARAM_GRAVITY_IS_POINT: return gravity_is_point;
  117. case PhysicsServer::AREA_PARAM_GRAVITY_DISTANCE_SCALE: return gravity_distance_scale;
  118. case PhysicsServer::AREA_PARAM_GRAVITY_POINT_ATTENUATION: return point_attenuation;
  119. case PhysicsServer::AREA_PARAM_LINEAR_DAMP: return linear_damp;
  120. case PhysicsServer::AREA_PARAM_ANGULAR_DAMP: return angular_damp;
  121. case PhysicsServer::AREA_PARAM_PRIORITY: return priority;
  122. }
  123. return Variant();
  124. }
  125. void AreaSW::_queue_monitor_update() {
  126. ERR_FAIL_COND(!get_space());
  127. if (!monitor_query_list.in_list())
  128. get_space()->area_add_to_monitor_query_list(&monitor_query_list);
  129. }
  130. void AreaSW::set_monitorable(bool p_monitorable) {
  131. if (monitorable == p_monitorable)
  132. return;
  133. monitorable = p_monitorable;
  134. _set_static(!monitorable);
  135. }
  136. void AreaSW::call_queries() {
  137. if (monitor_callback_id && !monitored_bodies.empty()) {
  138. Variant res[5];
  139. Variant *resptr[5];
  140. for (int i = 0; i < 5; i++)
  141. resptr[i] = &res[i];
  142. Object *obj = ObjectDB::get_instance(monitor_callback_id);
  143. if (!obj) {
  144. monitored_bodies.clear();
  145. monitor_callback_id = 0;
  146. return;
  147. }
  148. for (Map<BodyKey, BodyState>::Element *E = monitored_bodies.front(); E; E = E->next()) {
  149. if (E->get().state == 0)
  150. continue; //nothing happened
  151. res[0] = E->get().state > 0 ? PhysicsServer::AREA_BODY_ADDED : PhysicsServer::AREA_BODY_REMOVED;
  152. res[1] = E->key().rid;
  153. res[2] = E->key().instance_id;
  154. res[3] = E->key().body_shape;
  155. res[4] = E->key().area_shape;
  156. Variant::CallError ce;
  157. obj->call(monitor_callback_method, (const Variant **)resptr, 5, ce);
  158. }
  159. }
  160. monitored_bodies.clear();
  161. if (area_monitor_callback_id && !monitored_areas.empty()) {
  162. Variant res[5];
  163. Variant *resptr[5];
  164. for (int i = 0; i < 5; i++)
  165. resptr[i] = &res[i];
  166. Object *obj = ObjectDB::get_instance(area_monitor_callback_id);
  167. if (!obj) {
  168. monitored_areas.clear();
  169. area_monitor_callback_id = 0;
  170. return;
  171. }
  172. for (Map<BodyKey, BodyState>::Element *E = monitored_areas.front(); E; E = E->next()) {
  173. if (E->get().state == 0)
  174. continue; //nothing happened
  175. res[0] = E->get().state > 0 ? PhysicsServer::AREA_BODY_ADDED : PhysicsServer::AREA_BODY_REMOVED;
  176. res[1] = E->key().rid;
  177. res[2] = E->key().instance_id;
  178. res[3] = E->key().body_shape;
  179. res[4] = E->key().area_shape;
  180. Variant::CallError ce;
  181. obj->call(area_monitor_callback_method, (const Variant **)resptr, 5, ce);
  182. }
  183. }
  184. monitored_areas.clear();
  185. //get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
  186. }
  187. AreaSW::AreaSW() :
  188. CollisionObjectSW(TYPE_AREA),
  189. monitor_query_list(this),
  190. moved_list(this) {
  191. _set_static(true); //areas are never active
  192. space_override_mode = PhysicsServer::AREA_SPACE_OVERRIDE_DISABLED;
  193. gravity = 9.80665;
  194. gravity_vector = Vector3(0, -1, 0);
  195. gravity_is_point = false;
  196. gravity_distance_scale = 0;
  197. point_attenuation = 1;
  198. angular_damp = 1.0;
  199. linear_damp = 0.1;
  200. priority = 0;
  201. set_ray_pickable(false);
  202. monitor_callback_id = 0;
  203. area_monitor_callback_id = 0;
  204. monitorable = false;
  205. }
  206. AreaSW::~AreaSW() {
  207. }