godot_area_3d.cpp 12 KB

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