in Ambit/Source/Ambit/Vehicle/AmbitWheeledVehicleAIController.cpp [55:168]
void AAmbitWheeledVehicleAIController::Tick(const float DeltaTime)
{
Super::Tick(DeltaTime);
if (!IsValid(Vehicle))
{
return;
}
UWheeledVehicleMovementComponent4W* Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(
Vehicle->GetVehicleMovement());
if (WaypointsBuffer.Num() == 0)
{
Vehicle4W->SetSteeringInput(0);
Vehicle4W->SetBrakeInput(1);
Vehicle4W->SetThrottleInput(0);
return;
}
if (WaypointsBuffer.Num() < 3 && IsLooped)
{
for (const FVector& Waypoint : Waypoints)
{
WaypointsBuffer.Emplace(Waypoint);
}
}
const FVector CurrentLocation = Vehicle->GetActorLocation();
const float CurrentSpeed = Vehicle4W->GetForwardSpeed();
float TargetSpeed = SpeedLimit;
if (WaypointsBuffer.Num() >= 3)
{
float Radius = AmbitVehicleHelpers::GetThreePointCircleRadius(PastWaypoint, WaypointsBuffer[0],
WaypointsBuffer[1]);
TargetSpeed = TargetSpeed > Radius * VehicleControl::TurningRadiusSpeedFactor
? Radius * VehicleControl::TurningRadiusSpeedFactor
: TargetSpeed;
for (int i = 0; i < WaypointsBuffer.Num() - 2; i++)
{
if (FVector::Dist(FVector(CurrentLocation.X, CurrentLocation.Y, 0),
FVector(WaypointsBuffer[i].X, WaypointsBuffer[i].Y, 0)) < CurrentSpeed *
VehicleControl::LookingAheadDistanceFactor)
{
Radius = AmbitVehicleHelpers::GetThreePointCircleRadius(WaypointsBuffer[i], WaypointsBuffer[i + 1],
WaypointsBuffer[i + 2]);
TargetSpeed = TargetSpeed > Radius * VehicleControl::TurningRadiusSpeedFactor
? Radius * VehicleControl::TurningRadiusSpeedFactor
: TargetSpeed;
}
else
{
break;
}
}
}
const float Acceleration = ThrottleController.RunStep(TargetSpeed, CurrentSpeed, DeltaTime);
if (Acceleration > 0)
{
Vehicle4W->SetBrakeInput(0);
Vehicle4W->SetThrottleInput(Acceleration);
}
else
{
Vehicle4W->SetBrakeInput(-Acceleration);
Vehicle4W->SetThrottleInput(0);
}
const FVector TargetLocation = WaypointsBuffer[0];
const FVector Forward = Vehicle->GetActorForwardVector();
//Not change steering input significantly
float Steering = SteeringController.RunStep(TargetLocation, CurrentLocation, Forward, DeltaTime);
if (Steering > PastSteering + VehicleControl::SteeringDelta)
{
Steering = PastSteering + VehicleControl::SteeringDelta;
}
else if (Steering < PastSteering - VehicleControl::SteeringDelta)
{
Steering = PastSteering - VehicleControl::SteeringDelta;
}
Vehicle4W->SetSteeringInput(Steering);
PastSteering = Steering;
//Purge all past Waypoints
float MaxIndex = -1;
for (int i = 0; i < WaypointsBuffer.Num(); i++)
{
if (FVector::Dist(FVector(CurrentLocation.X, CurrentLocation.Y, 0),
FVector(WaypointsBuffer[i].X, WaypointsBuffer[i].Y, 0)) < WaypointDistanceThreshold)
{
MaxIndex = i;
}
else
{
break;
}
}
if (MaxIndex >= 0)
{
PastWaypoint = WaypointsBuffer[MaxIndex];
for (int i = 0; i < MaxIndex + 1; i++)
{
WaypointsBuffer.RemoveAt(0);
}
}
}