Skip to content

Commit dde8f0e

Browse files
numberZerosfan5
authored andcommitted
Replace a non-aligned cuboid with a cylinder in client::ActiveObjectMgr::getActiveSelectableObjects
1 parent 21035bf commit dde8f0e

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

src/client/activeobjectmgr.cpp

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,17 +110,8 @@ void ActiveObjectMgr::getActiveObjects(const v3f &origin, f32 max_d,
110110
std::vector<DistanceSortedActiveObject> ActiveObjectMgr::getActiveSelectableObjects(const core::line3d<f32> &shootline)
111111
{
112112
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-
118113
f32 max_d = shootline.getLength();
119114
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);
124115

125116
for (auto &ao_it : m_active_objects) {
126117
ClientActiveObject *obj = ao_it.second;
@@ -129,23 +120,22 @@ std::vector<DistanceSortedActiveObject> ActiveObjectMgr::getActiveSelectableObje
129120
if (!obj->getSelectionBox(&selection_box))
130121
continue;
131122

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;
136125

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
138129

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)
141131
continue;
142132

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)
146136
continue;
147137

148-
dest.emplace_back(obj, d);
138+
dest.emplace_back(obj, a);
149139
}
150140
return dest;
151141
}

0 commit comments

Comments
 (0)