16#include "EngineUtils.h"
17#include "GameFramework/Pawn.h"
18#include "WheeledVehicleMovementComponent.h"
24static bool RayCast(
const AActor &Actor,
const FVector &Start,
const FVector &End)
27 static FName TraceTag = FName(TEXT(
"VehicleTrace"));
28 FCollisionQueryParams CollisionParams(TraceTag,
true);
29 CollisionParams.AddIgnoredActor(&Actor);
31 const bool Success = Actor.GetWorld()->LineTraceSingleByObjectType(
35 FCollisionObjectQueryParams(FCollisionObjectQueryParams::AllDynamicObjects),
41 return Success && OutHit.bBlockingHit;
47 const FVector &Direction)
49 const auto ForwardVector =
Vehicle.GetVehicleOrientation();
50 const auto VehicleBounds =
Vehicle.GetVehicleBoundingBoxExtent();
52 FVector NormDirection = Direction.GetSafeNormal();
54 const float Distance = std::max(50.0f, Speed * Speed);
56 const FVector StartCenter =
Vehicle.GetActorLocation() +
57 (ForwardVector * (250.0f + VehicleBounds.X / 2.0f)) + FVector(0.0f, 0.0f, 50.0f);
58 const FVector EndCenter = StartCenter + NormDirection * (Distance + VehicleBounds.X / 2.0f);
60 const FVector StartRight = StartCenter +
61 (FVector(ForwardVector.Y, -ForwardVector.X, ForwardVector.Z) * 100.0f);
62 const FVector EndRight = StartRight + NormDirection * (Distance + VehicleBounds.X / 2.0f);
64 const FVector StartLeft = StartCenter +
65 (FVector(-ForwardVector.Y, ForwardVector.X, ForwardVector.Z) * 100.0f);
66 const FVector EndLeft = StartLeft + NormDirection * (Distance + VehicleBounds.X / 2.0f);
77 std::queue<T> EmptyQueue;
78 Queue.swap(EmptyQueue);
86 : Super(ObjectInitializer)
88 RandomEngine = CreateDefaultSubobject<URandomEngine>(TEXT(
"RandomEngine"));
92 PrimaryActorTick.bCanEverTick =
true;
93 PrimaryActorTick.TickGroup = TG_PrePhysics;
104 Super::OnPossess(aPawn);
108 UE_LOG(LogCarla, Error, TEXT(
"Controller already possessing a vehicle!"));
111 Vehicle = Cast<ACarlaWheeledVehicle>(aPawn);
119 TActorIterator<ACityMapGenerator> It(GetWorld());
120 RoadMap = (It ? It->GetRoadMap() :
nullptr);
126 Super::OnUnPossess();
134 Super::Tick(DeltaTime);
167 ECarlaWheeledVehicleState::FreeDriving :
168 ECarlaWheeledVehicleState::AutopilotOff);
171 TrafficLightState = ETrafficLightState::Green;
179 const TArray<FVector> &Locations,
180 const bool bOverwriteCurrent)
182 if (bOverwriteCurrent)
186 for (
auto &Location : Locations)
static bool RayCast(const AActor &Actor, const FVector &Start, const FVector &End)
static void ClearQueue(std::queue< T > &Queue)
static bool IsThereAnObstacleAhead(const ACarlaWheeledVehicle &Vehicle, const float Speed, const FVector &Direction)
Base class for CARLA wheeled vehicles.
void SetAIVehicleState(ECarlaWheeledVehicleState InState)
void SetThrottleInput(float Value)
void SetReverse(bool Value)
float GetMaximumSteerAngle() const
Get the maximum angle at which the front wheel can steer.
void SetBrakeInput(float Value)
void SetSteeringInput(float Value)
void FlushVehicleControl()
void ApplyVehicleControl(const FVehicleControl &Control, EVehicleInputPriority Priority)
void SetHandbrakeInput(bool Value)
void ConfigureAutopilot(const bool Enable, const bool KeepState=false)
void OnUnPossess() override
AWheeledVehicleAIController(const FObjectInitializer &ObjectInitializer)
URandomEngine * RandomEngine
bool IsPossessingAVehicle() const
void OnPossess(APawn *aPawn) override
void SetFixedRoute(const TArray< FVector > &Locations, bool bOverwriteCurrent=true)
Set a fixed route to follow if autopilot is enabled.
void Tick(float DeltaTime) override
~AWheeledVehicleAIController()
std::queue< FVector > TargetLocations
ACarlaWheeledVehicle * Vehicle
void Seed(int32 InSeed)
Seed the random engine.
static int32 GenerateRandomSeed()
Generate a non-deterministic random seed.