Fixing friction bug + refactor

This commit is contained in:
Manish 2019-05-06 16:12:27 +02:00
parent 0f2064b58f
commit da9e8d4136
6 changed files with 47 additions and 24 deletions

View File

@ -236,7 +236,7 @@ static void AddVariationsForTrigger(FActorDefinition &Def)
FActorVariation Friction;
Friction.Id = FString("friction");
Friction.Type = EActorAttributeType::Float;
Friction.RecommendedValues = { TEXT("0.0f") };
Friction.RecommendedValues = { TEXT("3.5f") };
Friction.bRestrictToRecommended = false;
Def.Variations.Emplace(Friction);
@ -250,7 +250,7 @@ static void AddVariationsForTrigger(FActorDefinition &Def)
ExtentCoordinate.Id = JoinStrings(TEXT("_"), Extent, Coordinate);
ExtentCoordinate.Type = EActorAttributeType::Float;
ExtentCoordinate.RecommendedValues = { TEXT("0.0f") };
ExtentCoordinate.RecommendedValues = { TEXT("1.0f") };
ExtentCoordinate.bRestrictToRecommended = false;
Def.Variations.Emplace(ExtentCoordinate);

View File

@ -80,12 +80,17 @@ void AFrictionTrigger::BeginPlay()
void AFrictionTrigger::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
// Deregister the delegate
// Deregister delegates
if (TriggerVolume->OnComponentBeginOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerBeginOverlap))
{
TriggerVolume->OnComponentBeginOverlap.RemoveDynamic(this, &AFrictionTrigger::OnTriggerBeginOverlap);
}
if (TriggerVolume->OnComponentEndOverlap.IsAlreadyBound(this, &AFrictionTrigger::OnTriggerEndOverlap))
{
TriggerVolume->OnComponentEndOverlap.RemoveDynamic(this, &AFrictionTrigger::OnTriggerEndOverlap);
}
Super::EndPlay(EndPlayReason);
}

View File

@ -61,6 +61,8 @@ protected:
virtual void EndPlay(EEndPlayReason::Type EndPlayReason) override;
virtual void Tick(float DeltaTime) override;
public:
UPROPERTY(EditAnywhere)
@ -69,7 +71,5 @@ public:
UPROPERTY(EditAnywhere)
UBoxComponent *TriggerVolume;
virtual void Tick(float DeltaTime) override;
const UCarlaEpisode *Episode = nullptr;
};

View File

@ -11,8 +11,6 @@
#include "Carla/Game/CarlaStatics.h"
#include "Carla/Trigger/FrictionTrigger.h"
#include "Carla/Game/CarlaGameInstance.h"
// =============================================================================
// -- ASensorFactory -----------------------------------------------------------
// =============================================================================
@ -48,7 +46,6 @@ FActorSpawnResult ATriggerFactory::SpawnActor(
return {};
}
UE_LOG(LogCarla, Log, TEXT("%s"), *Description.Class->GetName());
auto *Trigger = World->SpawnActorDeferred<AFrictionTrigger>(
Description.Class,
Transform,
@ -70,7 +67,7 @@ FActorSpawnResult ATriggerFactory::SpawnActor(
// Retrieve Friction
float Friction = UActorBlueprintFunctionLibrary::RetrieveActorAttributeToFloat("friction",
Description.Variations,
0.0f);
3.5f);
Trigger->SetFriction(Friction);
// Retrieve Extent

View File

@ -32,9 +32,23 @@ ACarlaWheeledVehicle::~ACarlaWheeledVehicle() {}
void ACarlaWheeledVehicle::BeginPlay()
{
Super::BeginPlay();
// Check if it overlaps with a Friction trigger
float FrictionScale = 3.5f;
TArray<AActor *> OverlapActors;
GetOverlappingActors(OverlapActors, AFrictionTrigger::StaticClass());
for (auto &Actor : OverlapActors)
{
AFrictionTrigger* FrictionTrigger = Cast<AFrictionTrigger>(Actor);
if (FrictionTrigger) {
FrictionScale = FrictionTrigger->Friction;
}
}
// Setup Tire Configs
UWheeledVehicleMovementComponent4W *Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(
UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
GetVehicleMovementComponent());
check (Vehicle4W != nullptr);
// Wheels Setup
TArray<FWheelSetup> NewWheelSetups = Vehicle4W->WheelSetups;
@ -44,15 +58,14 @@ void ACarlaWheeledVehicle::BeginPlay()
UVehicleWheel *Wheel = NewWheelSetups[i].WheelClass.GetDefaultObject();
// Assigning new tire config
float Friction = Wheel->TireConfig->GetFrictionScale();
Wheel->TireConfig = NewObject<UTireConfig>();
// Setting a new value to friction
Wheel->TireConfig->SetFrictionScale(Friction);
Wheel->TireConfig->SetFrictionScale(FrictionScale);
}
Vehicle4W->WheelSetups = NewWheelSetups;
Vehicle4W->VehicleSetupTag++;
Vehicle4W->RecreatePhysicsState();
}
// =============================================================================
@ -160,8 +173,9 @@ void ACarlaWheeledVehicle::SetHandbrakeInput(const bool Value)
TArray<float> ACarlaWheeledVehicle::GetWheelsFrictionScale()
{
UWheeledVehicleMovementComponent4W *Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(
UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
GetVehicleMovement());
check (Vehicle4W != nullptr);
TArray<float> WheelsFrictionScale;
for (auto &Wheel : Vehicle4W->Wheels)
@ -173,8 +187,10 @@ TArray<float> ACarlaWheeledVehicle::GetWheelsFrictionScale()
void ACarlaWheeledVehicle::SetWheelsFrictionScale(TArray<float> &WheelsFrictionScale)
{
UWheeledVehicleMovementComponent4W *Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(
UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
GetVehicleMovement());
check (Vehicle4W != nullptr);
for (int32 i = 0; i < Vehicle4W->Wheels.Num(); ++i)
{
Vehicle4W->Wheels[i]->TireConfig->SetFrictionScale(WheelsFrictionScale[i]);
@ -183,8 +199,9 @@ void ACarlaWheeledVehicle::SetWheelsFrictionScale(TArray<float> &WheelsFrictionS
FVehiclePhysicsControl ACarlaWheeledVehicle::GetVehiclePhysicsControl()
{
UWheeledVehicleMovementComponent4W *Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(
UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
GetVehicleMovement());
check(Vehicle4W != nullptr);
FVehiclePhysicsControl PhysicsControl;
@ -210,6 +227,8 @@ FVehiclePhysicsControl ACarlaWheeledVehicle::GetVehiclePhysicsControl()
// Center of mass offset (Center of mass is always zero vector in local
// position)
UPrimitiveComponent *UpdatedPrimitive = Cast<UPrimitiveComponent>(Vehicle4W->UpdatedComponent);
check(UpdatedPrimitive != nullptr);
PhysicsControl.CenterOfMass = UpdatedPrimitive->BodyInstance.COMNudge;
// Transmission Setup
@ -239,8 +258,9 @@ FVehiclePhysicsControl ACarlaWheeledVehicle::GetVehiclePhysicsControl()
void ACarlaWheeledVehicle::ApplyVehiclePhysicsControl(const FVehiclePhysicsControl &PhysicsControl)
{
UWheeledVehicleMovementComponent4W *Vehicle4W = CastChecked<UWheeledVehicleMovementComponent4W>(
UWheeledVehicleMovementComponent4W *Vehicle4W = Cast<UWheeledVehicleMovementComponent4W>(
GetVehicleMovement());
check(Vehicle4W != nullptr);
// Engine Setup
Vehicle4W->EngineSetup.TorqueCurve.EditorCurveData = PhysicsControl.TorqueCurve;
@ -271,8 +291,9 @@ void ACarlaWheeledVehicle::ApplyVehiclePhysicsControl(const FVehiclePhysicsContr
Vehicle4W->SteeringCurve.EditorCurveData = PhysicsControl.SteeringCurve;
// Wheels Setup
TArray<FWheelSetup> NewWheelSetups;
check(Vehicle4W->WheelSetups.Num() == PhysicsControl.Wheels.Num());
TArray<FWheelSetup> NewWheelSetups;
for (int32 i = 0; i < Vehicle4W->WheelSetups.Num(); ++i)
{
FWheelSetup WheelSetup = Vehicle4W->WheelSetups[i];
@ -284,9 +305,9 @@ void ACarlaWheeledVehicle::ApplyVehiclePhysicsControl(const FVehiclePhysicsContr
WheelSetup.bDisableSteering = !PhysicsControl.Wheels[i].IsSteerable;
NewWheelSetups.Add(WheelSetup);
NewWheelSetups.Add(std::move(WheelSetup));
}
Vehicle4W->WheelSetups = NewWheelSetups;
Vehicle4W->VehicleSetupTag++;
}
Vehicle4W->RecreatePhysicsState();
}

View File

@ -14,13 +14,13 @@ struct CARLA_API FWheelPhysicsControl
GENERATED_BODY()
UPROPERTY(Category = "Wheel Tire Friction", EditAnywhere, BlueprintReadWrite)
float TireFriction = 0.0f;
float TireFriction = 3.5f;
UPROPERTY(Category = "Wheel Damping Rate", EditAnywhere, BlueprintReadWrite)
float DampingRate = 0.0f;
float DampingRate = 1.0f;
UPROPERTY(Category = "Wheel Max Steer Angle", EditAnywhere, BlueprintReadWrite)
float MaxSteerAngle = 0.0f;
float MaxSteerAngle = 70.0f;
UPROPERTY(Category = "Wheel Is Steerable", EditAnywhere, BlueprintReadWrite)
bool IsSteerable = true;