Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 68 additions & 1 deletion src/engine/pathfinding/RecastNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,8 @@ export class RecastNavigation {
y: point.z,
}));

const smoothedPath = this.smoothPath(rawPath, agentRadius);
// CRITICAL: Use water-specific smoothing to avoid cutting across land
const smoothedPath = this.smoothWaterPath(rawPath, agentRadius);

return { path: smoothedPath, found: true };
} catch {
Expand Down Expand Up @@ -795,6 +796,72 @@ export class RecastNavigation {
return true;
}

/**
* Smooth water path by removing redundant waypoints.
* Uses water navmesh validation to avoid cutting across land.
*/
private smoothWaterPath(
path: Array<{ x: number; y: number }>,
agentRadius: number
): Array<{ x: number; y: number }> {
if (path.length <= 2) return path;

const smoothed: Array<{ x: number; y: number }> = [path[0]];
let currentIndex = 0;

while (currentIndex < path.length - 1) {
let farthestReachable = currentIndex + 1;

for (let i = path.length - 1; i > currentIndex + 1; i--) {
// Use water-specific walkability check
if (this.canWalkDirectWater(path[currentIndex], path[i], agentRadius)) {
farthestReachable = i;
break;
}
}

smoothed.push(path[farthestReachable]);
currentIndex = farthestReachable;
}

return smoothed;
}

/**
* Check if a direct path between two points is navigable on water.
* Uses water navmesh validation to prevent paths cutting across land.
*/
private canWalkDirectWater(
from: { x: number; y: number },
to: { x: number; y: number },
agentRadius: number
): boolean {
if (!this.waterNavMeshQuery) return false;

const dx = to.x - from.x;
const dy = to.y - from.y;
const dist = distance(from.x, from.y, to.x, to.y);

if (dist < 0.5) return true;

// Sample points along the line with finer granularity for water
// to catch narrow peninsulas and islands
const stepSize = agentRadius * 0.4;
const steps = Math.ceil(dist / stepSize);

for (let i = 1; i < steps; i++) {
const t = i / steps;
const x = from.x + dx * t;
const y = from.y + dy * t;

if (!this.isWaterWalkable(x, y)) {
return false;
}
}

return true;
}

/**
* Find nearest point on navmesh.
* Uses terrain height for better query accuracy.
Expand Down
Loading