AI navigation system that dynamically generates and adjusts racing paths for opponent vehicles, featuring obstacle avoidance and adaptive pathfinding.
🧠 AI Navigation Overview
| Feature | Description |
|---|
| Dynamic Path Generation | Creates real-time optimal racing lines |
| Obstacle Avoidance | Detects and routes around blockers |
| Progress Tracking | Monitors checkpoint progression |
| Path Optimization | Maintains efficient route calculations |
📊 Core Properties
Path State
| Property | Type | Description |
|---|
actualPath | CheckPoint[] | Active navigation path |
_actualPathLength | number | Current path distance |
_actualPathCurrentPointOffset | number | Path traversal progress |
Navigation Parameters
| Property | Default | Description |
|---|
_pathOffset | 3 | Lookahead checkpoint count |
_safeDistance | 150 | Minimum obstacle clearance |
_maxActualPathLength | 20 | Maximum path segments |
Progress Tracking
| Property | Type | Description |
|---|
visitedCheckpoints | number | Passed checkpoint count |
_actualPathCurrentPoint | number | Active waypoint index |
🎯 Path Targeting
currentTargetCheckPoint
get currentTargetCheckPoint(): CheckPoint {
return this.actualPath[this._actualPathCurrentPoint];
}
nextTargetCheckPoint
get nextTargetCheckPoint(): CheckPoint {
const nextIndex = (this._actualPathCurrentPoint + 1) % this.actualPath.length;
return this.actualPath[nextIndex];
}
🛠️ Key Methods
Path Generation
generateActualPath()
graph TD
A[Get base track path] --> B[Apply lane offset]
B --> C[Calculate smoothing]
C --> D[Trim to max length]
D --> E[Store as actualPath]
Dynamic Adjustment
updateActualTrackPathIfIntersects()
- Raycast ahead for obstacles
- If collision detected:
- Calculate avoidance vector
- Generate new path segment
- Blend into existing path
| Parameter | Type | Description |
|---|
parentController | OpponentController | AI vehicle reference |
Utility Methods
| Method | Returns | Description |
|---|
getDistanceToActualPoint() | number | Meters to next waypoint |
isPointOnTrack() | boolean | Position validity check |
🏭 Factory Method
createFromTrackPath()
static createFromTrackPath(
trackPath: TrackPath,
offset?: number = 1.5
): EnemyPath
Typical Usage:
const aiPath = EnemyPath.createFromTrackPath(
mainTrackPath,
2.0 // Lane offset
);
🚦 Behavioral Logic
Path Following Algorithm
- Get current target waypoint
- Calculate steering vector
- If within waypoint radius:
- Increment
_actualPathCurrentPoint
- Trim passed path segments
- Repeat
Obstacle Avoidance
graph LR
A[Detect obstacle] --> B{Can pass left?}
B -->|Yes| C[Steer left]
B -->|No| D{Can pass right?}
D -->|Yes| E[Steer right]
D -->|No| F[Brake]
📝 Example Usage
// Initialize
const enemyPath = new EnemyPath(trackPath);
enemyPath._safeDistance = 200; // Aggressive AI
// Game loop
function updateAI() {
enemyPath.updateActualTrackPathIfIntersects(aiController);
const target = enemyPath.currentTargetCheckPoint;
aiController.steerToward(target.position);
}
🏎️ Pro Tip: Adjust _pathOffset based on difficulty level - higher values make AI look further ahead for smoother racing lines.
- Path recalculation happens every:
- Obstacle detection
- 2 seconds (preventative)
- Keep
_maxActualPathLength under 25 for balance
- Use spatial partitioning for obstacle checks