]> git.lizzy.rs Git - minetest.git/commitdiff
Object selection: Improve distance checks (#12974)
authorDS <vorunbekannt75@web.de>
Sun, 20 Nov 2022 20:27:47 +0000 (21:27 +0100)
committerGitHub <noreply@github.com>
Sun, 20 Nov 2022 20:27:47 +0000 (21:27 +0100)
src/client/activeobjectmgr.cpp
src/client/activeobjectmgr.h
src/client/clientenvironment.cpp

index 82f3cb9447921cbf3e4596cdb51ce94f7b24321d..3f51789080c4d058bd03bebaa0a804043a71d738 100644 (file)
@@ -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
index 510b2d6e3a214796cfecedc3c3ab87ede698b096..78147abd451faee3fec787020705d4874bd4366b 100644 (file)
@@ -37,5 +37,12 @@ class ActiveObjectMgr : public ::ActiveObjectMgr<ClientActiveObject>
 
        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
index 1ce443bccc6f509e064d0bd087fb0ca68310a8c0..0070fa82f95ce2d9311489071214f03e866f9c09 100644 (file)
@@ -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) {