Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Unreal/Plugins/AirSim/Source/Vehicles/Car/CarPawn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,34 @@ void ACarPawn::EndPlay(const EEndPlayReason::Type EndPlayReason)
Super::EndPlay(EndPlayReason);
}

void ACarPawn::FellOutOfWorld(const UDamageType& dmgType)
{
// The engine destroys actors that drop below the level's KillZ, but AirSim keeps using its
// vehicle pawns and crashes on a destroyed one. Keep the vehicle and put it back at its start
// pose on the next tick: this is called from the physics sync, which overrides a teleport here.
if (!fell_out_of_world_) {
UE_LOG(LogTemp, Warning, TEXT("%s fell out of the world; moving it back to its start pose"), *GetName());
UAirBlueprintLib::LogMessage(GetName(), TEXT("fell out of the world and was moved back to its start pose"), LogDebugLevel::Failure);
}
fell_out_of_world_ = true;
}

void ACarPawn::respawnIfFellOutOfWorld()
{
if (!fell_out_of_world_)
return;
fell_out_of_world_ = false;
for (UPrimitiveComponent* phys_comp : UAirBlueprintLib::getPhysicsComponents(this)) {
phys_comp->SetPhysicsLinearVelocity(FVector::ZeroVector);
phys_comp->SetPhysicsAngularVelocityInDegrees(FVector::ZeroVector);
}
SetActorTransform(start_transform_, false, nullptr, ETeleportType::TeleportPhysics);
}

void ACarPawn::Tick(float Delta)
{
Super::Tick(Delta);
respawnIfFellOutOfWorld();

// update physics material
updatePhysicsMaterial();
Expand All @@ -264,6 +289,7 @@ void ACarPawn::Tick(float Delta)
void ACarPawn::BeginPlay()
{
Super::BeginPlay();
start_transform_ = GetActorTransform();

// Start an engine sound playing
engine_sound_audio_->Play();
Expand Down
6 changes: 6 additions & 0 deletions Unreal/Plugins/AirSim/Source/Vehicles/Car/CarPawn.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ACarPawn : public AWheeledVehiclePawn
virtual void BeginPlay() override;
virtual void Tick(float Delta) override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
virtual void FellOutOfWorld(const class UDamageType& dmgType) override;
virtual void NotifyHit(class UPrimitiveComponent* MyComp, class AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation,
FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit) override;

Expand All @@ -53,6 +54,11 @@ class ACarPawn : public AWheeledVehiclePawn
return keyboard_controls_;
}

private:
void respawnIfFellOutOfWorld();
FTransform start_transform_;
bool fell_out_of_world_ = false;

private:
void updateHUDStrings();
void setupVehicleMovementComponent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,34 @@ void ASkidVehiclePawn::EndPlay(const EEndPlayReason::Type EndPlayReason)
Super::EndPlay(EndPlayReason);
}

void ASkidVehiclePawn::FellOutOfWorld(const UDamageType& dmgType)
{
// The engine destroys actors that drop below the level's KillZ, but AirSim keeps using its
// vehicle pawns and crashes on a destroyed one. Keep the vehicle and put it back at its start
// pose on the next tick: this is called from the physics sync, which overrides a teleport here.
if (!fell_out_of_world_) {
UE_LOG(LogTemp, Warning, TEXT("%s fell out of the world; moving it back to its start pose"), *GetName());
UAirBlueprintLib::LogMessage(GetName(), TEXT("fell out of the world and was moved back to its start pose"), LogDebugLevel::Failure);
}
fell_out_of_world_ = true;
}

void ASkidVehiclePawn::respawnIfFellOutOfWorld()
{
if (!fell_out_of_world_)
return;
fell_out_of_world_ = false;
for (UPrimitiveComponent* phys_comp : UAirBlueprintLib::getPhysicsComponents(this)) {
phys_comp->SetPhysicsLinearVelocity(FVector::ZeroVector);
phys_comp->SetPhysicsAngularVelocityInDegrees(FVector::ZeroVector);
}
SetActorTransform(start_transform_, false, nullptr, ETeleportType::TeleportPhysics);
}

void ASkidVehiclePawn::Tick(float Delta)
{
Super::Tick(Delta);
respawnIfFellOutOfWorld();

USkeletalMeshComponent* mesh = CastChecked<USkeletalMeshComponent>(getVehicleMesh());
if (mesh->GetPhysicsAngularVelocityInDegrees().Size() > max_angular_velocity_)
Expand Down Expand Up @@ -274,6 +299,7 @@ void ASkidVehiclePawn::Tick(float Delta)
void ASkidVehiclePawn::BeginPlay()
{
Super::BeginPlay();
start_transform_ = GetActorTransform();

// Start an engine sound playing
engine_sound_audio_->Play();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ASkidVehiclePawn : public AWheeledVehiclePawn
virtual void BeginPlay() override;
virtual void Tick(float Delta) override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
virtual void FellOutOfWorld(const class UDamageType& dmgType) override;
virtual void NotifyHit(class UPrimitiveComponent* MyComp, class AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation,
FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit) override;

Expand Down Expand Up @@ -64,6 +65,11 @@ class ASkidVehiclePawn : public AWheeledVehiclePawn
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "SkidVehicle|General")
bool stop_turn_ = false;

private:
void respawnIfFellOutOfWorld();
FTransform start_transform_;
bool fell_out_of_world_ = false;

private:
void updateHUDStrings();
void setupVehicleMovementComponent();
Expand Down