From 5f24a3c0c7630b2bad530aab7deeaa9468c59fd9 Mon Sep 17 00:00:00 2001 From: DS Date: Sun, 20 Nov 2022 21:27:47 +0100 Subject: [PATCH] Object selection: Improve distance checks (#12974) --- src/client/activeobjectmgr.cpp | 41 ++++++++++++++++++++++++++++++++ src/client/activeobjectmgr.h | 7 ++++++ src/client/clientenvironment.cpp | 3 +-- 3 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/client/activeobjectmgr.cpp b/src/client/activeobjectmgr.cpp index 82f3cb944..3f5178908 100644 --- a/src/client/activeobjectmgr.cpp +++ b/src/client/activeobjectmgr.cpp @@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include #include #include "profiler.h" #include "activeobjectmgr.h" @@ -106,4 +107,44 @@ void ActiveObjectMgr::getActiveObjects(const v3f &origin, f32 max_d, } } +void ActiveObjectMgr::getActiveSelectableObjects(const core::line3d &shootline, + std::vector &dest) +{ + // Imagine a not-axis-aligned cuboid oriented into the direction of the shootline, + // with the width of the object's selection box radius * 2 and with length of the + // shootline (+selection box radius forwards and backwards). We check whether + // the selection box center is inside this cuboid. + + f32 max_d = shootline.getLength(); + v3f dir = shootline.getVector().normalize(); + v3f dir_ortho1 = dir.crossProduct(dir + v3f(1,0,0)).normalize(); + v3f dir_ortho2 = dir.crossProduct(dir_ortho1); + + for (auto &ao_it : m_active_objects) { + ClientActiveObject *obj = ao_it.second; + + aabb3f selection_box; + if (!obj->getSelectionBox(&selection_box)) + continue; + + // possible optimization: get rid of the sqrt here + f32 selection_box_radius = selection_box.getRadius(); + + v3f pos_diff = obj->getPosition() + selection_box.getCenter() - shootline.start; + + f32 d = dir.dotProduct(pos_diff); + + // backward- and far-plane + if (d + selection_box_radius < 0.0f || d - selection_box_radius > max_d) + continue; + + // side-planes + if (std::fabs(dir_ortho1.dotProduct(pos_diff)) > selection_box_radius + || std::fabs(dir_ortho2.dotProduct(pos_diff)) > selection_box_radius) + continue; + + dest.emplace_back(obj, d); + } +} + } // namespace client diff --git a/src/client/activeobjectmgr.h b/src/client/activeobjectmgr.h index 510b2d6e3..78147abd4 100644 --- a/src/client/activeobjectmgr.h +++ b/src/client/activeobjectmgr.h @@ -37,5 +37,12 @@ public: void getActiveObjects(const v3f &origin, f32 max_d, std::vector &dest); + // Similar to above, but takes selection box sizes, and line direction into + // account. + // Objects without selectionbox are not returned. + // Returned distances are in direction of shootline. + // Distance check is coarse. + void getActiveSelectableObjects(const core::line3d &shootline, + std::vector &dest); }; } // namespace client diff --git a/src/client/clientenvironment.cpp b/src/client/clientenvironment.cpp index 1ce443bcc..0070fa82f 100644 --- a/src/client/clientenvironment.cpp +++ b/src/client/clientenvironment.cpp @@ -495,8 +495,7 @@ void ClientEnvironment::getSelectedActiveObjects( std::vector &objects) { std::vector allObjects; - getActiveObjects(shootline_on_map.start, - shootline_on_map.getLength() + 10.0f, allObjects); + m_ao_manager.getActiveSelectableObjects(shootline_on_map, allObjects); const v3f line_vector = shootline_on_map.getVector(); for (const auto &allObject : allObjects) {