Skip to content

EnemyPath

AI navigation system that dynamically generates and adjusts racing paths for opponent vehicles, featuring obstacle avoidance and adaptive pathfinding.


🧠 AI Navigation Overview

FeatureDescription
Dynamic Path GenerationCreates real-time optimal racing lines
Obstacle AvoidanceDetects and routes around blockers
Progress TrackingMonitors checkpoint progression
Path OptimizationMaintains efficient route calculations

📊 Core Properties

Path State

PropertyTypeDescription
actualPathCheckPoint[]Active navigation path
_actualPathLengthnumberCurrent path distance
_actualPathCurrentPointOffsetnumberPath traversal progress
PropertyDefaultDescription
_pathOffset3Lookahead checkpoint count
_safeDistance150Minimum obstacle clearance
_maxActualPathLength20Maximum path segments

Progress Tracking

PropertyTypeDescription
visitedCheckpointsnumberPassed checkpoint count
_actualPathCurrentPointnumberActive 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()

  1. Raycast ahead for obstacles
  2. If collision detected:
    • Calculate avoidance vector
    • Generate new path segment
    • Blend into existing path
ParameterTypeDescription
parentControllerOpponentControllerAI vehicle reference

Utility Methods

MethodReturnsDescription
getDistanceToActualPoint()numberMeters to next waypoint
isPointOnTrack()booleanPosition 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

  1. Get current target waypoint
  2. Calculate steering vector
  3. If within waypoint radius:
    • Increment _actualPathCurrentPoint
    • Trim passed path segments
  4. 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.


⚙️ Performance Considerations

  • Path recalculation happens every:
    • Obstacle detection
    • 2 seconds (preventative)
  • Keep _maxActualPathLength under 25 for balance
  • Use spatial partitioning for obstacle checks