@@ -110,17 +110,8 @@ void ActiveObjectMgr::getActiveObjects(const v3f &origin, f32 max_d,
110
110
std::vector<DistanceSortedActiveObject> ActiveObjectMgr::getActiveSelectableObjects (const core::line3d<f32 > &shootline)
111
111
{
112
112
std::vector<DistanceSortedActiveObject> dest;
113
- // Imagine a not-axis-aligned cuboid oriented into the direction of the shootline,
114
- // with the width of the object's selection box radius * 2 and with length of the
115
- // shootline (+selection box radius forwards and backwards). We check whether
116
- // the selection box center is inside this cuboid.
117
-
118
113
f32 max_d = shootline.getLength ();
119
114
v3f dir = shootline.getVector ().normalize ();
120
- // arbitrary linearly independent vector and orthogonal dirs
121
- v3f li2dir = dir + (std::fabs (dir.X ) < 0 .5f ? v3f (1 ,0 ,0 ) : v3f (0 ,1 ,0 ));
122
- v3f dir_ortho1 = dir.crossProduct (li2dir).normalize ();
123
- v3f dir_ortho2 = dir.crossProduct (dir_ortho1);
124
115
125
116
for (auto &ao_it : m_active_objects) {
126
117
ClientActiveObject *obj = ao_it.second ;
@@ -129,23 +120,22 @@ std::vector<DistanceSortedActiveObject> ActiveObjectMgr::getActiveSelectableObje
129
120
if (!obj->getSelectionBox (&selection_box))
130
121
continue ;
131
122
132
- // possible optimization: get rid of the sqrt here
133
- f32 selection_box_radius = selection_box.getRadius ();
134
-
135
- v3f pos_diff = obj->getPosition () + selection_box.getCenter () - shootline.start ;
123
+ v3f obj_center = obj->getPosition () + selection_box.getCenter ();
124
+ f32 obj_radius_sq = selection_box.getExtent ().getLengthSQ () / 4 ;
136
125
137
- f32 d = dir.dotProduct (pos_diff);
126
+ v3f c = obj_center - shootline.start ;
127
+ f32 a = dir.dotProduct (c); // project c onto dir
128
+ f32 b_sq = c.getLengthSQ () - a * a; // distance from shootline to obj_center, squared
138
129
139
- // backward- and far-plane
140
- if (d + selection_box_radius < 0 .0f || d - selection_box_radius > max_d)
130
+ if (b_sq > obj_radius_sq)
141
131
continue ;
142
132
143
- // side-planes
144
- if ( std::fabs (dir_ortho1. dotProduct (pos_diff)) > selection_box_radius
145
- || std::fabs (dir_ortho2. dotProduct (pos_diff)) > selection_box_radius )
133
+ // backward- and far-plane
134
+ f32 obj_radius = std::sqrt (obj_radius_sq);
135
+ if (a < -obj_radius || a > max_d + obj_radius )
146
136
continue ;
147
137
148
- dest.emplace_back (obj, d );
138
+ dest.emplace_back (obj, a );
149
139
}
150
140
return dest;
151
141
}
0 commit comments