Add controllers for vehicle movement
This commit is contained in:
parent
24a7d23c96
commit
48c970c7ba
|
@ -17,7 +17,11 @@
|
|||
{
|
||||
"Name": "Carla",
|
||||
"Type": "Developer",
|
||||
"LoadingPhase": "Default"
|
||||
"LoadingPhase": "Default",
|
||||
"AdditionalDependencies": [
|
||||
"Engine",
|
||||
"PhysXVehicles"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,23 +6,23 @@ public class Carla : ModuleRules
|
|||
{
|
||||
public Carla(TargetInfo Target)
|
||||
{
|
||||
|
||||
|
||||
PublicIncludePaths.AddRange(
|
||||
new string[] {
|
||||
"Carla/Public"
|
||||
// ... add public include paths required here ...
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
PrivateIncludePaths.AddRange(
|
||||
new string[] {
|
||||
"Carla/Private",
|
||||
// ... add other private include paths required here ...
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
PublicDependencyModuleNames.AddRange(
|
||||
new string[]
|
||||
{
|
||||
|
@ -30,8 +30,8 @@ public class Carla : ModuleRules
|
|||
// ... add other public dependencies that you statically link with here ...
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
PrivateDependencyModuleNames.AddRange(
|
||||
new string[]
|
||||
{
|
||||
|
@ -39,11 +39,12 @@ public class Carla : ModuleRules
|
|||
"Engine",
|
||||
"Slate",
|
||||
"SlateCore",
|
||||
// ... add private dependencies that you statically link with here ...
|
||||
"PhysXVehicles",
|
||||
// ... add private dependencies that you statically link with here ...
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
DynamicallyLoadedModuleNames.AddRange(
|
||||
new string[]
|
||||
{
|
||||
|
|
|
@ -2,80 +2,3 @@
|
|||
|
||||
#include "Carla.h"
|
||||
#include "CarlaServerController.h"
|
||||
|
||||
#include "Camera/CameraComponent.h"
|
||||
#include "GameFramework/Pawn.h"
|
||||
#include "GameFramework/SpringArmComponent.h"
|
||||
// #include "CarlaPlayerCameraManager.h"
|
||||
|
||||
ACarlaServerController::ACarlaServerController()
|
||||
{
|
||||
// PlayerCameraManagerClass = ACarlaPlayerCameraManager::StaticClass();
|
||||
bAutoManageActiveCameraTarget = false;
|
||||
|
||||
// Create the spring arm component
|
||||
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm0"));
|
||||
SpringArm->TargetOffset = FVector(0.f, 0.f, 200.f);
|
||||
SpringArm->SetRelativeRotation(FRotator(-15.f, 0.f, 0.f));
|
||||
SpringArm->SetupAttachment(RootComponent);
|
||||
SpringArm->TargetArmLength = 600.0f;
|
||||
SpringArm->bEnableCameraRotationLag = true;
|
||||
SpringArm->CameraRotationLagSpeed = 7.f;
|
||||
SpringArm->bInheritPitch = false;
|
||||
SpringArm->bInheritRoll = false;
|
||||
|
||||
// Do not collide, may clip into level.
|
||||
SpringArm->bDoCollisionTest = false;
|
||||
|
||||
// Create the camera component
|
||||
PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera0"));
|
||||
PlayerCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
|
||||
PlayerCamera->bUsePawnControlRotation = false;
|
||||
PlayerCamera->FieldOfView = 90.f;
|
||||
}
|
||||
|
||||
ACarlaServerController::~ACarlaServerController() {}
|
||||
|
||||
void ACarlaServerController::SetupInputComponent()
|
||||
{
|
||||
Super::SetupInputComponent();
|
||||
check(InputComponent);
|
||||
InputComponent->BindAxis("CameraZoom", this, &ACarlaServerController::ChangeCameraZoom);
|
||||
InputComponent->BindAxis("CameraUp", this, &ACarlaServerController::ChangeCameraUp);
|
||||
InputComponent->BindAxis("CameraRight", this, &ACarlaServerController::ChangeCameraRight);
|
||||
InputComponent->BindAction("RestartLevel", IE_Pressed, this, &ACarlaServerController::RestartLevel);
|
||||
}
|
||||
|
||||
void ACarlaServerController::Possess(APawn *aPawn)
|
||||
{
|
||||
Super::Possess(aPawn);
|
||||
if (aPawn != nullptr) {
|
||||
SpringArm->AttachToComponent(
|
||||
aPawn->GetRootComponent(),
|
||||
FAttachmentTransformRules::KeepRelativeTransform);
|
||||
}
|
||||
}
|
||||
|
||||
void ACarlaServerController::CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult)
|
||||
{
|
||||
PlayerCamera->GetCameraView(DeltaTime, OutResult);
|
||||
}
|
||||
|
||||
void ACarlaServerController::ChangeCameraZoom(float Value)
|
||||
{
|
||||
SpringArm->TargetArmLength = FMath::Clamp(SpringArm->TargetArmLength + Value, 200.0f, 1e4f);
|
||||
}
|
||||
|
||||
void ACarlaServerController::ChangeCameraUp(float Value)
|
||||
{
|
||||
auto rotation = SpringArm->GetRelativeTransform().Rotator();
|
||||
rotation.Pitch = FMath::Clamp(rotation.Pitch - Value, -80.0f, 0.0f);
|
||||
SpringArm->SetRelativeRotation(rotation);
|
||||
}
|
||||
|
||||
void ACarlaServerController::ChangeCameraRight(float Value)
|
||||
{
|
||||
auto rotation = SpringArm->GetRelativeTransform().Rotator();
|
||||
rotation.Yaw -= Value;
|
||||
SpringArm->SetRelativeRotation(rotation);
|
||||
}
|
||||
|
|
|
@ -2,45 +2,15 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include "GameFramework/PlayerController.h"
|
||||
#include "CarlaVehicleController.h"
|
||||
#include "CarlaServerController.generated.h"
|
||||
|
||||
class USpringArmComponent;
|
||||
class UCameraComponent;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
UCLASS()
|
||||
class CARLA_API ACarlaServerController : public APlayerController
|
||||
class CARLA_API ACarlaServerController : public ACarlaVehicleController
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
public:
|
||||
|
||||
ACarlaServerController();
|
||||
|
||||
~ACarlaServerController();
|
||||
|
||||
virtual void SetupInputComponent() override;
|
||||
|
||||
virtual void Possess(APawn *aPawn) override;
|
||||
|
||||
// virtual void UnPossess() override;
|
||||
|
||||
virtual void CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult) override;
|
||||
|
||||
private:
|
||||
|
||||
void ChangeCameraZoom(float Value);
|
||||
|
||||
void ChangeCameraUp(float Value);
|
||||
|
||||
void ChangeCameraRight(float Value);
|
||||
|
||||
UPROPERTY()
|
||||
USpringArmComponent *SpringArm;
|
||||
|
||||
UPROPERTY()
|
||||
UCameraComponent *PlayerCamera;
|
||||
};
|
||||
|
|
|
@ -0,0 +1,156 @@
|
|||
// CARLA, Copyright (C) 2017 Computer Vision Center (CVC)
|
||||
|
||||
#include "Carla.h"
|
||||
#include "CarlaVehicleController.h"
|
||||
|
||||
#include "Camera/CameraComponent.h"
|
||||
#include "GameFramework/Pawn.h"
|
||||
#include "GameFramework/SpringArmComponent.h"
|
||||
#include "WheeledVehicle.h"
|
||||
#include "WheeledVehicleMovementComponent.h"
|
||||
|
||||
// =============================================================================
|
||||
// -- Constructor and destructor -----------------------------------------------
|
||||
// =============================================================================
|
||||
|
||||
ACarlaVehicleController::ACarlaVehicleController()
|
||||
{
|
||||
bAutoManageActiveCameraTarget = false;
|
||||
|
||||
// Create the spring arm component
|
||||
SpringArm = CreateDefaultSubobject<USpringArmComponent>(TEXT("SpringArm0"));
|
||||
SpringArm->TargetOffset = FVector(0.f, 0.f, 200.f);
|
||||
SpringArm->SetRelativeRotation(FRotator(-15.f, 0.f, 0.f));
|
||||
SpringArm->SetupAttachment(RootComponent);
|
||||
SpringArm->TargetArmLength = 600.0f;
|
||||
SpringArm->bEnableCameraRotationLag = true;
|
||||
SpringArm->CameraRotationLagSpeed = 7.f;
|
||||
SpringArm->bInheritPitch = false;
|
||||
SpringArm->bInheritRoll = false;
|
||||
|
||||
// Do not collide, may clip into level.
|
||||
SpringArm->bDoCollisionTest = false;
|
||||
|
||||
// Create the camera component
|
||||
PlayerCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("Camera0"));
|
||||
PlayerCamera->SetupAttachment(SpringArm, USpringArmComponent::SocketName);
|
||||
PlayerCamera->bUsePawnControlRotation = false;
|
||||
PlayerCamera->FieldOfView = 90.f;
|
||||
}
|
||||
|
||||
ACarlaVehicleController::~ACarlaVehicleController() {}
|
||||
|
||||
// =============================================================================
|
||||
// -- APlayerController --------------------------------------------------------
|
||||
// =============================================================================
|
||||
|
||||
void ACarlaVehicleController::SetupInputComponent()
|
||||
{
|
||||
Super::SetupInputComponent();
|
||||
check(InputComponent != nullptr);
|
||||
// Camera.
|
||||
InputComponent->BindAxis("CameraZoom", this, &ACarlaVehicleController::ChangeCameraZoom);
|
||||
InputComponent->BindAxis("CameraUp", this, &ACarlaVehicleController::ChangeCameraUp);
|
||||
InputComponent->BindAxis("CameraRight", this, &ACarlaVehicleController::ChangeCameraRight);
|
||||
// Global options.
|
||||
InputComponent->BindAction("RestartLevel", IE_Pressed, this, &ACarlaVehicleController::RestartLevel);
|
||||
InputComponent->BindAction("ToggleManualMode", IE_Pressed, this, &ACarlaVehicleController::ToggleManualMode);
|
||||
}
|
||||
|
||||
void ACarlaVehicleController::Possess(APawn *aPawn)
|
||||
{
|
||||
Super::Possess(aPawn);
|
||||
auto *WheeledVehicle = Cast<AWheeledVehicle>(aPawn);
|
||||
if (WheeledVehicle != nullptr) {
|
||||
SpringArm->AttachToComponent(
|
||||
aPawn->GetRootComponent(),
|
||||
FAttachmentTransformRules::KeepRelativeTransform);
|
||||
MovementComponent = WheeledVehicle->GetVehicleMovementComponent();
|
||||
check(MovementComponent != nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
void ACarlaVehicleController::CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult)
|
||||
{
|
||||
PlayerCamera->GetCameraView(DeltaTime, OutResult);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// -- Car movement methods -----------------------------------------------------
|
||||
// =============================================================================
|
||||
|
||||
void ACarlaVehicleController::SetThrottleInput(float Value)
|
||||
{
|
||||
check(MovementComponent != nullptr);
|
||||
MovementComponent->SetThrottleInput(Value);
|
||||
}
|
||||
|
||||
void ACarlaVehicleController::SetSteeringInput(float Value)
|
||||
{
|
||||
check(MovementComponent != nullptr);
|
||||
MovementComponent->SetSteeringInput(Value);
|
||||
}
|
||||
|
||||
void ACarlaVehicleController::SetHandbrakeInput(bool Value)
|
||||
{
|
||||
check(MovementComponent != nullptr);
|
||||
MovementComponent->SetHandbrakeInput(Value);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// -- Manual mode --------------------------------------------------------------
|
||||
// =============================================================================
|
||||
|
||||
void ACarlaVehicleController::SetManualMode(bool On)
|
||||
{
|
||||
if (On != bManualMode) {
|
||||
bManualMode = On;
|
||||
SetupControllerInput();
|
||||
}
|
||||
}
|
||||
|
||||
void ACarlaVehicleController::ToggleManualMode()
|
||||
{
|
||||
SetManualMode(!bManualMode);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// -- Input bindings -----------------------------------------------------------
|
||||
// =============================================================================
|
||||
|
||||
void ACarlaVehicleController::SetupControllerInput()
|
||||
{
|
||||
check(InputComponent != nullptr);
|
||||
// Vehicle movement.
|
||||
if (IsInManualMode()) {
|
||||
InputComponent->BindAxis("MoveForward", this, &ACarlaVehicleController::SetThrottleInput);
|
||||
InputComponent->BindAxis("MoveRight", this, &ACarlaVehicleController::SetSteeringInput);
|
||||
InputComponent->BindAction("Handbrake", IE_Pressed, this, &ACarlaVehicleController::HoldHandbrake);
|
||||
InputComponent->BindAction("Handbrake", IE_Released, this, &ACarlaVehicleController::ReleaseHandbrake);
|
||||
} else {
|
||||
UE_LOG(LogCarla, Error, TEXT("Not implemented")); /// @todo
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// -- Camera movement methods --------------------------------------------------
|
||||
// =============================================================================
|
||||
|
||||
void ACarlaVehicleController::ACarlaVehicleController::ChangeCameraZoom(float Value)
|
||||
{
|
||||
SpringArm->TargetArmLength = FMath::Clamp(SpringArm->TargetArmLength + Value, 200.0f, 1e4f);
|
||||
}
|
||||
|
||||
void ACarlaVehicleController::ChangeCameraUp(float Value)
|
||||
{
|
||||
auto Rotation = SpringArm->GetRelativeTransform().Rotator();
|
||||
Rotation.Pitch = FMath::Clamp(Rotation.Pitch - Value, -80.0f, 0.0f);
|
||||
SpringArm->SetRelativeRotation(Rotation);
|
||||
}
|
||||
|
||||
void ACarlaVehicleController::ChangeCameraRight(float Value)
|
||||
{
|
||||
auto Rotation = SpringArm->GetRelativeTransform().Rotator();
|
||||
Rotation.Yaw -= Value;
|
||||
SpringArm->SetRelativeRotation(Rotation);
|
||||
}
|
|
@ -0,0 +1,119 @@
|
|||
// CARLA, Copyright (C) 2017 Computer Vision Center (CVC)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "GameFramework/PlayerController.h"
|
||||
#include "CarlaVehicleController.generated.h"
|
||||
|
||||
class UCameraComponent;
|
||||
class USpringArmComponent;
|
||||
class UWheeledVehicleMovementComponent;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
UCLASS()
|
||||
class CARLA_API ACarlaVehicleController : public APlayerController
|
||||
{
|
||||
GENERATED_BODY()
|
||||
|
||||
// ===========================================================================
|
||||
/// @name Constructor and destructor
|
||||
// ===========================================================================
|
||||
/// @{
|
||||
public:
|
||||
|
||||
ACarlaVehicleController();
|
||||
|
||||
~ACarlaVehicleController();
|
||||
|
||||
/// @}
|
||||
// ===========================================================================
|
||||
/// @name APlayerController overrides
|
||||
// ===========================================================================
|
||||
/// @{
|
||||
public:
|
||||
|
||||
virtual void SetupInputComponent() override;
|
||||
|
||||
virtual void Possess(APawn *aPawn) override;
|
||||
|
||||
virtual void CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult) override;
|
||||
|
||||
/// @}
|
||||
// ===========================================================================
|
||||
/// @name Car input
|
||||
// ===========================================================================
|
||||
/// @{
|
||||
public:
|
||||
|
||||
void SetThrottleInput(float Value);
|
||||
|
||||
void SetSteeringInput(float Value);
|
||||
|
||||
void SetHandbrakeInput(bool Value);
|
||||
|
||||
void HoldHandbrake() {
|
||||
SetHandbrakeInput(true);
|
||||
}
|
||||
|
||||
void ReleaseHandbrake() {
|
||||
SetHandbrakeInput(false);
|
||||
}
|
||||
|
||||
/// @}
|
||||
// ===========================================================================
|
||||
/// @name Manual mode
|
||||
// ===========================================================================
|
||||
/// @{
|
||||
public:
|
||||
|
||||
bool IsInManualMode() const
|
||||
{
|
||||
return bManualMode;
|
||||
}
|
||||
|
||||
void SetManualMode(bool On);
|
||||
|
||||
void ToggleManualMode();
|
||||
|
||||
/// @}
|
||||
// ===========================================================================
|
||||
/// @name Camera input
|
||||
// ===========================================================================
|
||||
/// @{
|
||||
private:
|
||||
|
||||
void ChangeCameraZoom(float Value);
|
||||
|
||||
void ChangeCameraUp(float Value);
|
||||
|
||||
void ChangeCameraRight(float Value);
|
||||
|
||||
/// @}
|
||||
// ===========================================================================
|
||||
/// @name Input bindings
|
||||
// ===========================================================================
|
||||
/// @{
|
||||
private:
|
||||
|
||||
void SetupControllerInput();
|
||||
|
||||
/// @}
|
||||
// ===========================================================================
|
||||
// -- Member variables -------------------------------------------------------
|
||||
// ===========================================================================
|
||||
private:
|
||||
|
||||
UPROPERTY()
|
||||
bool bManualMode = false;
|
||||
|
||||
UPROPERTY()
|
||||
USpringArmComponent *SpringArm;
|
||||
|
||||
UPROPERTY()
|
||||
UCameraComponent *PlayerCamera;
|
||||
|
||||
UPROPERTY()
|
||||
UWheeledVehicleMovementComponent *MovementComponent;
|
||||
};
|
Loading…
Reference in New Issue