Skip to content

Instantly share code, notes, and snippets.

@tonyN7923
Created April 3, 2024 14:32
Show Gist options
  • Save tonyN7923/bd3c5c34207795108371dd74521502bc to your computer and use it in GitHub Desktop.
Save tonyN7923/bd3c5c34207795108371dd74521502bc to your computer and use it in GitHub Desktop.
Utility AI Plugin - Curve Functions
for (int32 PointIndex = 0; PointIndex < NumPoints; ++PointIndex)
{
const float X = PointIndex / (NumPoints - 1.0);
// Process value with the set curve function
float Y = (*CurveFunction.Get())(X, Slope.Get(), Exponent.Get(), HShift.Get(), VShift.Get());
if (bInvert.Get()) Y = 1 - Y;
// Handle Y values outside of normalized graph
if (Y < 0 || Y > 1) continue;
// Handle undefined points
if (FMath::IsNaN(Y)) continue;
Points.Add(PointsTransform.TransformPoint(FVector2D(X, Y)));
}
namespace FUtilityResponseCurveFunctions
{
const FUtilityResponseCurveFunction Default = [](const float X, const float M, const float K, const float B, const float C){ return 0.f; };
const FUtilityResponseCurveFunction Binary = [](const float X, const float M, const float K, const float B, const float C){ return X > 0.5f ? 1.f : 0.f; };
const FUtilityResponseCurveFunction Linear = [](const float X, const float M, const float K, const float B, const float C){ return M * (X - B) + C; };
const FUtilityResponseCurveFunction Polynomial = [](const float X, const float M, const float K, const float B, const float C){ return M * pow(X - B, K) + C; };
const FUtilityResponseCurveFunction Sine = [](const float X, const float M, const float K, const float B, const float C){ return M * sin(UE_HALF_PI * (X - B)) + C; };
const FUtilityResponseCurveFunction Cosine = [](const float X, const float M, const float K, const float B, const float C){ return M * cos(UE_HALF_PI * (X - B)) + C; };
const FUtilityResponseCurveFunction Logistic = [](const float X, const float M, const float K, const float B, const float C){ return C / (1 + pow(UE_EULERS_NUMBER, -K * (X - B))); };
const FUtilityResponseCurveFunction Logit = [](const float X, const float M, const float K, const float B, const float C){ return M * log(X / (1 - X)) + C; };
}
/*
* Declares a type alias for response curve functions.
* Parameters that get passed in:
* X = input value
* M = slope
* K = exponent
* B = horizontal shift
* C = vertical shift
*/
using FUtilityResponseCurveFunction = TFunction<float(float /* X */, float /* M */, float /* K */, float /* B */, float /* C */)>;
namespace FUtilityResponseCurveFunctions
{
extern const FUtilityResponseCurveFunction Default;
extern const FUtilityResponseCurveFunction Binary;
extern const FUtilityResponseCurveFunction Linear;
extern const FUtilityResponseCurveFunction Polynomial;
extern const FUtilityResponseCurveFunction Sine;
extern const FUtilityResponseCurveFunction Cosine;
extern const FUtilityResponseCurveFunction Logistic;
extern const FUtilityResponseCurveFunction Logit;
}
void UUtilityConsideration::SetCurveFunction()
{
if (!CurveFunction) return;
switch (CurvePreset)
{
case EResponseCurvePreset::Binary: CurveFunction = FUtilityResponseCurveFunctions::Binary; break;
case EResponseCurvePreset::Linear: CurveFunction = FUtilityResponseCurveFunctions::Linear; break;
case EResponseCurvePreset::Polynomial: CurveFunction = FUtilityResponseCurveFunctions::Polynomial; break;
case EResponseCurvePreset::Sine: CurveFunction = FUtilityResponseCurveFunctions::Sine; break;
case EResponseCurvePreset::Cosine: CurveFunction = FUtilityResponseCurveFunctions::Cosine; break;
case EResponseCurvePreset::Logistic: CurveFunction = FUtilityResponseCurveFunctions::Logistic; break;
case EResponseCurvePreset::Logit: CurveFunction = FUtilityResponseCurveFunctions::Logit; break;
default: CurveFunction = FUtilityResponseCurveFunctions::Default; break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment