Skip to content

Commit 408ed66

Browse files
committed
Performance getNearestWaypoints
This will speed up the function getNearestWaypoints by 5x. Idea is to catch up the waypoints in a distance of 50m around the looking position with simple distance function instead of performing complex calculations on all waypoints. After that the complex calculations are performed on the catched waypoints to find the suitable nearest waypoint. In case less then 10 waypoints are found the existing approach is followed. Possible further improvements: - consider workwith instead of fixed 50m - consider number of workers(multitools) on same course Remark: I will not invest more time on this, so leave it up to you: Take it, improve it or drop it.
1 parent 557c1e0 commit 408ed66

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

scripts/Course.lua

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,39 @@ function Course:getNearestWaypoints(node, startIx)
13331333
local dClosest, dClosestRightDirection = math.huge, math.huge
13341334
local ixClosest, ixClosestRightDirection = 1, 1
13351335

1336+
local nearestRange = 50
1337+
local p = self.waypoints[1]
1338+
local x, z = 0, 0
1339+
local d = math.huge
1340+
local waypointIDs = {}
1341+
1342+
for i = startIx or 1, #self.waypoints do
1343+
p = self.waypoints[i]
1344+
x, _, z = p:getPosition()
1345+
d = MathUtil.getPointPointDistance(x, z, nx, nz)
1346+
if d < nearestRange then
1347+
table.insert(waypointIDs, i)
1348+
end
1349+
end
1350+
1351+
if #waypointIDs > 9 then
1352+
for _, i in ipairs (waypointIDs) do
1353+
p = self.waypoints[i]
1354+
x, _, z = self:getWaypointPosition(i)
1355+
d = MathUtil.getPointPointDistance(x, z, nx, nz)
1356+
if d < dClosest then
1357+
dClosest = d
1358+
ixClosest = i
1359+
end
1360+
local deltaAngle = math.abs(CpMathUtil.getDeltaAngle(math.rad(p.angle), nodeAngle))
1361+
if d < dClosestRightDirection and deltaAngle < maxDeltaAngle then
1362+
dClosestRightDirection = d
1363+
ixClosestRightDirection = i
1364+
end
1365+
end
1366+
return ixClosest, dClosest, ixClosestRightDirection, dClosestRightDirection
1367+
end
1368+
13361369
for i = startIx or 1, #self.waypoints do
13371370
local p = self.waypoints[i]
13381371
local x, _, z = self:getWaypointPosition(i)

0 commit comments

Comments
 (0)