area_3d.cpp 32 KB

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