godot_area_3d.cpp 12 KB

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