Skip to content

Commit

Permalink
Solve post-impact collapse problem by adding wind simulation (#1218)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xi3Chen authored Jan 24, 2025
1 parent 70505e6 commit d1bedfb
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include "FDMTypes.h"

const FSimpleWindState FSimpleWindState::Calm = FSimpleWindState();
const FSimpleWindState FSimpleWindState::StandardEastZephyr = FSimpleWindState(ETurbType::Standard, 0.0, 0.0, FVector::RightVector, 0.0);
const FSimpleWindState FSimpleWindState::StandardWestZephyr = FSimpleWindState(ETurbType::Standard, 0.0, 0.0, FVector::RightVector * -1.0, 0.0);
const FSimpleWindState FSimpleWindState::StandardNorthZephyr = FSimpleWindState(ETurbType::Standard, 0.0, 0.0, FVector::ForwardVector, 0.0);
const FSimpleWindState FSimpleWindState::StandardSouthZephyr = FSimpleWindState(ETurbType::Standard, 0.0, 0.0, FVector::ForwardVector * -1.0, 0.0);
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,41 @@ void UJSBSimMovementComponent::CommandConsoleBatch(TArray<FString> Property, TAr
//}
}

void UJSBSimMovementComponent::SetWind(FSimpleWindState WindState)
{
auto ConvertUnrealWindModeToFGWindMode = [](ETurbType Unreal) {
switch (Unreal)
{
case ETurbType::None:
return JSBSim::FGWinds::ttNone;
break;
case ETurbType::Standard:
return JSBSim::FGWinds::ttStandard;
break;
case ETurbType::Culp:
return JSBSim::FGWinds::ttCulp;
break;
case ETurbType::Milspec:
return JSBSim::FGWinds::ttMilspec;
break;
case ETurbType::Tustin:
return JSBSim::FGWinds::ttTustin;
break;
default:
break;
}
return JSBSim::FGWinds::ttNone;
};
if (Winds)
{
Winds->SetTurbType(ConvertUnrealWindModeToFGWindMode(WindState.TurbType));
Winds->SetTurbGain(WindState.TurbGain);
Winds->SetTurbRate(WindState.TurbRate);
Winds->SetWindNED(*(FGColumnVector3*)&WindState.WindNED);
Winds->SetProbabilityOfExceedence(WindState.ProbabilityOfExceedence);
}
}



void UJSBSimMovementComponent::LoadAircraft(bool ResetToDefaultSettings)
Expand Down Expand Up @@ -312,8 +347,16 @@ void UJSBSimMovementComponent::TickComponent(float DeltaTime, ELevelTick TickTyp
// Update the ForwardHorizontal vector used for the PFD
AircraftState.UEForwardHorizontal = ENUTransform.TransformVector(ECEFForwardHorizontal);

// Apply the transform to the Parent actor
Parent->SetActorLocationAndRotation(EngineLocation, EngineRotationQuat);
// Apply the transform to the Parent actor
if (EngineLocation.ContainsNaN() || EngineRotationQuat.ContainsNaN())
{
CrashedEvent();
}
else
{
Parent->SetActorLocationAndRotation(EngineLocation, EngineRotationQuat);

}
}

// Basic debugging string and symbols
Expand Down Expand Up @@ -694,9 +737,7 @@ void UJSBSimMovementComponent::CopyFromJSBSim()
AircraftState.AltitudeAGLFt = Propagate->GetDistanceAGL();
// force a sim crashed if crashed (altitude AGL < 0)
if (AircraftState.AltitudeAGLFt < -10.0 || AircraftState.AltitudeASLFt < -10.0) {
Exec->SuspendIntegration();
AircraftState.Crashed = true;
AircraftCrashed.Broadcast();
CrashedEvent();
}

// Copy the fuel levels from JSBSim if fuel
Expand Down Expand Up @@ -1045,6 +1086,13 @@ void UJSBSimMovementComponent::GetEnginesStates()

/////////// Logging and Debugging Methods

void UJSBSimMovementComponent::CrashedEvent()
{
Exec->SuspendIntegration();
AircraftState.Crashed = true;
AircraftCrashed.Broadcast();
}

void UJSBSimMovementComponent::LogInitialization()
{
UE_LOG(LogJSBSim, Display, TEXT("Initialized JSB Sim with : "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,3 +433,70 @@ struct FAircraftState
SpoilersPosition = 0;
}
};

//ue TurbType
UENUM(BlueprintType)
enum class ETurbType:uint8
{
//No turbulence model is used. The aircraft will not experience any random wind effects. This mode can be beneficial for initial testing or when the influence of wind needs to be simplified.
None = 0 UMETA(DisplayName ="turbulence disabled"),
//A basic turbulence model that uses standard statistical characteristics to simulate wind fluctuations. This model is generally used for simple flight simulations.
Standard UMETA(DisplayName = "Ordinary turbulence"),
//The Culp turbulence model typically provides a more detailed simulation of turbulence (considering factors such as turbulence intensity and frequency). The Culp model adapts well to different aircraft and flight conditions, making it suitable for more complex simulations.
Culp UMETA(DisplayName = "Culp turbulence model"),
//This model uses the Dryden spectrum to simulate turbulence, adhering to the guidelines set in the MIL-F-8785C document. The parameters are designed differently for flights at altitudes below 1000 feet and above 2000 feet, with linear interpolation applied for altitudes in between. This model is well-suited for military applications and scenarios where specific turbulence characteristics are required.
Milspec UMETA(DisplayName = "Milspec turbulence model (Dryden spectrum)"),
//Similar to ttMilspec, this model also uses the Dryden spectrum. The main difference lies in how the transfer functions are implemented based on the specifications in the military document. It helps in simulating realistic turbulence under similar conditions as the Milspec model.
Tustin UMETA(DisplayName = "Tustin turbulence model (Dryden spectrum)") ,

};
/*
* Wind State
* I hope that in the future there will be a new class that can provide more settings. However, this data is sufficient for now.
*/
USTRUCT(BlueprintType)
struct JSBSIMFLIGHTDYNAMICSMODEL_API FSimpleWindState
{
GENERATED_BODY()
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Wind")
ETurbType TurbType = ETurbType::None;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Wind")
double TurbGain = 0.0;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Wind")
double TurbRate = 0.0;
//unit knots
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Wind")
FVector WindNED = FVector::ZeroVector;
UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Wind")
double ProbabilityOfExceedence = 0.0;

FSimpleWindState() = default;

FSimpleWindState(ETurbType InTurbType, double InTurbGain, double InTurbRate, FVector InWindNED,
double InProbabilityOfExceedence) : TurbType(InTurbType), TurbGain(InTurbGain), TurbRate(InTurbRate),
WindNED(InWindNED),
ProbabilityOfExceedence(InProbabilityOfExceedence)
{

}
FString GetDebugMessage()const
{
FString DebugMessage;
DebugMessage += FString::Printf(TEXT("TurbType %d TurbGain %.2f TurbRate %.2f WindNED %s ProbabilityOfExceedence %.2f"), TurbType, TurbGain, TurbRate, *WindNED.ToString(), ProbabilityOfExceedence);
return DebugMessage;
}

static const FSimpleWindState Calm;
// East wind, a wind speed that people perceive as relatively strong.
static const FSimpleWindState StandardEastZephyr;
// West wind, a wind speed that people perceive as relatively strong.
static const FSimpleWindState StandardWestZephyr;
//North wind, a wind speed that people perceive as relatively strong.
static const FSimpleWindState StandardNorthZephyr;
//South wind, a wind speed that people perceive as relatively strong.
static const FSimpleWindState StandardSouthZephyr;



};

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Fill out your copyright notice in the Description page of Project Settings.
// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

Expand Down Expand Up @@ -243,8 +243,13 @@ class JSBSIMFLIGHTDYNAMICSMODEL_API UJSBSimMovementComponent : public UActorComp
* otherwise you will override the system value! */
UFUNCTION(BlueprintCallable, DisplayName = "Command Console Batch")
void CommandConsoleBatch(TArray<FString> Property, TArray<FString> InValue, TArray<FString>& OutValue);


/**
* Set environmental wind parameters
*
*/
UFUNCTION(BlueprintCallable, DisplayName = "Set Winds")
void SetWind(FSimpleWindState WindState);

protected:
// Called when the game starts
virtual void BeginPlay() override;
Expand Down Expand Up @@ -342,6 +347,9 @@ class JSBSIMFLIGHTDYNAMICSMODEL_API UJSBSimMovementComponent : public UActorComp
void GetEnginesStates();


//aircrafts
void CrashedEvent();

/////////// Logging and Debugging Methods
void LogInitialization();

Expand Down

0 comments on commit d1bedfb

Please sign in to comment.