From 48c970c7baadeaa4eac2ee7e3ad434c292bdf0f0 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Thu, 9 Mar 2017 11:52:21 +0000 Subject: [PATCH] Add controllers for vehicle movement --- Carla.uplugin | 8 +- Source/Carla/Carla.Build.cs | 21 +-- Source/Carla/Game/CarlaServerController.cpp | 77 --------- Source/Carla/Game/CarlaServerController.h | 34 +--- Source/Carla/Game/CarlaVehicleController.cpp | 156 +++++++++++++++++++ Source/Carla/Game/CarlaVehicleController.h | 119 ++++++++++++++ 6 files changed, 294 insertions(+), 121 deletions(-) create mode 100644 Source/Carla/Game/CarlaVehicleController.cpp create mode 100644 Source/Carla/Game/CarlaVehicleController.h diff --git a/Carla.uplugin b/Carla.uplugin index 65c95ad63..e85152a4f 100644 --- a/Carla.uplugin +++ b/Carla.uplugin @@ -17,7 +17,11 @@ { "Name": "Carla", "Type": "Developer", - "LoadingPhase": "Default" + "LoadingPhase": "Default", + "AdditionalDependencies": [ + "Engine", + "PhysXVehicles" + ] } ] -} \ No newline at end of file +} diff --git a/Source/Carla/Carla.Build.cs b/Source/Carla/Carla.Build.cs index b0421389b..87cef8023 100644 --- a/Source/Carla/Carla.Build.cs +++ b/Source/Carla/Carla.Build.cs @@ -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[] { diff --git a/Source/Carla/Game/CarlaServerController.cpp b/Source/Carla/Game/CarlaServerController.cpp index e1706d352..1bfa15345 100644 --- a/Source/Carla/Game/CarlaServerController.cpp +++ b/Source/Carla/Game/CarlaServerController.cpp @@ -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(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(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); -} diff --git a/Source/Carla/Game/CarlaServerController.h b/Source/Carla/Game/CarlaServerController.h index da4a70041..35c1af4ec 100644 --- a/Source/Carla/Game/CarlaServerController.h +++ b/Source/Carla/Game/CarlaServerController.h @@ -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; }; diff --git a/Source/Carla/Game/CarlaVehicleController.cpp b/Source/Carla/Game/CarlaVehicleController.cpp new file mode 100644 index 000000000..dbcc0c958 --- /dev/null +++ b/Source/Carla/Game/CarlaVehicleController.cpp @@ -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(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(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(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); +} diff --git a/Source/Carla/Game/CarlaVehicleController.h b/Source/Carla/Game/CarlaVehicleController.h new file mode 100644 index 000000000..c22833062 --- /dev/null +++ b/Source/Carla/Game/CarlaVehicleController.h @@ -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; +};