jolt_area_3d.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /**************************************************************************/
  2. /* jolt_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 "jolt_area_3d.h"
  31. #include "../jolt_project_settings.h"
  32. #include "../misc/jolt_math_funcs.h"
  33. #include "../misc/jolt_type_conversions.h"
  34. #include "../shapes/jolt_shape_3d.h"
  35. #include "../spaces/jolt_broad_phase_layer.h"
  36. #include "../spaces/jolt_space_3d.h"
  37. #include "jolt_body_3d.h"
  38. #include "jolt_group_filter.h"
  39. #include "jolt_soft_body_3d.h"
  40. namespace {
  41. constexpr double AREA_DEFAULT_WIND_MAGNITUDE = 0.0;
  42. constexpr double AREA_DEFAULT_WIND_ATTENUATION = 0.0;
  43. const Vector3 AREA_DEFAULT_WIND_SOURCE = Vector3();
  44. const Vector3 AREA_DEFAULT_WIND_DIRECTION = Vector3();
  45. } // namespace
  46. JPH::BroadPhaseLayer JoltArea3D::_get_broad_phase_layer() const {
  47. return monitorable ? JoltBroadPhaseLayer::AREA_DETECTABLE : JoltBroadPhaseLayer::AREA_UNDETECTABLE;
  48. }
  49. JPH::ObjectLayer JoltArea3D::_get_object_layer() const {
  50. ERR_FAIL_NULL_V(space, 0);
  51. if (jolt_shape == nullptr || jolt_shape->GetType() == JPH::EShapeType::Empty) {
  52. // No point doing collision checks against a shapeless object.
  53. return space->map_to_object_layer(_get_broad_phase_layer(), 0, 0);
  54. }
  55. return space->map_to_object_layer(_get_broad_phase_layer(), collision_layer, collision_mask);
  56. }
  57. void JoltArea3D::_add_to_space() {
  58. jolt_shape = build_shapes(true);
  59. JPH::CollisionGroup::GroupID group_id = 0;
  60. JPH::CollisionGroup::SubGroupID sub_group_id = 0;
  61. JoltGroupFilter::encode_object(this, group_id, sub_group_id);
  62. jolt_settings->mUserData = reinterpret_cast<JPH::uint64>(this);
  63. jolt_settings->mObjectLayer = _get_object_layer();
  64. jolt_settings->mCollisionGroup = JPH::CollisionGroup(nullptr, group_id, sub_group_id);
  65. jolt_settings->mMotionType = _get_motion_type();
  66. jolt_settings->mIsSensor = true;
  67. jolt_settings->mCollideKinematicVsNonDynamic = true;
  68. jolt_settings->mUseManifoldReduction = false;
  69. jolt_settings->mOverrideMassProperties = JPH::EOverrideMassProperties::MassAndInertiaProvided;
  70. jolt_settings->mMassPropertiesOverride.mMass = 1.0f;
  71. jolt_settings->mMassPropertiesOverride.mInertia = JPH::Mat44::sIdentity();
  72. jolt_settings->SetShape(jolt_shape);
  73. JPH::Body *new_jolt_body = space->add_object(*this, *jolt_settings, _should_sleep());
  74. if (new_jolt_body == nullptr) {
  75. return;
  76. }
  77. jolt_body = new_jolt_body;
  78. delete jolt_settings;
  79. jolt_settings = nullptr;
  80. }
  81. void JoltArea3D::_enqueue_call_queries() {
  82. if (space != nullptr) {
  83. space->enqueue_call_queries(&call_queries_element);
  84. }
  85. }
  86. void JoltArea3D::_dequeue_call_queries() {
  87. if (space != nullptr) {
  88. space->dequeue_call_queries(&call_queries_element);
  89. }
  90. }
  91. void JoltArea3D::_add_shape_pair(Overlap &p_overlap, const JPH::BodyID &p_body_id, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
  92. const JoltShapedObject3D *other_object = space->try_get_shaped(p_body_id);
  93. ERR_FAIL_NULL(other_object);
  94. p_overlap.rid = other_object->get_rid();
  95. p_overlap.instance_id = other_object->get_instance_id();
  96. HashMap<ShapeIDPair, ShapeIndexPair, ShapeIDPair>::Iterator shape_pair = p_overlap.shape_pairs.find(ShapeIDPair(p_other_shape_id, p_self_shape_id));
  97. if (shape_pair == p_overlap.shape_pairs.end()) {
  98. const int other_shape_index = other_object->find_shape_index(p_other_shape_id);
  99. const int self_shape_index = find_shape_index(p_self_shape_id);
  100. shape_pair = p_overlap.shape_pairs.insert(ShapeIDPair(p_other_shape_id, p_self_shape_id), ShapeIndexPair(other_shape_index, self_shape_index));
  101. }
  102. p_overlap.pending_added.push_back(shape_pair->value);
  103. _events_changed();
  104. }
  105. bool JoltArea3D::_remove_shape_pair(Overlap &p_overlap, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
  106. HashMap<ShapeIDPair, ShapeIndexPair, ShapeIDPair>::Iterator shape_pair = p_overlap.shape_pairs.find(ShapeIDPair(p_other_shape_id, p_self_shape_id));
  107. if (shape_pair == p_overlap.shape_pairs.end()) {
  108. return false;
  109. }
  110. p_overlap.pending_removed.push_back(shape_pair->value);
  111. p_overlap.shape_pairs.remove(shape_pair);
  112. _events_changed();
  113. return true;
  114. }
  115. void JoltArea3D::_flush_events(OverlapsById &p_objects, const Callable &p_callback) {
  116. for (OverlapsById::Iterator E = p_objects.begin(); E;) {
  117. Overlap &overlap = E->value;
  118. if (p_callback.is_valid()) {
  119. for (const ShapeIndexPair &shape_indices : overlap.pending_added) {
  120. int &ref_count = overlap.ref_counts[shape_indices];
  121. if (ref_count++ == 0) {
  122. _report_event(p_callback, PhysicsServer3D::AREA_BODY_ADDED, overlap.rid, overlap.instance_id, shape_indices.other, shape_indices.self);
  123. }
  124. }
  125. for (const ShapeIndexPair &shape_indices : overlap.pending_removed) {
  126. int &ref_count = overlap.ref_counts[shape_indices];
  127. ERR_CONTINUE(ref_count <= 0);
  128. if (--ref_count == 0) {
  129. _report_event(p_callback, PhysicsServer3D::AREA_BODY_REMOVED, overlap.rid, overlap.instance_id, shape_indices.other, shape_indices.self);
  130. overlap.ref_counts.erase(shape_indices);
  131. }
  132. }
  133. }
  134. overlap.pending_removed.clear();
  135. overlap.pending_added.clear();
  136. OverlapsById::Iterator next = E;
  137. ++next;
  138. if (overlap.shape_pairs.is_empty()) {
  139. p_objects.remove(E);
  140. }
  141. E = next;
  142. }
  143. }
  144. void JoltArea3D::_report_event(const Callable &p_callback, PhysicsServer3D::AreaBodyStatus p_status, const RID &p_other_rid, ObjectID p_other_instance_id, int p_other_shape_index, int p_self_shape_index) const {
  145. ERR_FAIL_COND(!p_callback.is_valid());
  146. const Variant arg1 = p_status;
  147. const Variant arg2 = p_other_rid;
  148. const Variant arg3 = p_other_instance_id;
  149. const Variant arg4 = p_other_shape_index;
  150. const Variant arg5 = p_self_shape_index;
  151. const Variant *args[5] = { &arg1, &arg2, &arg3, &arg4, &arg5 };
  152. Callable::CallError ce;
  153. Variant ret;
  154. p_callback.callp(args, 5, ret, ce);
  155. if (unlikely(ce.error != Callable::CallError::CALL_OK)) {
  156. ERR_PRINT_ONCE(vformat("Failed to call area monitor callback for '%s'. It returned the following error: '%s'.", to_string(), Variant::get_callable_error_text(p_callback, args, 5, ce)));
  157. }
  158. }
  159. void JoltArea3D::_notify_body_entered(const JPH::BodyID &p_body_id) {
  160. if (JoltBody3D *other_body = space->try_get_body(p_body_id)) {
  161. other_body->add_area(this);
  162. }
  163. }
  164. void JoltArea3D::_notify_body_exited(const JPH::BodyID &p_body_id) {
  165. if (JoltBody3D *other_body = space->try_get_body(p_body_id)) {
  166. other_body->remove_area(this);
  167. }
  168. }
  169. void JoltArea3D::_remove_all_overlaps() {
  170. for (KeyValue<JPH::BodyID, Overlap> &E : bodies_by_id) {
  171. _notify_body_exited(E.key);
  172. }
  173. bodies_by_id.clear();
  174. areas_by_id.clear();
  175. }
  176. void JoltArea3D::_update_sleeping() {
  177. if (!in_space()) {
  178. return;
  179. }
  180. space->set_is_object_sleeping(jolt_body->GetID(), _should_sleep());
  181. }
  182. void JoltArea3D::_update_group_filter() {
  183. if (!in_space()) {
  184. return;
  185. }
  186. jolt_body->GetCollisionGroup().SetGroupFilter(JoltGroupFilter::instance);
  187. }
  188. void JoltArea3D::_update_default_gravity() {
  189. if (is_default_area()) {
  190. space->get_physics_system().SetGravity(to_jolt(gravity_vector) * gravity);
  191. }
  192. }
  193. void JoltArea3D::_space_changing() {
  194. JoltShapedObject3D::_space_changing();
  195. _remove_all_overlaps();
  196. _dequeue_call_queries();
  197. }
  198. void JoltArea3D::_space_changed() {
  199. JoltShapedObject3D::_space_changed();
  200. _update_group_filter();
  201. _update_default_gravity();
  202. }
  203. void JoltArea3D::_events_changed() {
  204. _enqueue_call_queries();
  205. }
  206. void JoltArea3D::_body_monitoring_changed() {
  207. _update_sleeping();
  208. }
  209. void JoltArea3D::_area_monitoring_changed() {
  210. _update_sleeping();
  211. }
  212. void JoltArea3D::_monitorable_changed() {
  213. _update_object_layer();
  214. }
  215. void JoltArea3D::_gravity_changed() {
  216. _update_default_gravity();
  217. }
  218. JoltArea3D::JoltArea3D() :
  219. JoltShapedObject3D(OBJECT_TYPE_AREA),
  220. call_queries_element(this) {
  221. }
  222. bool JoltArea3D::is_default_area() const {
  223. return space != nullptr && space->get_default_area() == this;
  224. }
  225. void JoltArea3D::set_default_area(bool p_value) {
  226. if (p_value) {
  227. _update_default_gravity();
  228. }
  229. }
  230. void JoltArea3D::set_transform(Transform3D p_transform) {
  231. JOLT_ENSURE_SCALE_NOT_ZERO(p_transform, vformat("An invalid transform was passed to area '%s'.", to_string()));
  232. Vector3 new_scale;
  233. JoltMath::decompose(p_transform, new_scale);
  234. // Ideally we would do an exact comparison here, but due to floating-point precision this would be invalidated very often.
  235. if (!scale.is_equal_approx(new_scale)) {
  236. scale = new_scale;
  237. _shapes_changed();
  238. }
  239. if (!in_space()) {
  240. jolt_settings->mPosition = to_jolt_r(p_transform.origin);
  241. jolt_settings->mRotation = to_jolt(p_transform.basis);
  242. } else {
  243. space->get_body_iface().SetPositionAndRotation(jolt_body->GetID(), to_jolt_r(p_transform.origin), to_jolt(p_transform.basis), JPH::EActivation::DontActivate);
  244. }
  245. }
  246. Variant JoltArea3D::get_param(PhysicsServer3D::AreaParameter p_param) const {
  247. switch (p_param) {
  248. case PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE: {
  249. return get_gravity_mode();
  250. }
  251. case PhysicsServer3D::AREA_PARAM_GRAVITY: {
  252. return get_gravity();
  253. }
  254. case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR: {
  255. return get_gravity_vector();
  256. }
  257. case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT: {
  258. return is_point_gravity();
  259. }
  260. case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE: {
  261. return get_point_gravity_distance();
  262. }
  263. case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE: {
  264. return get_linear_damp_mode();
  265. }
  266. case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP: {
  267. return get_linear_damp();
  268. }
  269. case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE: {
  270. return get_angular_damp_mode();
  271. }
  272. case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP: {
  273. return get_angular_damp();
  274. }
  275. case PhysicsServer3D::AREA_PARAM_PRIORITY: {
  276. return get_priority();
  277. }
  278. case PhysicsServer3D::AREA_PARAM_WIND_FORCE_MAGNITUDE: {
  279. return AREA_DEFAULT_WIND_MAGNITUDE;
  280. }
  281. case PhysicsServer3D::AREA_PARAM_WIND_SOURCE: {
  282. return AREA_DEFAULT_WIND_SOURCE;
  283. }
  284. case PhysicsServer3D::AREA_PARAM_WIND_DIRECTION: {
  285. return AREA_DEFAULT_WIND_DIRECTION;
  286. }
  287. case PhysicsServer3D::AREA_PARAM_WIND_ATTENUATION_FACTOR: {
  288. return AREA_DEFAULT_WIND_ATTENUATION;
  289. }
  290. default: {
  291. ERR_FAIL_V_MSG(Variant(), vformat("Unhandled area parameter: '%d'. This should not happen. Please report this.", p_param));
  292. }
  293. }
  294. }
  295. void JoltArea3D::set_param(PhysicsServer3D::AreaParameter p_param, const Variant &p_value) {
  296. switch (p_param) {
  297. case PhysicsServer3D::AREA_PARAM_GRAVITY_OVERRIDE_MODE: {
  298. set_gravity_mode((OverrideMode)(int)p_value);
  299. } break;
  300. case PhysicsServer3D::AREA_PARAM_GRAVITY: {
  301. set_gravity(p_value);
  302. } break;
  303. case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR: {
  304. set_gravity_vector(p_value);
  305. } break;
  306. case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT: {
  307. set_point_gravity(p_value);
  308. } break;
  309. case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE: {
  310. set_point_gravity_distance(p_value);
  311. } break;
  312. case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE: {
  313. set_linear_damp_mode((OverrideMode)(int)p_value);
  314. } break;
  315. case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP: {
  316. set_area_linear_damp(p_value);
  317. } break;
  318. case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE: {
  319. set_angular_damp_mode((OverrideMode)(int)p_value);
  320. } break;
  321. case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP: {
  322. set_area_angular_damp(p_value);
  323. } break;
  324. case PhysicsServer3D::AREA_PARAM_PRIORITY: {
  325. set_priority(p_value);
  326. } break;
  327. case PhysicsServer3D::AREA_PARAM_WIND_FORCE_MAGNITUDE: {
  328. if (!Math::is_equal_approx((double)p_value, AREA_DEFAULT_WIND_MAGNITUDE)) {
  329. WARN_PRINT(vformat("Invalid wind force magnitude for '%s'. Area wind force magnitude is not supported when using Jolt Physics. Any such value will be ignored.", to_string()));
  330. }
  331. } break;
  332. case PhysicsServer3D::AREA_PARAM_WIND_SOURCE: {
  333. if (!((Vector3)p_value).is_equal_approx(AREA_DEFAULT_WIND_SOURCE)) {
  334. WARN_PRINT(vformat("Invalid wind source for '%s'. Area wind source is not supported when using Jolt Physics. Any such value will be ignored.", to_string()));
  335. }
  336. } break;
  337. case PhysicsServer3D::AREA_PARAM_WIND_DIRECTION: {
  338. if (!((Vector3)p_value).is_equal_approx(AREA_DEFAULT_WIND_DIRECTION)) {
  339. WARN_PRINT(vformat("Invalid wind direction for '%s'. Area wind direction is not supported when using Jolt Physics. Any such value will be ignored.", to_string()));
  340. }
  341. } break;
  342. case PhysicsServer3D::AREA_PARAM_WIND_ATTENUATION_FACTOR: {
  343. if (!Math::is_equal_approx((double)p_value, AREA_DEFAULT_WIND_ATTENUATION)) {
  344. WARN_PRINT(vformat("Invalid wind attenuation for '%s'. Area wind attenuation is not supported when using Jolt Physics. Any such value will be ignored.", to_string()));
  345. }
  346. } break;
  347. default: {
  348. ERR_FAIL_MSG(vformat("Unhandled area parameter: '%d'. This should not happen. Please report this.", p_param));
  349. } break;
  350. }
  351. }
  352. void JoltArea3D::set_body_monitor_callback(const Callable &p_callback) {
  353. if (p_callback == body_monitor_callback) {
  354. return;
  355. }
  356. body_monitor_callback = p_callback;
  357. _body_monitoring_changed();
  358. }
  359. void JoltArea3D::set_area_monitor_callback(const Callable &p_callback) {
  360. if (p_callback == area_monitor_callback) {
  361. return;
  362. }
  363. area_monitor_callback = p_callback;
  364. _area_monitoring_changed();
  365. }
  366. void JoltArea3D::set_monitorable(bool p_monitorable) {
  367. if (p_monitorable == monitorable) {
  368. return;
  369. }
  370. monitorable = p_monitorable;
  371. _monitorable_changed();
  372. }
  373. bool JoltArea3D::can_monitor(const JoltBody3D &p_other) const {
  374. return is_monitoring_bodies() && (collision_mask & p_other.get_collision_layer()) != 0;
  375. }
  376. bool JoltArea3D::can_monitor(const JoltSoftBody3D &p_other) const {
  377. return false;
  378. }
  379. bool JoltArea3D::can_monitor(const JoltArea3D &p_other) const {
  380. return is_monitoring_areas() && p_other.is_monitorable() && (collision_mask & p_other.get_collision_layer()) != 0;
  381. }
  382. bool JoltArea3D::can_interact_with(const JoltBody3D &p_other) const {
  383. return can_monitor(p_other);
  384. }
  385. bool JoltArea3D::can_interact_with(const JoltSoftBody3D &p_other) const {
  386. return false;
  387. }
  388. bool JoltArea3D::can_interact_with(const JoltArea3D &p_other) const {
  389. return can_monitor(p_other) || p_other.can_monitor(*this);
  390. }
  391. Vector3 JoltArea3D::get_velocity_at_position(const Vector3 &p_position) const {
  392. return Vector3();
  393. }
  394. void JoltArea3D::set_point_gravity(bool p_enabled) {
  395. if (point_gravity == p_enabled) {
  396. return;
  397. }
  398. point_gravity = p_enabled;
  399. _gravity_changed();
  400. }
  401. void JoltArea3D::set_gravity(float p_gravity) {
  402. if (gravity == p_gravity) {
  403. return;
  404. }
  405. gravity = p_gravity;
  406. _gravity_changed();
  407. }
  408. void JoltArea3D::set_point_gravity_distance(float p_distance) {
  409. if (point_gravity_distance == p_distance) {
  410. return;
  411. }
  412. point_gravity_distance = p_distance;
  413. _gravity_changed();
  414. }
  415. void JoltArea3D::set_gravity_mode(OverrideMode p_mode) {
  416. if (gravity_mode == p_mode) {
  417. return;
  418. }
  419. gravity_mode = p_mode;
  420. _gravity_changed();
  421. }
  422. void JoltArea3D::set_gravity_vector(const Vector3 &p_vector) {
  423. if (gravity_vector == p_vector) {
  424. return;
  425. }
  426. gravity_vector = p_vector;
  427. _gravity_changed();
  428. }
  429. Vector3 JoltArea3D::compute_gravity(const Vector3 &p_position) const {
  430. if (!point_gravity) {
  431. return gravity_vector * gravity;
  432. }
  433. const Vector3 point = get_transform_scaled().xform(gravity_vector);
  434. const Vector3 to_point = point - p_position;
  435. const real_t to_point_dist_sq = MAX(to_point.length_squared(), (real_t)CMP_EPSILON);
  436. const Vector3 to_point_dir = to_point / Math::sqrt(to_point_dist_sq);
  437. if (point_gravity_distance == 0.0f) {
  438. return to_point_dir * gravity;
  439. }
  440. const float gravity_dist_sq = point_gravity_distance * point_gravity_distance;
  441. return to_point_dir * (gravity * gravity_dist_sq / to_point_dist_sq);
  442. }
  443. void JoltArea3D::body_shape_entered(const JPH::BodyID &p_body_id, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
  444. Overlap &overlap = bodies_by_id[p_body_id];
  445. if (overlap.shape_pairs.is_empty()) {
  446. _notify_body_entered(p_body_id);
  447. }
  448. _add_shape_pair(overlap, p_body_id, p_other_shape_id, p_self_shape_id);
  449. }
  450. bool JoltArea3D::body_shape_exited(const JPH::BodyID &p_body_id, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
  451. Overlap *overlap = bodies_by_id.getptr(p_body_id);
  452. if (overlap == nullptr) {
  453. return false;
  454. }
  455. if (!_remove_shape_pair(*overlap, p_other_shape_id, p_self_shape_id)) {
  456. return false;
  457. }
  458. if (overlap->shape_pairs.is_empty()) {
  459. _notify_body_exited(p_body_id);
  460. }
  461. return true;
  462. }
  463. void JoltArea3D::area_shape_entered(const JPH::BodyID &p_body_id, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
  464. _add_shape_pair(areas_by_id[p_body_id], p_body_id, p_other_shape_id, p_self_shape_id);
  465. }
  466. bool JoltArea3D::area_shape_exited(const JPH::BodyID &p_body_id, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
  467. Overlap *overlap = areas_by_id.getptr(p_body_id);
  468. if (overlap == nullptr) {
  469. return false;
  470. }
  471. return _remove_shape_pair(*overlap, p_other_shape_id, p_self_shape_id);
  472. }
  473. bool JoltArea3D::shape_exited(const JPH::BodyID &p_body_id, const JPH::SubShapeID &p_other_shape_id, const JPH::SubShapeID &p_self_shape_id) {
  474. return body_shape_exited(p_body_id, p_other_shape_id, p_self_shape_id) || area_shape_exited(p_body_id, p_other_shape_id, p_self_shape_id);
  475. }
  476. void JoltArea3D::body_exited(const JPH::BodyID &p_body_id, bool p_notify) {
  477. Overlap *overlap = bodies_by_id.getptr(p_body_id);
  478. if (unlikely(overlap == nullptr)) {
  479. return;
  480. }
  481. if (unlikely(overlap->shape_pairs.is_empty())) {
  482. return;
  483. }
  484. for (const KeyValue<ShapeIDPair, ShapeIndexPair> &E : overlap->shape_pairs) {
  485. overlap->pending_added.erase(E.value);
  486. overlap->pending_removed.push_back(E.value);
  487. }
  488. _events_changed();
  489. overlap->shape_pairs.clear();
  490. if (p_notify) {
  491. _notify_body_exited(p_body_id);
  492. }
  493. }
  494. void JoltArea3D::area_exited(const JPH::BodyID &p_body_id) {
  495. Overlap *overlap = areas_by_id.getptr(p_body_id);
  496. if (unlikely(overlap == nullptr)) {
  497. return;
  498. }
  499. if (unlikely(overlap->shape_pairs.is_empty())) {
  500. return;
  501. }
  502. for (const KeyValue<ShapeIDPair, ShapeIndexPair> &E : overlap->shape_pairs) {
  503. overlap->pending_added.erase(E.value);
  504. overlap->pending_removed.push_back(E.value);
  505. }
  506. _events_changed();
  507. overlap->shape_pairs.clear();
  508. }
  509. void JoltArea3D::call_queries() {
  510. _flush_events(bodies_by_id, body_monitor_callback);
  511. _flush_events(areas_by_id, area_monitor_callback);
  512. }