Skip to content

Commit

Permalink
scene: Only allow moving forward in the path
Browse files Browse the repository at this point in the history
Fixes oscillating between different points in the path.
  • Loading branch information
BastianBlokland committed Jul 11, 2023
1 parent 10442e3 commit caeb289
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions libs/scene/include/scene_nav.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ ecs_comp_extern_public(SceneNavAgentComp) {
ecs_comp_extern_public(SceneNavPathComp) {
GeoNavCell* cells;
u32 cellCount;
u32 currentTargetIndex; // Index in the path we are currently moving towards.
GeoVector destination;
TimeDuration nextRefreshTime;
};
Expand Down
9 changes: 6 additions & 3 deletions libs/scene/src/nav.c
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ ecs_system_define(SceneNavUpdateAgentsSys) {
path->cellCount = geo_nav_path(env->navGrid, fromCell, toCell, container);
path->nextRefreshTime = path_next_refresh_time(time);
path->destination = toPos;
path->currentTargetIndex = 1; // Path includes the start point; should be skipped.
--pathQueriesRemaining;

// Stop if no path is possible at this time.
Expand All @@ -358,16 +359,18 @@ ecs_system_define(SceneNavUpdateAgentsSys) {
}

// Attempt to take a shortcut as far up the path as possible without being obstructed.
for (u32 i = (path->cellCount); i-- > 1;) {
for (u32 i = path->cellCount; i-- > path->currentTargetIndex;) {
if (!geo_nav_line_blocked(env->navGrid, fromCell, path->cells[i])) {
path->currentTargetIndex = i;
scene_locomotion_move(loco, geo_nav_position(env->navGrid, path->cells[i]));
goto Done;
}
}

// No shortcut available; move to the next cell in the path.
// No shortcut available; move to the current target cell in the path.
if (path->cellCount > 1) {
scene_locomotion_move(loco, geo_nav_position(env->navGrid, path->cells[1]));
const GeoNavCell moveTowardsCell = path->cells[path->currentTargetIndex];
scene_locomotion_move(loco, geo_nav_position(env->navGrid, moveTowardsCell));
goto Done;
}

Expand Down

0 comments on commit caeb289

Please sign in to comment.