godot_area_3d.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*************************************************************************/
  2. /* godot_area_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "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 p_mode) {
  108. bool do_override = p_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;
  109. if (do_override == (space_override_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED)) {
  110. return;
  111. }
  112. _unregister_shapes();
  113. space_override_mode = p_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:
  119. gravity = p_value;
  120. break;
  121. case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR:
  122. gravity_vector = p_value;
  123. break;
  124. case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT:
  125. gravity_is_point = p_value;
  126. break;
  127. case PhysicsServer3D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
  128. gravity_distance_scale = p_value;
  129. break;
  130. case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
  131. point_attenuation = p_value;
  132. break;
  133. case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP:
  134. linear_damp = p_value;
  135. break;
  136. case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP:
  137. angular_damp = p_value;
  138. break;
  139. case PhysicsServer3D::AREA_PARAM_PRIORITY:
  140. priority = p_value;
  141. break;
  142. case PhysicsServer3D::AREA_PARAM_WIND_FORCE_MAGNITUDE:
  143. ERR_FAIL_COND_MSG(wind_force_magnitude < 0, "Wind force magnitude must be a non-negative real number, but a negative number was specified.");
  144. wind_force_magnitude = p_value;
  145. break;
  146. case PhysicsServer3D::AREA_PARAM_WIND_SOURCE:
  147. wind_source = p_value;
  148. break;
  149. case PhysicsServer3D::AREA_PARAM_WIND_DIRECTION:
  150. wind_direction = p_value;
  151. break;
  152. case PhysicsServer3D::AREA_PARAM_WIND_ATTENUATION_FACTOR:
  153. ERR_FAIL_COND_MSG(wind_attenuation_factor < 0, "Wind attenuation factor must be a non-negative real number, but a negative number was specified.");
  154. wind_attenuation_factor = p_value;
  155. break;
  156. }
  157. }
  158. Variant GodotArea3D::get_param(PhysicsServer3D::AreaParameter p_param) const {
  159. switch (p_param) {
  160. case PhysicsServer3D::AREA_PARAM_GRAVITY:
  161. return gravity;
  162. case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR:
  163. return gravity_vector;
  164. case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT:
  165. return gravity_is_point;
  166. case PhysicsServer3D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
  167. return gravity_distance_scale;
  168. case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
  169. return point_attenuation;
  170. case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP:
  171. return linear_damp;
  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_COND(!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. }
  200. void GodotArea3D::call_queries() {
  201. if (!monitor_callback.is_null() && !monitored_bodies.is_empty()) {
  202. if (monitor_callback.is_valid()) {
  203. Variant res[5];
  204. Variant *resptr[5];
  205. for (int i = 0; i < 5; i++) {
  206. resptr[i] = &res[i];
  207. }
  208. for (Map<BodyKey, BodyState>::Element *E = monitored_bodies.front(); E;) {
  209. if (E->get().state == 0) { // Nothing happened
  210. Map<BodyKey, BodyState>::Element *next = E->next();
  211. monitored_bodies.erase(E);
  212. E = next;
  213. continue;
  214. }
  215. res[0] = E->get().state > 0 ? PhysicsServer3D::AREA_BODY_ADDED : PhysicsServer3D::AREA_BODY_REMOVED;
  216. res[1] = E->key().rid;
  217. res[2] = E->key().instance_id;
  218. res[3] = E->key().body_shape;
  219. res[4] = E->key().area_shape;
  220. Map<BodyKey, BodyState>::Element *next = E->next();
  221. monitored_bodies.erase(E);
  222. E = next;
  223. Callable::CallError ce;
  224. Variant ret;
  225. monitor_callback.call((const Variant **)resptr, 5, ret, ce);
  226. }
  227. } else {
  228. monitored_bodies.clear();
  229. monitor_callback = Callable();
  230. }
  231. }
  232. if (!area_monitor_callback.is_null() && !monitored_areas.is_empty()) {
  233. if (area_monitor_callback.is_valid()) {
  234. Variant res[5];
  235. Variant *resptr[5];
  236. for (int i = 0; i < 5; i++) {
  237. resptr[i] = &res[i];
  238. }
  239. for (Map<BodyKey, BodyState>::Element *E = monitored_areas.front(); E;) {
  240. if (E->get().state == 0) { // Nothing happened
  241. Map<BodyKey, BodyState>::Element *next = E->next();
  242. monitored_areas.erase(E);
  243. E = next;
  244. continue;
  245. }
  246. res[0] = E->get().state > 0 ? PhysicsServer3D::AREA_BODY_ADDED : PhysicsServer3D::AREA_BODY_REMOVED;
  247. res[1] = E->key().rid;
  248. res[2] = E->key().instance_id;
  249. res[3] = E->key().body_shape;
  250. res[4] = E->key().area_shape;
  251. Map<BodyKey, BodyState>::Element *next = E->next();
  252. monitored_areas.erase(E);
  253. E = next;
  254. Callable::CallError ce;
  255. Variant ret;
  256. area_monitor_callback.call((const Variant **)resptr, 5, ret, ce);
  257. }
  258. } else {
  259. monitored_areas.clear();
  260. area_monitor_callback = Callable();
  261. }
  262. }
  263. }
  264. void GodotArea3D::compute_gravity(const Vector3 &p_position, Vector3 &r_gravity) const {
  265. if (is_gravity_point()) {
  266. const real_t gravity_distance_scale = get_gravity_distance_scale();
  267. Vector3 v = get_transform().xform(get_gravity_vector()) - p_position;
  268. if (gravity_distance_scale > 0) {
  269. const real_t v_length = v.length();
  270. if (v_length > 0) {
  271. const real_t v_scaled = v_length * gravity_distance_scale;
  272. r_gravity = (v.normalized() * (get_gravity() / (v_scaled * v_scaled)));
  273. } else {
  274. r_gravity = Vector3();
  275. }
  276. } else {
  277. r_gravity = v.normalized() * get_gravity();
  278. }
  279. } else {
  280. r_gravity = get_gravity_vector() * get_gravity();
  281. }
  282. }
  283. GodotArea3D::GodotArea3D() :
  284. GodotCollisionObject3D(TYPE_AREA),
  285. monitor_query_list(this),
  286. moved_list(this) {
  287. _set_static(true); //areas are never active
  288. set_ray_pickable(false);
  289. }
  290. GodotArea3D::~GodotArea3D() {
  291. }