Skip to content

Opponents controller

AI-controlled vehicle that extends the base physics controller with racing-specific behaviors and decision-making capabilities. 🧠


🚀 Core Features

FeatureDescriptionKey Properties
AI DrivingSmart navigation using driving policydrivingPolicy
Race TrackingLap counting and finishing logiccurrentLap, finished
Performance LimitsSpeed and acceleration capscurrentMaxSpeedForward
Cooldown SystemPrevents input spamming_lastRotation, _accelerationCooldown

🏗️ Constructor

constructor(
sprite: Sprite,
startPosition: StartPosition,
drivingPolicy: DrivingPolicyBase,
nickname: string,
traction: number
)

Parameters:

ParameterTypeDescription
spriteSpriteVisual representation asset 🖼️
startPositionStartPositionInitial position and angle 🏁
drivingPolicyDrivingPolicyBaseAI decision-making logic 🧠
nicknamestringOpponent identifier 🏷️
tractionnumberSurface grip coefficient (0-1)

Initial Setup:

  • Sets initial position with collision box offset (+30, +15)
  • Configures speed limits (200 max speed, 80 acceleration)
  • Links driving policy to controller

🔄 Update Loop

override update(deltaTime: number)

Behavior Flow:

graph TD
A[Update Physics] --> B[Get AI Decision]
B --> C{Accelerate?}
C -->|Yes| D[Apply Acceleration]
B --> E{Rotate?}
E -->|Yes| F[Apply Rotation]
B --> G{Brake?}
G -->|Yes| H[Apply Brake]
H --> I[Check Race Completion]

Key Actions:

  1. Updates cooldown timers ⏱️
  2. Gets driving decision from policy 🧠
  3. Applies movement commands ↔️
  4. Checks race completion status 🏁

🏆 Race Completion Logic

Finishing Conditions:

  1. Completes 3 laps (currentLap >= 3) ✅
  2. Sets finished flag and records time 🕒
  3. Triggers results scene when all finish 🎬
if (this.currentLap >= 3) {
this.finished = true;
this.finishedTime = Scoreboard.instance.currentTime;
// ...trigger results scene
}

⚙️ Technical Details

Cooldown System

ActionCooldownPurpose
Rotation0.04sSmooth turning 🌀
Acceleration0.00sInstant response ⚡
Braking0.02sControlled stops 🛑

Position Offset

  • Adds (+30, +15) to align collision box
  • Matches getCarCorners utility function

🎮 Usage Example

// Create opponent
const opponent = new OpponentController(
carSprite,
startPosition,
new AggressiveDrivingPolicy(),
"AI Racer",
0.85
);
// Game loop
function update(deltaTime) {
opponent.update(deltaTime);
}

🛠️ Design Considerations

Inheritance

  • Extends PhysicsBasedController for core physics
  • Adds AI-specific behaviors on top

Performance

  • Cooldowns prevent over-processing
  • Simple condition checks
  • Efficient position calculations

💡 Customization Tips

  1. Adjust Driving Policy:

    this.drivingPolicy = new CustomDrivingPolicy();
  2. Modify Speed Limits:

    this.currentMaxSpeedForward = 250; // Faster AI
  3. Change Cooldowns:

    this._rotationCooldown = 0.06; // Slower turns

🚦 Pro Tip: Use different driving policies to create varied AI difficulty levels!


📊 State Management

StateDescription
finishedRace completion status
currentLapProgress tracking
_lastRotationTurn cooldown timer
_lastAccelerationThrottle cooldown timer