Skip to content

Commit

Permalink
dynamically load assets through BP classes so packaging works
Browse files Browse the repository at this point in the history
  • Loading branch information
sytelus committed Apr 11, 2018
1 parent 35f25b9 commit 94b9d23
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 150 deletions.
6 changes: 3 additions & 3 deletions AirLib/include/common/AirSimSettings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,11 @@ struct AirSimSettings {
{
pawn_paths.clear();
pawn_paths.emplace("BareboneCar",
PawnPath("/AirSim/VehicleAdv/Vehicle/VehicleAdvPawn"));
PawnPath("Class'/AirSim/VehicleAdv/Vehicle/VehicleAdvPawn.VehicleAdvPawn_C'"));
pawn_paths.emplace("DefaultCar",
PawnPath("/AirSim/VehicleAdv/SUV/SuvCarPawn"));
PawnPath("Class'/AirSim/VehicleAdv/SUV/SuvCarPawn.SuvCarPawn_C'"));
pawn_paths.emplace("DefaultQuadrotor",
PawnPath("/AirSim/Blueprints/BP_FlyingPawn"));
PawnPath("Class'/AirSim/Blueprints/BP_FlyingPawn.BP_FlyingPawn_C'"));


msr::airlib::Settings pawn_paths_child;
Expand Down
6 changes: 3 additions & 3 deletions AirLib/src/vehicles/multirotor/api/MultirotorRpcLibServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ MultirotorRpcLibServer::MultirotorRpcLibServer(MultirotorApi* drone, string serv
float obs_avoidance_vel, const MultirotorRpcLibAdapators::Vector3r& origin, float xy_length, float max_z, float min_z) ->
bool { return getDroneApi()->setSafety(SafetyEval::SafetyViolationType(enable_reasons), obs_clearance, obs_startegy,
obs_avoidance_vel, origin.to(), xy_length, max_z, min_z); });
(static_cast<rpc::server*>(getServer()))->
bind("setRCData", [&](const MultirotorRpcLibAdapators::RCData& data) ->
void { getDroneApi()->setRCData(data.to()); });
(static_cast<rpc::server*>(getServer()))->
bind("setRCData", [&](const MultirotorRpcLibAdapators::RCData& data) ->
void { getDroneApi()->setRCData(data.to()); });


//getters
Expand Down
15 changes: 14 additions & 1 deletion Unreal/Plugins/AirSim/Source/AirBlueprintLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,10 +604,23 @@ UObject* UAirBlueprintLib::LoadObject(const std::string& name)
FString str(name.c_str());
UObject *obj = StaticLoadObject(UObject::StaticClass(), nullptr, *str);
if (obj == nullptr) {
std::string msg = "Failed to load asset - " + name;
std::string msg = "Failed to load asset object - " + name;
FString fmsg(msg.c_str());
LogMessage(TEXT("Load: "), fmsg, LogDebugLevel::Failure);
throw std::invalid_argument(msg);
}
return obj;
}

UClass* UAirBlueprintLib::LoadClass(const std::string& name)
{
FString str(name.c_str());
UClass *cls = StaticLoadClass(UObject::StaticClass(), nullptr, *str);
if (cls == nullptr) {
std::string msg = "Failed to load asset class - " + name;
FString fmsg(msg.c_str());
LogMessage(TEXT("Load: "), fmsg, LogDebugLevel::Failure);
throw std::invalid_argument(msg);
}
return cls;
}
1 change: 1 addition & 0 deletions Unreal/Plugins/AirSim/Source/AirBlueprintLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class UAirBlueprintLib : public UBlueprintFunctionLibrary
static std::vector<UPrimitiveComponent*> getPhysicsComponents(AActor* actor);

static UObject* LoadObject(const std::string& name);
static UClass* LoadClass(const std::string& name);

private:
template<typename T>
Expand Down
211 changes: 103 additions & 108 deletions Unreal/Plugins/AirSim/Source/Car/CarPawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,10 @@
#include "PIPCamera.h"
#include <vector>



// Needed for VR Headset
#if HMD_MODULE_INCLUDED
#include "IHeadMountedDisplay.h"
#endif // HMD_MODULE_INCLUDED

const FName ACarPawn::LookUpBinding("LookUp");
const FName ACarPawn::LookRightBinding("LookRight");
const FName ACarPawn::EngineAudioRPM("RPM");

#define LOCTEXT_NAMESPACE "VehiclePawn"


ACarPawn::ACarPawn()
{
this->AutoPossessPlayer = EAutoReceiveInput::Player0;
//this->AutoReceiveInput = EAutoReceiveInput::Player0;

UWheeledVehicleMovementComponent4W* Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(GetVehicleMovement());

static ConstructorHelpers::FClassFinder<APIPCamera> pip_camera_class(TEXT("Blueprint'/AirSim/Blueprints/BP_PIPCamera'"));
pip_camera_class_ = pip_camera_class.Succeeded() ? pip_camera_class.Class : nullptr;

Expand All @@ -43,76 +26,16 @@ ACarPawn::ACarPawn()
auto non_slippery_mat = Cast<UPhysicalMaterial>(
UAirBlueprintLib::LoadObject(car_mesh_paths.non_slippery_mat));
if (slippery_mat)
SlipperyMaterial = slippery_mat;
slippery_mat_ = slippery_mat;
else
UAirBlueprintLib::LogMessageString("Failed to load Slippery physics material", "", LogDebugLevel::Failure);
if (non_slippery_mat)
NonSlipperyMaterial = non_slippery_mat;

check(Vehicle4W->WheelSetups.Num() == 4);

// Wheels/Tyres
// Setup the wheels
Vehicle4W->WheelSetups[0].WheelClass = UCarWheelFront::StaticClass();
Vehicle4W->WheelSetups[0].BoneName = FName("PhysWheel_FL");
Vehicle4W->WheelSetups[0].AdditionalOffset = FVector(0.f, -8.f, 0.f);

Vehicle4W->WheelSetups[1].WheelClass = UCarWheelFront::StaticClass();
Vehicle4W->WheelSetups[1].BoneName = FName("PhysWheel_FR");
Vehicle4W->WheelSetups[1].AdditionalOffset = FVector(0.f, 8.f, 0.f);

Vehicle4W->WheelSetups[2].WheelClass = UCarWheelRear::StaticClass();
Vehicle4W->WheelSetups[2].BoneName = FName("PhysWheel_BL");
Vehicle4W->WheelSetups[2].AdditionalOffset = FVector(0.f, -8.f, 0.f);

Vehicle4W->WheelSetups[3].WheelClass = UCarWheelRear::StaticClass();
Vehicle4W->WheelSetups[3].BoneName = FName("PhysWheel_BR");
Vehicle4W->WheelSetups[3].AdditionalOffset = FVector(0.f, 8.f, 0.f);

// Adjust the tire loading
Vehicle4W->MinNormalizedTireLoad = 0.0f;
Vehicle4W->MinNormalizedTireLoadFiltered = 0.2308f;
Vehicle4W->MaxNormalizedTireLoad = 2.0f;
Vehicle4W->MaxNormalizedTireLoadFiltered = 2.0f;

// Engine
// Torque setup
Vehicle4W->EngineSetup.MaxRPM = 5700.0f;
Vehicle4W->EngineSetup.TorqueCurve.GetRichCurve()->Reset();
Vehicle4W->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(0.0f, 400.0f);
Vehicle4W->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(1890.0f, 500.0f);
Vehicle4W->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(5730.0f, 400.0f);

// Adjust the steering
Vehicle4W->SteeringCurve.GetRichCurve()->Reset();
Vehicle4W->SteeringCurve.GetRichCurve()->AddKey(0.0f, 1.0f);
Vehicle4W->SteeringCurve.GetRichCurve()->AddKey(40.0f, 0.7f);
Vehicle4W->SteeringCurve.GetRichCurve()->AddKey(120.0f, 0.6f);

// Transmission
// We want 4wd
Vehicle4W->DifferentialSetup.DifferentialType = EVehicleDifferential4W::LimitedSlip_4W;

// Drive the front wheels a little more than the rear
Vehicle4W->DifferentialSetup.FrontRearSplit = 0.65;

// Automatic gearbox
Vehicle4W->TransmissionSetup.bUseGearAutoBox = true;
Vehicle4W->TransmissionSetup.GearSwitchTime = 0.15f;
Vehicle4W->TransmissionSetup.GearAutoBoxLatency = 1.0f;

// Disable reverse as brake, this is needed for SetBreakInput() to take effect
Vehicle4W->bReverseAsBrake = false;
non_slippery_mat_ = non_slippery_mat;
else
UAirBlueprintLib::LogMessageString("Failed to load NonSlippery physics material", "", LogDebugLevel::Failure);

// Physics settings
// Adjust the center of mass - the buggy is quite low
UPrimitiveComponent* UpdatedPrimitive = Cast<UPrimitiveComponent>(Vehicle4W->UpdatedComponent);
if (UpdatedPrimitive)
{
UpdatedPrimitive->BodyInstance.COMNudge = FVector(8.0f, 0.0f, 0.0f);
}
setupVehicleMovementComponent();

// Set the inertia scale. This controls how the mass of the vehicle is distributed.
Vehicle4W->InertiaTensorScale = FVector(1.0f, 1.333f, 1.2f);
Vehicle4W->bDeprecatedSpringOffsetMode = true;

// Create In-Car camera component
InternalCameraBase1 = CreateDefaultSubobject<USceneComponent>(TEXT("InternalCameraBase1"));
Expand Down Expand Up @@ -159,11 +82,83 @@ ACarPawn::ACarPawn()
GearDisplayReverseColor = FColor(255, 0, 0, 255);
GearDisplayColor = FColor(255, 255, 255, 255);

bIsLowFriction = false;
is_low_friction_ = false;

wrapper_.reset(new VehiclePawnWrapper());
}

void ACarPawn::setupVehicleMovementComponent()
{
UWheeledVehicleMovementComponent4W* movement = CastChecked<UWheeledVehicleMovementComponent4W>(GetVehicleMovement());
check(movement->WheelSetups.Num() == 4);

// Wheels/Tyres
// Setup the wheels
movement->WheelSetups[0].WheelClass = UCarWheelFront::StaticClass();
movement->WheelSetups[0].BoneName = FName("PhysWheel_FL");
movement->WheelSetups[0].AdditionalOffset = FVector(0.f, -8.f, 0.f);

movement->WheelSetups[1].WheelClass = UCarWheelFront::StaticClass();
movement->WheelSetups[1].BoneName = FName("PhysWheel_FR");
movement->WheelSetups[1].AdditionalOffset = FVector(0.f, 8.f, 0.f);

movement->WheelSetups[2].WheelClass = UCarWheelRear::StaticClass();
movement->WheelSetups[2].BoneName = FName("PhysWheel_BL");
movement->WheelSetups[2].AdditionalOffset = FVector(0.f, -8.f, 0.f);

movement->WheelSetups[3].WheelClass = UCarWheelRear::StaticClass();
movement->WheelSetups[3].BoneName = FName("PhysWheel_BR");
movement->WheelSetups[3].AdditionalOffset = FVector(0.f, 8.f, 0.f);

// Adjust the tire loading
movement->MinNormalizedTireLoad = 0.0f;
movement->MinNormalizedTireLoadFiltered = 0.2308f;
movement->MaxNormalizedTireLoad = 2.0f;
movement->MaxNormalizedTireLoadFiltered = 2.0f;

// Engine
// Torque setup
movement->EngineSetup.MaxRPM = 5700.0f;
movement->EngineSetup.TorqueCurve.GetRichCurve()->Reset();
movement->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(0.0f, 400.0f);
movement->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(1890.0f, 500.0f);
movement->EngineSetup.TorqueCurve.GetRichCurve()->AddKey(5730.0f, 400.0f);

// Adjust the steering
movement->SteeringCurve.GetRichCurve()->Reset();
movement->SteeringCurve.GetRichCurve()->AddKey(0.0f, 1.0f);
movement->SteeringCurve.GetRichCurve()->AddKey(40.0f, 0.7f);
movement->SteeringCurve.GetRichCurve()->AddKey(120.0f, 0.6f);

// Transmission
// We want 4wd
movement->DifferentialSetup.DifferentialType = EVehicleDifferential4W::LimitedSlip_4W;

// Drive the front wheels a little more than the rear
movement->DifferentialSetup.FrontRearSplit = 0.65;

// Automatic gearbox
movement->TransmissionSetup.bUseGearAutoBox = true;
movement->TransmissionSetup.GearSwitchTime = 0.15f;
movement->TransmissionSetup.GearAutoBoxLatency = 1.0f;

// Disable reverse as brake, this is needed for SetBreakInput() to take effect
movement->bReverseAsBrake = false;

// Physics settings
// Adjust the center of mass - the buggy is quite low
UPrimitiveComponent* primitive = Cast<UPrimitiveComponent>(movement->UpdatedComponent);
if (primitive)
{
primitive->BodyInstance.COMNudge = FVector(8.0f, 0.0f, 0.0f);
}

// Set the inertia scale. This controls how the mass of the vehicle is distributed.
movement->InertiaTensorScale = FVector(1.0f, 1.333f, 1.2f);
movement->bDeprecatedSpringOffsetMode = true;
}


void ACarPawn::NotifyHit(class UPrimitiveComponent* MyComp, class AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation,
FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit)
{
Expand Down Expand Up @@ -299,8 +294,8 @@ void ACarPawn::setupInputBindings()
UAirBlueprintLib::BindAxisToKey(FInputAxisKeyMapping("MoveRight", EKeys::Left, -0.5), this,
this, &ACarPawn::MoveRight);

UAirBlueprintLib::BindActionToKey("Handbrake", EKeys::End, this, &ACarPawn::OnHandbrakePressed, true);
UAirBlueprintLib::BindActionToKey("Handbrake", EKeys::End, this, &ACarPawn::OnHandbrakeReleased, false);
UAirBlueprintLib::BindActionToKey("Handbrake", EKeys::End, this, &ACarPawn::onHandbrakePressed, true);
UAirBlueprintLib::BindActionToKey("Handbrake", EKeys::End, this, &ACarPawn::onHandbrakeReleased, false);

UAirBlueprintLib::BindAxisToKey(FInputAxisKeyMapping("Footbrake", EKeys::SpaceBar, 1), this,
this, &ACarPawn::FootBrake);
Expand All @@ -315,16 +310,16 @@ void ACarPawn::setupInputBindings()
this, &ACarPawn::FootBrake);

//below is not needed
//UAirBlueprintLib::BindActionToKey("Reverse", EKeys::Down, this, &ACarPawn::OnReversePressed, true);
//UAirBlueprintLib::BindActionToKey("Reverse", EKeys::Down, this, &ACarPawn::OnReverseReleased, false);
//UAirBlueprintLib::BindActionToKey("Reverse", EKeys::Down, this, &ACarPawn::onReversePressed, true);
//UAirBlueprintLib::BindActionToKey("Reverse", EKeys::Down, this, &ACarPawn::onReverseReleased, false);
}

void ACarPawn::MoveForward(float Val)
{
if (Val < 0)
OnReversePressed();
onReversePressed();
else
OnReverseReleased();
onReverseReleased();

keyboard_controls_.throttle = Val;
}
Expand All @@ -334,12 +329,12 @@ void ACarPawn::MoveRight(float Val)
keyboard_controls_.steering = Val;
}

void ACarPawn::OnHandbrakePressed()
void ACarPawn::onHandbrakePressed()
{
keyboard_controls_.handbrake = true;
}

void ACarPawn::OnHandbrakeReleased()
void ACarPawn::onHandbrakeReleased()
{
keyboard_controls_.handbrake = false;
}
Expand All @@ -349,7 +344,7 @@ void ACarPawn::FootBrake(float Val)
keyboard_controls_.brake = Val;
}

void ACarPawn::OnReversePressed()
void ACarPawn::onReversePressed()
{
if (keyboard_controls_.manual_gear >= 0) {
keyboard_controls_.is_manual_gear = true;
Expand All @@ -358,7 +353,7 @@ void ACarPawn::OnReversePressed()
}
}

void ACarPawn::OnReverseReleased()
void ACarPawn::onReverseReleased()
{
if (keyboard_controls_.manual_gear < 0) {
keyboard_controls_.is_manual_gear = false;
Expand Down Expand Up @@ -391,18 +386,18 @@ void ACarPawn::Tick(float Delta)

updateKinematics(Delta);

// Update phsyics material
UpdatePhysicsMaterial();
// update phsyics material
updatePhysicsMaterial();

// Update the strings used in the hud (incar and onscreen)
UpdateHUDStrings();
updateHUDStrings();

// Set the string in the incar hud
UpdateInCarHUD();
updateInCarHUD();

// Pass the engine RPM to the sound component
float RPMToAudioScale = 2500.0f / GetVehicleMovement()->GetEngineMaxRotationSpeed();
EngineSoundComponent->SetFloatParameter(EngineAudioRPM, GetVehicleMovement()->GetEngineRotationSpeed()*RPMToAudioScale);
EngineSoundComponent->SetFloatParameter(FName("RPM"), GetVehicleMovement()->GetEngineRotationSpeed()*RPMToAudioScale);

getVehiclePawnWrapper()->setLogLine(getLogString());
}
Expand Down Expand Up @@ -465,7 +460,7 @@ void ACarPawn::BeginPlay()
EngineSoundComponent->Play();
}

void ACarPawn::UpdateHUDStrings()
void ACarPawn::updateHUDStrings()
{
float vel = FMath::Abs(GetVehicleMovement()->GetForwardSpeed() / 100); //cm/s -> m/s
float vel_rounded = FMath::FloorToInt(vel * 10) / 10.0f;
Expand All @@ -492,7 +487,7 @@ void ACarPawn::UpdateHUDStrings()

}

void ACarPawn::UpdateInCarHUD()
void ACarPawn::updateInCarHUD()
{
APlayerController* PlayerController = Cast<APlayerController>(GetController());
if ((PlayerController != nullptr) && (InCarSpeed != nullptr) && (InCarGear != nullptr))
Expand All @@ -512,19 +507,19 @@ void ACarPawn::UpdateInCarHUD()
}
}

void ACarPawn::UpdatePhysicsMaterial()
void ACarPawn::updatePhysicsMaterial()
{
if (GetActorUpVector().Z < 0)
{
if (bIsLowFriction == true)
if (is_low_friction_ == true)
{
GetMesh()->SetPhysMaterialOverride(NonSlipperyMaterial);
bIsLowFriction = false;
GetMesh()->SetPhysMaterialOverride(non_slippery_mat_);
is_low_friction_ = false;
}
else
{
GetMesh()->SetPhysMaterialOverride(SlipperyMaterial);
bIsLowFriction = true;
GetMesh()->SetPhysMaterialOverride(slippery_mat_);
is_low_friction_ = true;
}
}
}
Expand Down
Loading

0 comments on commit 94b9d23

Please sign in to comment.