Added ApplyRolloverBehavior function

This commit is contained in:
Guillermo 2022-04-13 12:02:31 +02:00 committed by bernat
parent a58503b9e8
commit 974d240887
2 changed files with 40 additions and 0 deletions

View File

@ -957,3 +957,33 @@ void ACarlaWheeledVehicle::CloseDoorPhys(const EVehicleDoor DoorIdx)
DoorComponent->AttachToComponent(
GetMesh(), FAttachmentTransformRules(EAttachmentRule::KeepWorld, true));
}
void ACarlaWheeledVehicle::ApplyRolloverBehavior()
{
auto roll = GetVehicleTransform().Rotator().Roll;
// The angular velocity reduction is applied in 4 stages, to improve its smoothness
switch (RolloverBehaviorTracker) {
case 0: CheckRollover(roll, std::make_pair(130.0, 230.0)); break;
case 1: CheckRollover(roll, std::make_pair(140.0, 220.0)); break;
case 2: CheckRollover(roll, std::make_pair(150.0, 210.0)); break;
case 3: CheckRollover(roll, std::make_pair(160.0, 200.0)); break;
case 4: break;
default:
RolloverBehaviorTracker = 4;
}
// In case the vehicle recovers, reset the rollover tracker
if (RolloverBehaviorTracker > 0 && -30 < roll && roll < 30){
RolloverBehaviorTracker = 0;
}
}
void ACarlaWheeledVehicle::CheckRollover(const float roll, const std::pair<float, float> threshold_roll){
if (threshold_roll.first < roll && roll < threshold_roll.second){
auto RootComponent = Cast<UPrimitiveComponent>(GetRootComponent());
auto angular_velocity = RootComponent->GetPhysicsAngularVelocityInDegrees();
RootComponent->SetPhysicsAngularVelocity((1 - RolloverBehaviorForce) * angular_velocity);
RolloverBehaviorTracker += 1;
}
}

View File

@ -342,6 +342,9 @@ private:
bool bAckermannControlActive = false;
FAckermannController AckermannController;
float RolloverBehaviorForce = 0.35;
int RolloverBehaviorTracker = 0;
public:
/// Set the rotation of the car wheels indicated by the user
@ -397,4 +400,11 @@ private:
UPROPERTY(Category="CARLA Wheeled Vehicle", VisibleAnywhere, BlueprintReadOnly, meta = (AllowPrivateAccess = "true"))
TMap<UPrimitiveComponent*, UPhysicsConstraintComponent*> CollisionDisableConstraints;
/// Rollovers tend to have too much angular velocity, resulting in the vehicle doing a full 360º flip.
/// This function progressively reduces the vehicle's angular velocity so that it ends up upside down instead.
UFUNCTION(Category = "CARLA Wheeled Vehicle", BlueprintCallable)
void ApplyRolloverBehavior();
void CheckRollover(const float roll, const std::pair<float, float> threshold_roll);
};