diff options
| author | DS <vorunbekannt75@web.de> | 2022-11-20 21:27:47 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-20 21:27:47 +0100 |
| commit | 5f24a3c0c7630b2bad530aab7deeaa9468c59fd9 (patch) | |
| tree | d67c3640bbc28bbd8e814cded679664540340ac9 /src | |
| parent | 475005012a0e34285e6301dca057720694c7ff4e (diff) | |
| download | minetest-5f24a3c0c7630b2bad530aab7deeaa9468c59fd9.tar.xz | |
Object selection: Improve distance checks (#12974)
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/activeobjectmgr.cpp | 41 | ||||
| -rw-r--r-- | src/client/activeobjectmgr.h | 7 | ||||
| -rw-r--r-- | 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 <cmath> #include <log.h> #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<f32> &shootline, + std::vector<DistanceSortedActiveObject> &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<DistanceSortedActiveObject> &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<f32> &shootline, + std::vector<DistanceSortedActiveObject> &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<PointedThing> &objects) { std::vector<DistanceSortedActiveObject> 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) { |
