Fix bone-attached entities (#10015)
[oweals/minetest.git] / src / raycast.cpp
index 42cc22587d69984d70ebd06d1af6c2c6db716e1d..ebc40235db815042b8d785732af2a9720a5230c1 100644 (file)
@@ -28,16 +28,28 @@ bool RaycastSort::operator() (const PointedThing &pt1,
        // "nothing" can not be sorted
        assert(pt1.type != POINTEDTHING_NOTHING);
        assert(pt2.type != POINTEDTHING_NOTHING);
+       f32 pt1_distSq = pt1.distanceSq;
+
+       // Add some bonus when one of them is an object
+       if (pt1.type != pt2.type) {
+               if (pt1.type == POINTEDTHING_OBJECT)
+                       pt1_distSq -= BS * BS;
+               else if (pt2.type == POINTEDTHING_OBJECT)
+                       pt1_distSq += BS * BS;
+       }
+
        // returns false if pt1 is nearer than pt2
-       if (pt1.distanceSq < pt2.distanceSq) {
+       if (pt1_distSq < pt2.distanceSq) {
                return false;
-       } else if (pt1.distanceSq == pt2.distanceSq) {
+       }
+
+       if (pt1_distSq == pt2.distanceSq) {
                // Sort them to allow only one order
                if (pt1.type == POINTEDTHING_OBJECT)
                        return (pt2.type == POINTEDTHING_OBJECT
                                && pt1.object_id < pt2.object_id);
-               else
-                       return (pt2.type == POINTEDTHING_OBJECT
+
+               return (pt2.type == POINTEDTHING_OBJECT
                                || pt1.node_undersurface < pt2.node_undersurface);
        }
        return true;