Skip to content

Commit

Permalink
Spawn and control two drones
Browse files Browse the repository at this point in the history
  • Loading branch information
saihv committed Mar 30, 2017
1 parent 4f7d098 commit f57e629
Show file tree
Hide file tree
Showing 24 changed files with 216 additions and 363 deletions.
Binary file not shown.
Binary file modified Unreal/Plugins/AirSim/Content/Blueprints/BP_FlyingPawn.uasset
Binary file not shown.
Binary file not shown.
Binary file modified Unreal/Plugins/AirSim/Content/Blueprints/BP_PIPCamera.uasset
Binary file not shown.
Binary file not shown.
Binary file modified Unreal/Plugins/AirSim/Content/Blueprints/BP_SimHUDWidget.uasset
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 2 additions & 3 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,9 @@ void UAirBlueprintLib::FollowActor(AActor* follower, const AActor* followee, con

template<class UserClass>
FInputActionBinding& UAirBlueprintLib::BindActionToKey(const FName action_name, const FKey in_key, UserClass* actor,
typename FInputActionHandlerSignature::TUObjectMethodDelegate<UserClass>::FMethodPtr func,
bool shift_key, bool control_key, bool alt_key, bool command_key)
typename FInputActionHandlerSignature::TUObjectMethodDelegate<UserClass>::FMethodPtr func)
{
FInputActionKeyMapping action(action_name, in_key, shift_key, control_key, alt_key, command_key);
FInputActionKeyMapping action(action_name, in_key);

APlayerController* controller = actor->GetWorld()->GetFirstPlayerController();

Expand Down
3 changes: 1 addition & 2 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary

template<class UserClass>
static FInputActionBinding& BindActionToKey(const FName action_name, const FKey in_key, UserClass* actor,
typename FInputActionHandlerSignature::TUObjectMethodDelegate< UserClass >::FMethodPtr func,
bool shift_key = false, bool control_key = false, bool alt_key = false, bool command_key = false);
typename FInputActionHandlerSignature::TUObjectMethodDelegate< UserClass >::FMethodPtr func);

template<class UserClass>
static FInputAxisBinding& BindAxisToKey(const FName axis_name, const FKey in_key, UserClass* actor,
Expand Down
2 changes: 1 addition & 1 deletion Unreal/Plugins/AirSim/Source/AirSim.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void SetupCompileMode(CompileMode mode, TargetInfo Target)
public AirSim(TargetInfo Target)
{
UEBuildConfiguration.bForceEnableExceptions = true;
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "RenderCore" });
PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "RenderCore", "RHI" });
PrivateDependencyModuleNames.AddRange(new string[] { "UMG", "Slate", "SlateCore" });

//suppress VC++ proprietary warnings
Expand Down
297 changes: 102 additions & 195 deletions Unreal/Plugins/AirSim/Source/CameraDirector.cpp
Original file line number Diff line number Diff line change
@@ -1,195 +1,102 @@
#include "AirSim.h"
#include "CameraDirector.h"
#include "AirBlueprintLib.h"


ACameraDirector::ACameraDirector()
{
PrimaryActorTick.bCanEverTick = true;
}

void ACameraDirector::BeginPlay()
{
setupInputBindings();

Super::BeginPlay();
}

void ACameraDirector::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );

if (mode_ == ECameraDirectorMode::CAMERA_DIRECTOR_MODE_MANUAL) {
ExternalCamera->SetActorLocationAndRotation(camera_location_manual_, camera_rotation_manual_);
}
}

ECameraDirectorMode ACameraDirector::getMode()
{
return mode_;
}

void ACameraDirector::setMode(ECameraDirectorMode mode)
{
mode_ = mode;

if (mode_ == ECameraDirectorMode::CAMERA_DIRECTOR_MODE_MANUAL) {
camera_location_manual_ = ExternalCamera->GetActorLocation();
camera_rotation_manual_ = ExternalCamera->GetActorRotation();
enableManualBindings(true);
}
else
enableManualBindings(false);
}

void ACameraDirector::setupInputBindings()
{
this->EnableInput(this->GetWorld()->GetFirstPlayerController());

UAirBlueprintLib::BindActionToKey("InputEventFpvView", EKeys::LeftBracket, this, &ACameraDirector::InputEventFpvView);
UAirBlueprintLib::BindActionToKey("InputEventFlyWithView", EKeys::RightBracket, this, &ACameraDirector::InputEventFlyWithView);
UAirBlueprintLib::BindActionToKey("InputEventGroundView", EKeys::Backslash, this, &ACameraDirector::InputEventGroundView);
UAirBlueprintLib::BindActionToKey("InputEventManualView", EKeys::Semicolon, this, &ACameraDirector::InputEventManualView);

left_binding_ = & UAirBlueprintLib::BindAxisToKey("inputManualArrowLeft", EKeys::Left, this, &ACameraDirector::inputManualLeft);
right_binding_ = & UAirBlueprintLib::BindAxisToKey("inputManualArrowRight", EKeys::Right, this, &ACameraDirector::inputManualRight);
forward_binding_ = & UAirBlueprintLib::BindAxisToKey("inputManualForward", EKeys::Up, this, &ACameraDirector::inputManualForward);
backward_binding_ = & UAirBlueprintLib::BindAxisToKey("inputManualBackward", EKeys::Down, this, &ACameraDirector::inputManualBackward);
up_binding_ = & UAirBlueprintLib::BindAxisToKey("inputManualArrowUp", EKeys::PageUp, this, &ACameraDirector::inputManualMoveUp);
down_binding_ = & UAirBlueprintLib::BindAxisToKey("inputManualArrowDown", EKeys::PageDown, this, &ACameraDirector::inputManualDown);
left_yaw_binding_ = & UAirBlueprintLib::BindAxisToKey("inputManualLeftYaw", EKeys::A, this, &ACameraDirector::inputManualLeftYaw);
up_pitch_binding_ = & UAirBlueprintLib::BindAxisToKey("inputManualUpPitch", EKeys::W, this, &ACameraDirector::inputManualUpPitch);
right_yaw_binding_ = & UAirBlueprintLib::BindAxisToKey("inputManualRightYaw", EKeys::D, this, &ACameraDirector::inputManualRightYaw);
down_pitch_binding_ = & UAirBlueprintLib::BindAxisToKey("inputManualDownPitch", EKeys::S, this, &ACameraDirector::inputManualDownPitch);
}

void ACameraDirector::enableManualBindings(bool enable)
{
left_binding_->bConsumeInput = right_binding_->bConsumeInput = up_binding_->bConsumeInput = down_binding_->bConsumeInput = enable;
forward_binding_->bConsumeInput = backward_binding_->bConsumeInput = left_yaw_binding_->bConsumeInput = up_pitch_binding_->bConsumeInput = enable;
right_yaw_binding_->bConsumeInput = down_pitch_binding_->bConsumeInput = enable;
}

void ACameraDirector::inputManualLeft(float val)
{
if (!FMath::IsNearlyEqual(val, 0.f)) {
camera_location_manual_ += FVector(0, -val * 10, 0);
}
}
void ACameraDirector::inputManualRight(float val)
{
if (!FMath::IsNearlyEqual(val, 0.f))
camera_location_manual_ += FVector(0, val * 10, 0);
}
void ACameraDirector::inputManualForward(float val)
{
if (!FMath::IsNearlyEqual(val, 0.f))
camera_location_manual_ += FVector(val * 10, 0, 0);
}
void ACameraDirector::inputManualBackward(float val)
{
if (!FMath::IsNearlyEqual(val, 0.f))
camera_location_manual_ += FVector(-val * 10, 0, 0);
}
void ACameraDirector::inputManualMoveUp(float val)
{
if (!FMath::IsNearlyEqual(val, 0.f))
camera_location_manual_ += FVector(0, 0, val * 10);
}
void ACameraDirector::inputManualDown(float val)
{
if (!FMath::IsNearlyEqual(val, 0.f))
camera_location_manual_ += FVector(0, 0, -val * 10);
}
void ACameraDirector::inputManualLeftYaw(float val)
{
if (!FMath::IsNearlyEqual(val, 0.f))
camera_rotation_manual_.Add(0, -val, 0);
}
void ACameraDirector::inputManualUpPitch(float val)
{
if (!FMath::IsNearlyEqual(val, 0.f))
camera_rotation_manual_.Add(val, 0, 0);
}
void ACameraDirector::inputManualRightYaw(float val)
{
if (!FMath::IsNearlyEqual(val, 0.f))
camera_rotation_manual_.Add(0, val, 0);
}
void ACameraDirector::inputManualDownPitch(float val)
{
if (!FMath::IsNearlyEqual(val, 0.f))
camera_rotation_manual_.Add(-val, 0, 0);
}


bool ACameraDirector::checkCameraRefs()
{
if (ExternalCamera == nullptr || TargetPawn == nullptr || TargetPawn->getFpvCamera() == nullptr) {
UAirBlueprintLib::LogMessage("Cannot toggle PIP camera because FPV pwn camera and/or external camera is not set", "", LogDebugLevel::Failure, 60);
return false;
}
return true;
}

bool ACameraDirector::togglePIPScene()
{
if (!checkCameraRefs())
return false;
EPIPCameraType main_state = ExternalCamera->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_SCENE);
EPIPCameraType pip_state = TargetPawn->getFpvCamera()->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_SCENE);

if (ExternalCamera->getCameraMode() == EPIPCameraMode::PIP_CAMERA_MODE_PIP)
return main_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
else
return pip_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
}

bool ACameraDirector::togglePIPDepth()
{
if (!checkCameraRefs())
return false;
EPIPCameraType main_state = ExternalCamera->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_DEPTH);
EPIPCameraType pip_state = TargetPawn->getFpvCamera()->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_DEPTH);

if (ExternalCamera->getCameraMode() == EPIPCameraMode::PIP_CAMERA_MODE_PIP)
return main_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
else
return pip_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
}

bool ACameraDirector::togglePIPSeg()
{
if (!checkCameraRefs())
return false;
EPIPCameraType main_state = ExternalCamera->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_SEG);
EPIPCameraType pip_state = TargetPawn->getFpvCamera()->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_SEG);

if (ExternalCamera->getCameraMode() == EPIPCameraMode::PIP_CAMERA_MODE_PIP)
return main_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
else
return pip_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
}

bool ACameraDirector::togglePIPAll()
{
if (!checkCameraRefs())
return false;
EPIPCameraType main_state = ExternalCamera->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_ALL);
EPIPCameraType pip_state = TargetPawn->getFpvCamera()->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_ALL);

if (ExternalCamera->getCameraMode() == EPIPCameraMode::PIP_CAMERA_MODE_PIP)
return main_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
else
return pip_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
}


APIPCamera* ACameraDirector::getCamera(int id)
{
//TODO: support multiple camera
if (TargetPawn != nullptr)
return TargetPawn->getFpvCamera();
else
return nullptr;
}
#include "AirSim.h"
#include "CameraDirector.h"
#include "AirBlueprintLib.h"


ACameraDirector::ACameraDirector()
{
PrimaryActorTick.bCanEverTick = true;

}

void ACameraDirector::BeginPlay()
{
Super::BeginPlay();

setupInputBindings();
}

void ACameraDirector::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );
}

void ACameraDirector::setupInputBindings()
{
this->EnableInput(this->GetWorld()->GetFirstPlayerController());

UAirBlueprintLib::BindActionToKey("InputEventFpvView", EKeys::LeftBracket, this, &ACameraDirector::InputEventFpvView);
UAirBlueprintLib::BindActionToKey("InputEventFlyWithView", EKeys::RightBracket, this, &ACameraDirector::InputEventFlyWithView);
UAirBlueprintLib::BindActionToKey("InputEventGroundView", EKeys::Backslash, this, &ACameraDirector::InputEventGroundView);
}

bool ACameraDirector::checkCameraRefs()
{
if (ExternalCamera == nullptr || TargetPawn == nullptr || TargetPawn->getFpvCamera() == nullptr) {
UAirBlueprintLib::LogMessage("Cannot toggle PIP camera because FPV pwn camera and/or external camera is not set", "", LogDebugLevel::Failure, 60);
return false;
}
return true;
}

bool ACameraDirector::togglePIPScene()
{
if (!checkCameraRefs())
return false;
EPIPCameraType main_state = ExternalCamera->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_SCENE);
EPIPCameraType pip_state = TargetPawn->getFpvCamera()->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_SCENE);

if (ExternalCamera->getCameraMode() == EPIPCameraMode::PIP_CAMERA_MODE_PIP)
return main_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
else
return pip_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
}

bool ACameraDirector::togglePIPDepth()
{
if (!checkCameraRefs())
return false;
EPIPCameraType main_state = ExternalCamera->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_DEPTH);
EPIPCameraType pip_state = TargetPawn->getFpvCamera()->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_DEPTH);

if (ExternalCamera->getCameraMode() == EPIPCameraMode::PIP_CAMERA_MODE_PIP)
return main_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
else
return pip_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
}

bool ACameraDirector::togglePIPSeg()
{
if (!checkCameraRefs())
return false;
EPIPCameraType main_state = ExternalCamera->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_SEG);
EPIPCameraType pip_state = TargetPawn->getFpvCamera()->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_SEG);

if (ExternalCamera->getCameraMode() == EPIPCameraMode::PIP_CAMERA_MODE_PIP)
return main_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
else
return pip_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
}

bool ACameraDirector::togglePIPAll()
{
if (!checkCameraRefs())
return false;
EPIPCameraType main_state = ExternalCamera->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_ALL);
EPIPCameraType pip_state = TargetPawn->getFpvCamera()->toggleEnableCameraTypes(EPIPCameraType::PIP_CAMERA_TYPE_ALL);

if (ExternalCamera->getCameraMode() == EPIPCameraMode::PIP_CAMERA_MODE_PIP)
return main_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
else
return pip_state != EPIPCameraType::PIP_CAMERA_TYPE_NONE;
}


APIPCamera* ACameraDirector::getCamera(int id)
{
//TODO: support multiple camera
if (TargetPawn != nullptr)
return TargetPawn->getFpvCamera();
else
return nullptr;
}
38 changes: 0 additions & 38 deletions Unreal/Plugins/AirSim/Source/CameraDirector.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@
#include "GameFramework/Actor.h"
#include "CameraDirector.generated.h"


UENUM(BlueprintType)
enum class ECameraDirectorMode : uint8
{
CAMERA_DIRECTOR_MODE_FPV = 1 UMETA(DisplayName="FPV"),
CAMERA_DIRECTOR_MODE_GROUND_OBSERVER = 2 UMETA(DisplayName="GroundObserver"),
CAMERA_DIRECTOR_MODE_FLY_WITH_ME = 3 UMETA(DisplayName="FlyWithMe"),
CAMERA_DIRECTOR_MODE_MANUAL = 4 UMETA(DisplayName="Manual")
};

UCLASS()
class AIRSIM_API ACameraDirector : public AActor
{
Expand All @@ -31,8 +21,6 @@ class AIRSIM_API ACameraDirector : public AActor
void InputEventFpvView();
UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "InputEventGroundView"))
void InputEventGroundView();
UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "InputEventManualView"))
void InputEventManualView();
UFUNCTION(BlueprintImplementableEvent, meta = (DisplayName = "InputEventFlyWithView"))
void InputEventFlyWithView();

Expand All @@ -53,33 +41,7 @@ class AIRSIM_API ACameraDirector : public AActor
virtual void BeginPlay() override;
virtual void Tick( float DeltaSeconds ) override;

UFUNCTION(BlueprintCallable, Category = "Modes")
ECameraDirectorMode getMode();
UFUNCTION(BlueprintCallable, Category = "Modes")
void setMode(ECameraDirectorMode mode);

private:
void setupInputBindings();
bool checkCameraRefs();
void enableManualBindings(bool enable);

void inputManualLeft(float val);
void inputManualRight(float val);
void inputManualForward(float val);
void inputManualBackward(float val);
void inputManualMoveUp(float val);
void inputManualDown(float val);
void inputManualLeftYaw(float val);
void inputManualUpPitch(float val);
void inputManualRightYaw(float val);
void inputManualDownPitch(float val);

private:
ECameraDirectorMode mode_;
FInputAxisBinding *left_binding_, *right_binding_, *up_binding_, *down_binding_;
FInputAxisBinding *forward_binding_, *backward_binding_, *left_yaw_binding_, *up_pitch_binding_;
FInputAxisBinding *right_yaw_binding_, *down_pitch_binding_;

FVector camera_location_manual_;
FRotator camera_rotation_manual_;
};
Loading

0 comments on commit f57e629

Please sign in to comment.