Skip to content

Commit b70be35

Browse files
author
Peter Vaiko
committed
perf: collision avoidance controller
If a harvester has an insanely long course, the collision avoidance controller, while looking for an intersection between the unloader and the harvester's course, iterated until the end of the harvester's which caused significant slowdowns. Limit the lookahead for the collision avoidance controller: always start checking for intersections at the current waypoint of the unloader and the harvester, and stop looking after the lookahead distance. #871
1 parent 86c3169 commit b70be35

1 file changed

Lines changed: 8 additions & 6 deletions

File tree

scripts/Course.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,24 +1330,26 @@ end
13301330

13311331
--- Check if our course intersects otherCourse
13321332
---@param otherCourse Course
1333-
---@param lookahead number distance in meters we want to traverse on our course to check for an intersection
1334-
---@param startAtCurrentWaypoint boolean if true, start checking at the current waypoint on both courses,
1335-
--- otherwise at the first waypoint
1333+
---@param lookahead number distance in meters we want to traverse on our course and on the other course
1334+
--- to check for an intersection. The search always starts at the current waypoint of each course
13361335
---@return number, number distance on my course to the intersection point (or nil when there is no intersection),
13371336
--- distance on the other course until the intersection point.
1338-
function Course:intersects(otherCourse, lookahead, startAtCurrentWaypoint)
1337+
function Course:intersects(otherCourse, lookahead)
13391338
local myDistance = 0
1340-
for i = startAtCurrentWaypoint and self:getCurrentWaypointIx() or 1, #self.waypoints - 1 do
1339+
for i = self:getCurrentWaypointIx() , #self.waypoints - 1 do
13411340
local m1, m2 = self.waypoints[i], self.waypoints[i + 1]
13421341
myDistance = myDistance + m2.dToHere - m1.dToHere
13431342
local otherDistance = 0
1344-
for j = startAtCurrentWaypoint and otherCourse:getCurrentWaypointIx() or 1, #otherCourse.waypoints - 1 do
1343+
for j = otherCourse:getCurrentWaypointIx(), #otherCourse.waypoints - 1 do
13451344
local o1, o2 = otherCourse.waypoints[j], otherCourse.waypoints[j + 1]
13461345
otherDistance = otherDistance + o2.dToHere - o1.dToHere
13471346
if CpMathUtil.getIntersectionPoint(m1.x, m1.z, m2.x, m2.z, o1.x, o1.z, o2.x, o2.z) then
13481347
-- these sections intersect
13491348
return myDistance, otherDistance
13501349
end
1350+
if otherDistance > lookahead then
1351+
break
1352+
end
13511353
end
13521354
if myDistance > lookahead then
13531355
break

0 commit comments

Comments
 (0)