Added friction variation + refactor

This commit is contained in:
Manish 2019-04-18 16:00:32 +02:00
parent d832a19028
commit f3164137d9
4 changed files with 54 additions and 39 deletions

View File

@ -232,6 +232,15 @@ static void AddVariationsForSensor(FActorDefinition &Def)
static void AddVariationsForTrigger(FActorDefinition &Def)
{
// Friction
FActorVariation Friction;
Friction.Id = FString("friction");
Friction.Type = EActorAttributeType::Float;
Friction.RecommendedValues = { TEXT("0.0f") };
Friction.bRestrictToRecommended = false;
Def.Variations.Emplace(Friction);
// Extent
FString Extent("extent");
FString Coordinates[3] = {FString("x"), FString("y"), FString("z")};

View File

@ -35,6 +35,21 @@ void AFrictionTrigger::Init()
}
}
void AFrictionTrigger::UpdateWheelsFriction(AActor *OtherActor, float NewFriction)
{
ACarlaWheeledVehicle *Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
if (Vehicle != nullptr)
{
TArray<float> WheelsFrictionScale = Vehicle->GetWheelsFrictionScale();
for (auto &FrictionScale : WheelsFrictionScale)
{
FrictionScale = NewFriction;
}
Vehicle->SetWheelsFrictionScale(WheelsFrictionScale);
}
}
void AFrictionTrigger::OnTriggerBeginOverlap(
UPrimitiveComponent * /*OverlappedComp*/,
AActor *OtherActor,
@ -43,17 +58,7 @@ void AFrictionTrigger::OnTriggerBeginOverlap(
bool /*bFromSweep*/,
const FHitResult & /*SweepResult*/)
{
ACarlaWheeledVehicle *Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
if (Vehicle != nullptr)
{
TArray<float> WheelsFrictionScale = Vehicle->GetWheelsFrictionScale();
for (auto &FrictionScale : WheelsFrictionScale)
{
FrictionScale = 0.0f;
}
Vehicle->SetWheelsFrictionScale(WheelsFrictionScale);
}
UpdateWheelsFriction(OtherActor, Friction);
}
void AFrictionTrigger::OnTriggerEndOverlap(
@ -62,17 +67,8 @@ void AFrictionTrigger::OnTriggerEndOverlap(
UPrimitiveComponent * /*OtherComp*/,
int32 /*OtherBodyIndex*/)
{
ACarlaWheeledVehicle *Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
if (Vehicle != nullptr)
{
TArray<float> WheelsFrictionScale = Vehicle->GetWheelsFrictionScale();
for (auto &FrictionScale : WheelsFrictionScale)
{
FrictionScale = 3.5f;
}
Vehicle->SetWheelsFrictionScale(WheelsFrictionScale);
}
// Set Back Default Friction Value
UpdateWheelsFriction(OtherActor, 3.5f);
}
// Called when the game starts or when spawned

View File

@ -18,6 +18,8 @@ private:
void Init();
void UpdateWheelsFriction(AActor *OtherActor, float NewFriction);
public:
AFrictionTrigger(const FObjectInitializer &ObjectInitializer);
@ -48,6 +50,11 @@ public:
TriggerVolume->SetBoxExtent(Extent);
}
void SetFriction(float NewFriction)
{
Friction = NewFriction;
}
protected:
virtual void BeginPlay() override;
@ -56,6 +63,9 @@ protected:
public:
UPROPERTY(EditAnywhere)
float Friction = 0.0f;
UPROPERTY(EditAnywhere)
UBoxComponent *TriggerVolume;

View File

@ -62,29 +62,29 @@ FActorSpawnResult ATriggerFactory::SpawnActor(
}
else
{
// Retrieve Episode
auto *Episode = GameInstance->GetCarlaEpisode();
check(Episode != nullptr);
Trigger->SetEpisode(*Episode);
// Retrieve Friction
float Friction = UActorBlueprintFunctionLibrary::RetrieveActorAttributeToFloat("friction",
Description.Variations,
0.0f);
Trigger->SetFriction(Friction);
// Retrieve Extent
FVector Extent {100.0f, 100.0f, 100.0f};
auto *ExtentX = Description.Variations.Find("extent_x");
if (ExtentX != nullptr)
{
Extent.X = std::atof(TCHAR_TO_UTF8(*ExtentX->Value));
}
auto *ExtentY = Description.Variations.Find("extent_y");
if (ExtentY != nullptr)
{
Extent.Y = std::atof(TCHAR_TO_UTF8(*ExtentY->Value));
}
auto *ExtentZ = Description.Variations.Find("extent_z");
if (ExtentZ != nullptr)
{
Extent.Z = std::atof(TCHAR_TO_UTF8(*ExtentZ->Value));
}
Extent.X = UActorBlueprintFunctionLibrary::RetrieveActorAttributeToFloat("extent_x",
Description.Variations,
Extent.X);
Extent.Y = UActorBlueprintFunctionLibrary::RetrieveActorAttributeToFloat("extent_y",
Description.Variations,
Extent.Y);
Extent.Z = UActorBlueprintFunctionLibrary::RetrieveActorAttributeToFloat("extent_z",
Description.Variations,
Extent.Z);
Trigger->SetBoxExtent(Extent);
}