area_3d.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791
  1. /**************************************************************************/
  2. /* 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 "area_3d.h"
  31. #include "scene/scene_string_names.h"
  32. #include "servers/audio_server.h"
  33. void Area3D::set_gravity_space_override_mode(SpaceOverride p_mode) {
  34. gravity_space_override = p_mode;
  35. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE, p_mode);
  36. }
  37. Area3D::SpaceOverride Area3D::get_gravity_space_override_mode() const {
  38. return gravity_space_override;
  39. }
  40. void Area3D::set_gravity_is_point(bool p_enabled) {
  41. gravity_is_point = p_enabled;
  42. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT, p_enabled);
  43. }
  44. bool Area3D::is_gravity_a_point() const {
  45. return gravity_is_point;
  46. }
  47. void Area3D::set_gravity_point_unit_distance(real_t p_scale) {
  48. gravity_point_unit_distance = p_scale;
  49. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE, p_scale);
  50. }
  51. real_t Area3D::get_gravity_point_unit_distance() const {
  52. return gravity_point_unit_distance;
  53. }
  54. void Area3D::set_gravity_point_center(const Vector3 &p_center) {
  55. gravity_vec = p_center;
  56. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR, p_center);
  57. }
  58. const Vector3 &Area3D::get_gravity_point_center() const {
  59. return gravity_vec;
  60. }
  61. void Area3D::set_gravity_direction(const Vector3 &p_direction) {
  62. gravity_vec = p_direction;
  63. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR, p_direction);
  64. }
  65. const Vector3 &Area3D::get_gravity_direction() const {
  66. return gravity_vec;
  67. }
  68. void Area3D::set_gravity(real_t p_gravity) {
  69. gravity = p_gravity;
  70. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_GRAVITY, p_gravity);
  71. }
  72. real_t Area3D::get_gravity() const {
  73. return gravity;
  74. }
  75. void Area3D::set_linear_damp_space_override_mode(SpaceOverride p_mode) {
  76. linear_damp_space_override = p_mode;
  77. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE, p_mode);
  78. }
  79. Area3D::SpaceOverride Area3D::get_linear_damp_space_override_mode() const {
  80. return linear_damp_space_override;
  81. }
  82. void Area3D::set_angular_damp_space_override_mode(SpaceOverride p_mode) {
  83. angular_damp_space_override = p_mode;
  84. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE, p_mode);
  85. }
  86. Area3D::SpaceOverride Area3D::get_angular_damp_space_override_mode() const {
  87. return angular_damp_space_override;
  88. }
  89. void Area3D::set_linear_damp(real_t p_linear_damp) {
  90. linear_damp = p_linear_damp;
  91. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_LINEAR_DAMP, p_linear_damp);
  92. }
  93. real_t Area3D::get_linear_damp() const {
  94. return linear_damp;
  95. }
  96. void Area3D::set_angular_damp(real_t p_angular_damp) {
  97. angular_damp = p_angular_damp;
  98. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP, p_angular_damp);
  99. }
  100. real_t Area3D::get_angular_damp() const {
  101. return angular_damp;
  102. }
  103. void Area3D::set_priority(int p_priority) {
  104. priority = p_priority;
  105. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_PRIORITY, p_priority);
  106. }
  107. int Area3D::get_priority() const {
  108. return priority;
  109. }
  110. void Area3D::set_wind_force_magnitude(real_t p_wind_force_magnitude) {
  111. wind_force_magnitude = p_wind_force_magnitude;
  112. if (is_inside_tree()) {
  113. _initialize_wind();
  114. }
  115. }
  116. real_t Area3D::get_wind_force_magnitude() const {
  117. return wind_force_magnitude;
  118. }
  119. void Area3D::set_wind_attenuation_factor(real_t p_wind_force_attenuation_factor) {
  120. wind_attenuation_factor = p_wind_force_attenuation_factor;
  121. if (is_inside_tree()) {
  122. _initialize_wind();
  123. }
  124. }
  125. real_t Area3D::get_wind_attenuation_factor() const {
  126. return wind_attenuation_factor;
  127. }
  128. void Area3D::set_wind_source_path(const NodePath &p_wind_source_path) {
  129. wind_source_path = p_wind_source_path;
  130. if (is_inside_tree()) {
  131. _initialize_wind();
  132. }
  133. }
  134. const NodePath &Area3D::get_wind_source_path() const {
  135. return wind_source_path;
  136. }
  137. void Area3D::_initialize_wind() {
  138. real_t temp_magnitude = 0.0;
  139. Vector3 wind_direction(0., 0., 0.);
  140. Vector3 wind_source(0., 0., 0.);
  141. // Overwrite with area-specified info if available
  142. if (!wind_source_path.is_empty()) {
  143. Node *wind_source_node = get_node_or_null(wind_source_path);
  144. ERR_FAIL_NULL_MSG(wind_source_node, "Path to wind source is invalid: '" + wind_source_path + "'.");
  145. Node3D *wind_source_node3d = Object::cast_to<Node3D>(wind_source_node);
  146. ERR_FAIL_NULL_MSG(wind_source_node3d, "Path to wind source does not point to a Node3D: '" + wind_source_path + "'.");
  147. Transform3D global_transform = wind_source_node3d->get_transform();
  148. wind_direction = -global_transform.basis.get_column(Vector3::AXIS_Z).normalized();
  149. wind_source = global_transform.origin;
  150. temp_magnitude = wind_force_magnitude;
  151. }
  152. // Set force, source and direction in the physics server.
  153. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_WIND_ATTENUATION_FACTOR, wind_attenuation_factor);
  154. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_WIND_SOURCE, wind_source);
  155. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_WIND_DIRECTION, wind_direction);
  156. PhysicsServer3D::get_singleton()->area_set_param(get_rid(), PhysicsServer3D::AREA_PARAM_WIND_FORCE_MAGNITUDE, temp_magnitude);
  157. }
  158. void Area3D::_body_enter_tree(ObjectID p_id) {
  159. Object *obj = ObjectDB::get_instance(p_id);
  160. Node *node = Object::cast_to<Node>(obj);
  161. ERR_FAIL_NULL(node);
  162. HashMap<ObjectID, BodyState>::Iterator E = body_map.find(p_id);
  163. ERR_FAIL_COND(!E);
  164. ERR_FAIL_COND(E->value.in_tree);
  165. E->value.in_tree = true;
  166. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  167. for (int i = 0; i < E->value.shapes.size(); i++) {
  168. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].area_shape);
  169. }
  170. }
  171. void Area3D::_body_exit_tree(ObjectID p_id) {
  172. Object *obj = ObjectDB::get_instance(p_id);
  173. Node *node = Object::cast_to<Node>(obj);
  174. ERR_FAIL_NULL(node);
  175. HashMap<ObjectID, BodyState>::Iterator E = body_map.find(p_id);
  176. ERR_FAIL_COND(!E);
  177. ERR_FAIL_COND(!E->value.in_tree);
  178. E->value.in_tree = false;
  179. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  180. for (int i = 0; i < E->value.shapes.size(); i++) {
  181. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, E->value.rid, node, E->value.shapes[i].body_shape, E->value.shapes[i].area_shape);
  182. }
  183. }
  184. void Area3D::_body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_area_shape) {
  185. bool body_in = p_status == PhysicsServer3D::AREA_BODY_ADDED;
  186. ObjectID objid = p_instance;
  187. Object *obj = ObjectDB::get_instance(objid);
  188. Node *node = Object::cast_to<Node>(obj);
  189. HashMap<ObjectID, BodyState>::Iterator E = body_map.find(objid);
  190. if (!body_in && !E) {
  191. return; //likely removed from the tree
  192. }
  193. lock_callback();
  194. locked = true;
  195. if (body_in) {
  196. if (!E) {
  197. E = body_map.insert(objid, BodyState());
  198. E->value.rid = p_body;
  199. E->value.rc = 0;
  200. E->value.in_tree = node && node->is_inside_tree();
  201. if (node) {
  202. node->connect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &Area3D::_body_enter_tree).bind(objid));
  203. node->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Area3D::_body_exit_tree).bind(objid));
  204. if (E->value.in_tree) {
  205. emit_signal(SceneStringNames::get_singleton()->body_entered, node);
  206. }
  207. }
  208. }
  209. E->value.rc++;
  210. if (node) {
  211. E->value.shapes.insert(ShapePair(p_body_shape, p_area_shape));
  212. }
  213. if (E->value.in_tree) {
  214. emit_signal(SceneStringNames::get_singleton()->body_shape_entered, p_body, node, p_body_shape, p_area_shape);
  215. }
  216. } else {
  217. E->value.rc--;
  218. if (node) {
  219. E->value.shapes.erase(ShapePair(p_body_shape, p_area_shape));
  220. }
  221. bool in_tree = E->value.in_tree;
  222. if (E->value.rc == 0) {
  223. body_map.remove(E);
  224. if (node) {
  225. node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &Area3D::_body_enter_tree));
  226. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Area3D::_body_exit_tree));
  227. if (in_tree) {
  228. emit_signal(SceneStringNames::get_singleton()->body_exited, obj);
  229. }
  230. }
  231. }
  232. if (node && in_tree) {
  233. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, p_body, obj, p_body_shape, p_area_shape);
  234. }
  235. }
  236. locked = false;
  237. unlock_callback();
  238. }
  239. void Area3D::_clear_monitoring() {
  240. ERR_FAIL_COND_MSG(locked, "This function can't be used during the in/out signal.");
  241. {
  242. HashMap<ObjectID, BodyState> bmcopy = body_map;
  243. body_map.clear();
  244. //disconnect all monitored stuff
  245. for (const KeyValue<ObjectID, BodyState> &E : bmcopy) {
  246. Object *obj = ObjectDB::get_instance(E.key);
  247. Node *node = Object::cast_to<Node>(obj);
  248. if (!node) { //node may have been deleted in previous frame or at other legitimate point
  249. continue;
  250. }
  251. //ERR_CONTINUE(!node);
  252. node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &Area3D::_body_enter_tree));
  253. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Area3D::_body_exit_tree));
  254. if (!E.value.in_tree) {
  255. continue;
  256. }
  257. for (int i = 0; i < E.value.shapes.size(); i++) {
  258. emit_signal(SceneStringNames::get_singleton()->body_shape_exited, E.value.rid, node, E.value.shapes[i].body_shape, E.value.shapes[i].area_shape);
  259. }
  260. emit_signal(SceneStringNames::get_singleton()->body_exited, node);
  261. }
  262. }
  263. {
  264. HashMap<ObjectID, AreaState> bmcopy = area_map;
  265. area_map.clear();
  266. //disconnect all monitored stuff
  267. for (const KeyValue<ObjectID, AreaState> &E : bmcopy) {
  268. Object *obj = ObjectDB::get_instance(E.key);
  269. Node *node = Object::cast_to<Node>(obj);
  270. if (!node) { //node may have been deleted in previous frame or at other legitimate point
  271. continue;
  272. }
  273. //ERR_CONTINUE(!node);
  274. node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &Area3D::_area_enter_tree));
  275. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Area3D::_area_exit_tree));
  276. if (!E.value.in_tree) {
  277. continue;
  278. }
  279. for (int i = 0; i < E.value.shapes.size(); i++) {
  280. emit_signal(SceneStringNames::get_singleton()->area_shape_exited, E.value.rid, node, E.value.shapes[i].area_shape, E.value.shapes[i].self_shape);
  281. }
  282. emit_signal(SceneStringNames::get_singleton()->area_exited, obj);
  283. }
  284. }
  285. }
  286. void Area3D::_notification(int p_what) {
  287. switch (p_what) {
  288. case NOTIFICATION_EXIT_TREE: {
  289. _clear_monitoring();
  290. } break;
  291. case NOTIFICATION_ENTER_TREE: {
  292. _initialize_wind();
  293. } break;
  294. }
  295. }
  296. void Area3D::set_monitoring(bool p_enable) {
  297. ERR_FAIL_COND_MSG(locked, "Function blocked during in/out signal. Use set_deferred(\"monitoring\", true/false).");
  298. if (p_enable == monitoring) {
  299. return;
  300. }
  301. monitoring = p_enable;
  302. if (monitoring) {
  303. PhysicsServer3D::get_singleton()->area_set_monitor_callback(get_rid(), callable_mp(this, &Area3D::_body_inout));
  304. PhysicsServer3D::get_singleton()->area_set_area_monitor_callback(get_rid(), callable_mp(this, &Area3D::_area_inout));
  305. } else {
  306. PhysicsServer3D::get_singleton()->area_set_monitor_callback(get_rid(), Callable());
  307. PhysicsServer3D::get_singleton()->area_set_area_monitor_callback(get_rid(), Callable());
  308. _clear_monitoring();
  309. }
  310. }
  311. void Area3D::_area_enter_tree(ObjectID p_id) {
  312. Object *obj = ObjectDB::get_instance(p_id);
  313. Node *node = Object::cast_to<Node>(obj);
  314. ERR_FAIL_NULL(node);
  315. HashMap<ObjectID, AreaState>::Iterator E = area_map.find(p_id);
  316. ERR_FAIL_COND(!E);
  317. ERR_FAIL_COND(E->value.in_tree);
  318. E->value.in_tree = true;
  319. emit_signal(SceneStringNames::get_singleton()->area_entered, node);
  320. for (int i = 0; i < E->value.shapes.size(); i++) {
  321. emit_signal(SceneStringNames::get_singleton()->area_shape_entered, E->value.rid, node, E->value.shapes[i].area_shape, E->value.shapes[i].self_shape);
  322. }
  323. }
  324. void Area3D::_area_exit_tree(ObjectID p_id) {
  325. Object *obj = ObjectDB::get_instance(p_id);
  326. Node *node = Object::cast_to<Node>(obj);
  327. ERR_FAIL_NULL(node);
  328. HashMap<ObjectID, AreaState>::Iterator E = area_map.find(p_id);
  329. ERR_FAIL_COND(!E);
  330. ERR_FAIL_COND(!E->value.in_tree);
  331. E->value.in_tree = false;
  332. emit_signal(SceneStringNames::get_singleton()->area_exited, node);
  333. for (int i = 0; i < E->value.shapes.size(); i++) {
  334. emit_signal(SceneStringNames::get_singleton()->area_shape_exited, E->value.rid, node, E->value.shapes[i].area_shape, E->value.shapes[i].self_shape);
  335. }
  336. }
  337. void Area3D::_area_inout(int p_status, const RID &p_area, ObjectID p_instance, int p_area_shape, int p_self_shape) {
  338. bool area_in = p_status == PhysicsServer3D::AREA_BODY_ADDED;
  339. ObjectID objid = p_instance;
  340. Object *obj = ObjectDB::get_instance(objid);
  341. Node *node = Object::cast_to<Node>(obj);
  342. HashMap<ObjectID, AreaState>::Iterator E = area_map.find(objid);
  343. if (!area_in && !E) {
  344. return; //likely removed from the tree
  345. }
  346. lock_callback();
  347. locked = true;
  348. if (area_in) {
  349. if (!E) {
  350. E = area_map.insert(objid, AreaState());
  351. E->value.rid = p_area;
  352. E->value.rc = 0;
  353. E->value.in_tree = node && node->is_inside_tree();
  354. if (node) {
  355. node->connect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &Area3D::_area_enter_tree).bind(objid));
  356. node->connect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Area3D::_area_exit_tree).bind(objid));
  357. if (E->value.in_tree) {
  358. emit_signal(SceneStringNames::get_singleton()->area_entered, node);
  359. }
  360. }
  361. }
  362. E->value.rc++;
  363. if (node) {
  364. E->value.shapes.insert(AreaShapePair(p_area_shape, p_self_shape));
  365. }
  366. if (!node || E->value.in_tree) {
  367. emit_signal(SceneStringNames::get_singleton()->area_shape_entered, p_area, node, p_area_shape, p_self_shape);
  368. }
  369. } else {
  370. E->value.rc--;
  371. if (node) {
  372. E->value.shapes.erase(AreaShapePair(p_area_shape, p_self_shape));
  373. }
  374. bool in_tree = E->value.in_tree;
  375. if (E->value.rc == 0) {
  376. area_map.remove(E);
  377. if (node) {
  378. node->disconnect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &Area3D::_area_enter_tree));
  379. node->disconnect(SceneStringNames::get_singleton()->tree_exiting, callable_mp(this, &Area3D::_area_exit_tree));
  380. if (in_tree) {
  381. emit_signal(SceneStringNames::get_singleton()->area_exited, obj);
  382. }
  383. }
  384. }
  385. if (!node || in_tree) {
  386. emit_signal(SceneStringNames::get_singleton()->area_shape_exited, p_area, obj, p_area_shape, p_self_shape);
  387. }
  388. }
  389. locked = false;
  390. unlock_callback();
  391. }
  392. bool Area3D::is_monitoring() const {
  393. return monitoring;
  394. }
  395. TypedArray<Node3D> Area3D::get_overlapping_bodies() const {
  396. TypedArray<Node3D> ret;
  397. ERR_FAIL_COND_V_MSG(!monitoring, ret, "Can't find overlapping bodies when monitoring is off.");
  398. ret.resize(body_map.size());
  399. int idx = 0;
  400. for (const KeyValue<ObjectID, BodyState> &E : body_map) {
  401. Object *obj = ObjectDB::get_instance(E.key);
  402. if (obj) {
  403. ret[idx] = obj;
  404. idx++;
  405. }
  406. }
  407. ret.resize(idx);
  408. return ret;
  409. }
  410. bool Area3D::has_overlapping_bodies() const {
  411. ERR_FAIL_COND_V_MSG(!monitoring, false, "Can't find overlapping bodies when monitoring is off.");
  412. return !body_map.is_empty();
  413. }
  414. void Area3D::set_monitorable(bool p_enable) {
  415. ERR_FAIL_COND_MSG(locked || (is_inside_tree() && PhysicsServer3D::get_singleton()->is_flushing_queries()), "Function blocked during in/out signal. Use set_deferred(\"monitorable\", true/false).");
  416. if (p_enable == monitorable) {
  417. return;
  418. }
  419. monitorable = p_enable;
  420. PhysicsServer3D::get_singleton()->area_set_monitorable(get_rid(), monitorable);
  421. }
  422. bool Area3D::is_monitorable() const {
  423. return monitorable;
  424. }
  425. TypedArray<Area3D> Area3D::get_overlapping_areas() const {
  426. TypedArray<Area3D> ret;
  427. ERR_FAIL_COND_V_MSG(!monitoring, ret, "Can't find overlapping areas when monitoring is off.");
  428. ret.resize(area_map.size());
  429. int idx = 0;
  430. for (const KeyValue<ObjectID, AreaState> &E : area_map) {
  431. Object *obj = ObjectDB::get_instance(E.key);
  432. if (obj) {
  433. ret[idx] = obj;
  434. idx++;
  435. }
  436. }
  437. ret.resize(idx);
  438. return ret;
  439. }
  440. bool Area3D::has_overlapping_areas() const {
  441. ERR_FAIL_COND_V_MSG(!monitoring, false, "Can't find overlapping areas when monitoring is off.");
  442. return !area_map.is_empty();
  443. }
  444. bool Area3D::overlaps_area(Node *p_area) const {
  445. ERR_FAIL_NULL_V(p_area, false);
  446. HashMap<ObjectID, AreaState>::ConstIterator E = area_map.find(p_area->get_instance_id());
  447. if (!E) {
  448. return false;
  449. }
  450. return E->value.in_tree;
  451. }
  452. bool Area3D::overlaps_body(Node *p_body) const {
  453. ERR_FAIL_NULL_V(p_body, false);
  454. HashMap<ObjectID, BodyState>::ConstIterator E = body_map.find(p_body->get_instance_id());
  455. if (!E) {
  456. return false;
  457. }
  458. return E->value.in_tree;
  459. }
  460. void Area3D::set_audio_bus_override(bool p_override) {
  461. audio_bus_override = p_override;
  462. }
  463. bool Area3D::is_overriding_audio_bus() const {
  464. return audio_bus_override;
  465. }
  466. void Area3D::set_audio_bus_name(const StringName &p_audio_bus) {
  467. audio_bus = p_audio_bus;
  468. }
  469. StringName Area3D::get_audio_bus_name() const {
  470. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  471. if (AudioServer::get_singleton()->get_bus_name(i) == audio_bus) {
  472. return audio_bus;
  473. }
  474. }
  475. return SceneStringNames::get_singleton()->Master;
  476. }
  477. void Area3D::set_use_reverb_bus(bool p_enable) {
  478. use_reverb_bus = p_enable;
  479. }
  480. bool Area3D::is_using_reverb_bus() const {
  481. return use_reverb_bus;
  482. }
  483. void Area3D::set_reverb_bus_name(const StringName &p_audio_bus) {
  484. reverb_bus = p_audio_bus;
  485. }
  486. StringName Area3D::get_reverb_bus_name() const {
  487. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  488. if (AudioServer::get_singleton()->get_bus_name(i) == reverb_bus) {
  489. return reverb_bus;
  490. }
  491. }
  492. return SceneStringNames::get_singleton()->Master;
  493. }
  494. void Area3D::set_reverb_amount(float p_amount) {
  495. reverb_amount = p_amount;
  496. }
  497. float Area3D::get_reverb_amount() const {
  498. return reverb_amount;
  499. }
  500. void Area3D::set_reverb_uniformity(float p_uniformity) {
  501. reverb_uniformity = p_uniformity;
  502. }
  503. float Area3D::get_reverb_uniformity() const {
  504. return reverb_uniformity;
  505. }
  506. void Area3D::_validate_property(PropertyInfo &p_property) const {
  507. if (p_property.name == "audio_bus_name" || p_property.name == "reverb_bus_name") {
  508. String options;
  509. for (int i = 0; i < AudioServer::get_singleton()->get_bus_count(); i++) {
  510. if (i > 0) {
  511. options += ",";
  512. }
  513. String name = AudioServer::get_singleton()->get_bus_name(i);
  514. options += name;
  515. }
  516. p_property.hint_string = options;
  517. } else if (p_property.name.begins_with("gravity") && p_property.name != "gravity_space_override") {
  518. if (gravity_space_override == SPACE_OVERRIDE_DISABLED) {
  519. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  520. } else {
  521. if (gravity_is_point) {
  522. if (p_property.name == "gravity_direction") {
  523. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  524. }
  525. } else {
  526. if (p_property.name.begins_with("gravity_point_")) {
  527. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  528. }
  529. }
  530. }
  531. } else if (p_property.name.begins_with("linear_damp") && p_property.name != "linear_damp_space_override") {
  532. if (linear_damp_space_override == SPACE_OVERRIDE_DISABLED) {
  533. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  534. }
  535. } else if (p_property.name.begins_with("angular_damp") && p_property.name != "angular_damp_space_override") {
  536. if (angular_damp_space_override == SPACE_OVERRIDE_DISABLED) {
  537. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  538. }
  539. }
  540. }
  541. void Area3D::_bind_methods() {
  542. ClassDB::bind_method(D_METHOD("set_gravity_space_override_mode", "space_override_mode"), &Area3D::set_gravity_space_override_mode);
  543. ClassDB::bind_method(D_METHOD("get_gravity_space_override_mode"), &Area3D::get_gravity_space_override_mode);
  544. ClassDB::bind_method(D_METHOD("set_gravity_is_point", "enable"), &Area3D::set_gravity_is_point);
  545. ClassDB::bind_method(D_METHOD("is_gravity_a_point"), &Area3D::is_gravity_a_point);
  546. ClassDB::bind_method(D_METHOD("set_gravity_point_unit_distance", "distance_scale"), &Area3D::set_gravity_point_unit_distance);
  547. ClassDB::bind_method(D_METHOD("get_gravity_point_unit_distance"), &Area3D::get_gravity_point_unit_distance);
  548. ClassDB::bind_method(D_METHOD("set_gravity_point_center", "center"), &Area3D::set_gravity_point_center);
  549. ClassDB::bind_method(D_METHOD("get_gravity_point_center"), &Area3D::get_gravity_point_center);
  550. ClassDB::bind_method(D_METHOD("set_gravity_direction", "direction"), &Area3D::set_gravity_direction);
  551. ClassDB::bind_method(D_METHOD("get_gravity_direction"), &Area3D::get_gravity_direction);
  552. ClassDB::bind_method(D_METHOD("set_gravity", "gravity"), &Area3D::set_gravity);
  553. ClassDB::bind_method(D_METHOD("get_gravity"), &Area3D::get_gravity);
  554. ClassDB::bind_method(D_METHOD("set_linear_damp_space_override_mode", "space_override_mode"), &Area3D::set_linear_damp_space_override_mode);
  555. ClassDB::bind_method(D_METHOD("get_linear_damp_space_override_mode"), &Area3D::get_linear_damp_space_override_mode);
  556. ClassDB::bind_method(D_METHOD("set_angular_damp_space_override_mode", "space_override_mode"), &Area3D::set_angular_damp_space_override_mode);
  557. ClassDB::bind_method(D_METHOD("get_angular_damp_space_override_mode"), &Area3D::get_angular_damp_space_override_mode);
  558. ClassDB::bind_method(D_METHOD("set_angular_damp", "angular_damp"), &Area3D::set_angular_damp);
  559. ClassDB::bind_method(D_METHOD("get_angular_damp"), &Area3D::get_angular_damp);
  560. ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &Area3D::set_linear_damp);
  561. ClassDB::bind_method(D_METHOD("get_linear_damp"), &Area3D::get_linear_damp);
  562. ClassDB::bind_method(D_METHOD("set_priority", "priority"), &Area3D::set_priority);
  563. ClassDB::bind_method(D_METHOD("get_priority"), &Area3D::get_priority);
  564. ClassDB::bind_method(D_METHOD("set_wind_force_magnitude", "wind_force_magnitude"), &Area3D::set_wind_force_magnitude);
  565. ClassDB::bind_method(D_METHOD("get_wind_force_magnitude"), &Area3D::get_wind_force_magnitude);
  566. ClassDB::bind_method(D_METHOD("set_wind_attenuation_factor", "wind_attenuation_factor"), &Area3D::set_wind_attenuation_factor);
  567. ClassDB::bind_method(D_METHOD("get_wind_attenuation_factor"), &Area3D::get_wind_attenuation_factor);
  568. ClassDB::bind_method(D_METHOD("set_wind_source_path", "wind_source_path"), &Area3D::set_wind_source_path);
  569. ClassDB::bind_method(D_METHOD("get_wind_source_path"), &Area3D::get_wind_source_path);
  570. ClassDB::bind_method(D_METHOD("set_monitorable", "enable"), &Area3D::set_monitorable);
  571. ClassDB::bind_method(D_METHOD("is_monitorable"), &Area3D::is_monitorable);
  572. ClassDB::bind_method(D_METHOD("set_monitoring", "enable"), &Area3D::set_monitoring);
  573. ClassDB::bind_method(D_METHOD("is_monitoring"), &Area3D::is_monitoring);
  574. ClassDB::bind_method(D_METHOD("get_overlapping_bodies"), &Area3D::get_overlapping_bodies);
  575. ClassDB::bind_method(D_METHOD("get_overlapping_areas"), &Area3D::get_overlapping_areas);
  576. ClassDB::bind_method(D_METHOD("has_overlapping_bodies"), &Area3D::has_overlapping_bodies);
  577. ClassDB::bind_method(D_METHOD("has_overlapping_areas"), &Area3D::has_overlapping_areas);
  578. ClassDB::bind_method(D_METHOD("overlaps_body", "body"), &Area3D::overlaps_body);
  579. ClassDB::bind_method(D_METHOD("overlaps_area", "area"), &Area3D::overlaps_area);
  580. ClassDB::bind_method(D_METHOD("set_audio_bus_override", "enable"), &Area3D::set_audio_bus_override);
  581. ClassDB::bind_method(D_METHOD("is_overriding_audio_bus"), &Area3D::is_overriding_audio_bus);
  582. ClassDB::bind_method(D_METHOD("set_audio_bus_name", "name"), &Area3D::set_audio_bus_name);
  583. ClassDB::bind_method(D_METHOD("get_audio_bus_name"), &Area3D::get_audio_bus_name);
  584. ClassDB::bind_method(D_METHOD("set_use_reverb_bus", "enable"), &Area3D::set_use_reverb_bus);
  585. ClassDB::bind_method(D_METHOD("is_using_reverb_bus"), &Area3D::is_using_reverb_bus);
  586. ClassDB::bind_method(D_METHOD("set_reverb_bus_name", "name"), &Area3D::set_reverb_bus_name);
  587. ClassDB::bind_method(D_METHOD("get_reverb_bus_name"), &Area3D::get_reverb_bus_name);
  588. ClassDB::bind_method(D_METHOD("set_reverb_amount", "amount"), &Area3D::set_reverb_amount);
  589. ClassDB::bind_method(D_METHOD("get_reverb_amount"), &Area3D::get_reverb_amount);
  590. ClassDB::bind_method(D_METHOD("set_reverb_uniformity", "amount"), &Area3D::set_reverb_uniformity);
  591. ClassDB::bind_method(D_METHOD("get_reverb_uniformity"), &Area3D::get_reverb_uniformity);
  592. ADD_SIGNAL(MethodInfo("body_shape_entered", PropertyInfo(Variant::RID, "body_rid"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node3D"), PropertyInfo(Variant::INT, "body_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  593. ADD_SIGNAL(MethodInfo("body_shape_exited", PropertyInfo(Variant::RID, "body_rid"), PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node3D"), PropertyInfo(Variant::INT, "body_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  594. ADD_SIGNAL(MethodInfo("body_entered", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node3D")));
  595. ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node3D")));
  596. ADD_SIGNAL(MethodInfo("area_shape_entered", PropertyInfo(Variant::RID, "area_rid"), PropertyInfo(Variant::OBJECT, "area", PROPERTY_HINT_RESOURCE_TYPE, "Area3D"), PropertyInfo(Variant::INT, "area_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  597. ADD_SIGNAL(MethodInfo("area_shape_exited", PropertyInfo(Variant::RID, "area_rid"), PropertyInfo(Variant::OBJECT, "area", PROPERTY_HINT_RESOURCE_TYPE, "Area3D"), PropertyInfo(Variant::INT, "area_shape_index"), PropertyInfo(Variant::INT, "local_shape_index")));
  598. ADD_SIGNAL(MethodInfo("area_entered", PropertyInfo(Variant::OBJECT, "area", PROPERTY_HINT_RESOURCE_TYPE, "Area3D")));
  599. ADD_SIGNAL(MethodInfo("area_exited", PropertyInfo(Variant::OBJECT, "area", PROPERTY_HINT_RESOURCE_TYPE, "Area3D")));
  600. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitoring"), "set_monitoring", "is_monitoring");
  601. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "monitorable"), "set_monitorable", "is_monitorable");
  602. ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,100000,1,or_greater,or_less"), "set_priority", "get_priority");
  603. ADD_GROUP("Gravity", "gravity_");
  604. ADD_PROPERTY(PropertyInfo(Variant::INT, "gravity_space_override", PROPERTY_HINT_ENUM, "Disabled,Combine,Combine-Replace,Replace,Replace-Combine", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_gravity_space_override_mode", "get_gravity_space_override_mode");
  605. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gravity_point", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_gravity_is_point", "is_gravity_a_point");
  606. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity_point_unit_distance", PROPERTY_HINT_RANGE, "0,1024,0.001,or_greater,exp,suffix:m"), "set_gravity_point_unit_distance", "get_gravity_point_unit_distance");
  607. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity_point_center", PROPERTY_HINT_NONE, "suffix:m"), "set_gravity_point_center", "get_gravity_point_center");
  608. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "gravity_direction"), "set_gravity_direction", "get_gravity_direction");
  609. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "gravity", PROPERTY_HINT_RANGE, U"-32,32,0.001,or_less,or_greater,suffix:m/s\u00B2"), "set_gravity", "get_gravity");
  610. ADD_GROUP("Linear Damp", "linear_damp_");
  611. ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_space_override", PROPERTY_HINT_ENUM, "Disabled,Combine,Combine-Replace,Replace,Replace-Combine", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_linear_damp_space_override_mode", "get_linear_damp_space_override_mode");
  612. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
  613. ADD_GROUP("Angular Damp", "angular_damp_");
  614. ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_space_override", PROPERTY_HINT_ENUM, "Disabled,Combine,Combine-Replace,Replace,Replace-Combine", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_angular_damp_space_override_mode", "get_angular_damp_space_override_mode");
  615. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp", PROPERTY_HINT_RANGE, "0,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
  616. ADD_GROUP("Wind", "wind_");
  617. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wind_force_magnitude", PROPERTY_HINT_RANGE, "0,10,0.001,or_greater"), "set_wind_force_magnitude", "get_wind_force_magnitude");
  618. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wind_attenuation_factor", PROPERTY_HINT_RANGE, "0.0,3.0,0.001,or_greater"), "set_wind_attenuation_factor", "get_wind_attenuation_factor");
  619. ADD_PROPERTY(PropertyInfo(Variant::NODE_PATH, "wind_source_path", PROPERTY_HINT_NODE_PATH_VALID_TYPES, "Node3D"), "set_wind_source_path", "get_wind_source_path");
  620. ADD_GROUP("Audio Bus", "audio_bus_");
  621. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "audio_bus_override"), "set_audio_bus_override", "is_overriding_audio_bus");
  622. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "audio_bus_name", PROPERTY_HINT_ENUM, ""), "set_audio_bus_name", "get_audio_bus_name");
  623. ADD_GROUP("Reverb Bus", "reverb_bus_");
  624. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reverb_bus_enabled"), "set_use_reverb_bus", "is_using_reverb_bus");
  625. ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "reverb_bus_name", PROPERTY_HINT_ENUM, ""), "set_reverb_bus_name", "get_reverb_bus_name");
  626. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "reverb_bus_amount", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_reverb_amount", "get_reverb_amount");
  627. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "reverb_bus_uniformity", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_reverb_uniformity", "get_reverb_uniformity");
  628. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_DISABLED);
  629. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_COMBINE);
  630. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_COMBINE_REPLACE);
  631. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_REPLACE);
  632. BIND_ENUM_CONSTANT(SPACE_OVERRIDE_REPLACE_COMBINE);
  633. }
  634. Area3D::Area3D() :
  635. CollisionObject3D(PhysicsServer3D::get_singleton()->area_create(), true) {
  636. set_gravity(9.8);
  637. set_gravity_direction(Vector3(0, -1, 0));
  638. set_monitoring(true);
  639. set_monitorable(true);
  640. }
  641. Area3D::~Area3D() {
  642. }