UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Curve.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
5#include "Containers/Array.h"
7
8namespace Chaos
9{
10 struct FCurveKey
11 {
12 float Time;
13 float Value;
14
16 {
17 Ar << Key.Time;
18 Ar << Key.Value;
19 return Ar;
20 }
21 };
22
24 {
25 public:
27
28 FLinearCurve() = default;
29
30 FLinearCurve(std::initializer_list<FCurveKey> InKeys)
31 : Keys(InKeys)
32 {}
33
34 int32 GetNumKeys() const { return Keys.Num(); };
35
36 // Evaluate the curve at a given time using simple linear interpolation
37 float Eval(float InTime) const
38 {
39 const int32 NumKeys = Keys.Num();
40 if (NumKeys == 0)
41 {
42 return 0.f;
43 }
44
45 if (InTime <= Keys[0].Time)
46 {
47 return Keys[0].Value;
48 }
49
50 if (InTime >= Keys[NumKeys - 1].Time)
51 {
52 return Keys[NumKeys - 1].Value;
53 }
54
55 // Linear interpolation between nearest keys
56 for (int32 i = 1; i < NumKeys; ++i)
57 {
58 if (InTime < Keys[i].Time)
59 {
60 const FCurveKey& A = Keys[i - 1];
61 const FCurveKey& B = Keys[i];
62
63 const float Alpha = (InTime - A.Time) / (B.Time - A.Time);
64 return FMath::Lerp(A.Value, B.Value, Alpha);
65 }
66 }
67
68 return 0.f; // Should not reach here
69 }
70
72 {
73 Ar << Keys;
74 return true;
75 }
76
78 {
79 Curve.Serialize(Ar);
80 return Ar;
81 }
82 };
83
84} // namespace Chaos
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition Curve.h:24
bool Serialize(FArchive &Ar)
Definition Curve.h:71
FLinearCurve(std::initializer_list< FCurveKey > InKeys)
Definition Curve.h:30
int32 GetNumKeys() const
Definition Curve.h:34
friend FArchive & operator<<(FArchive &Ar, FLinearCurve &Curve)
Definition Curve.h:77
FLinearCurve()=default
float Eval(float InTime) const
Definition Curve.h:37
TArray< FCurveKey > Keys
Definition Curve.h:26
Definition Archive.h:1208
Definition Array.h:670
Definition SkeletalMeshComponent.h:307
Definition Curve.h:11
float Value
Definition Curve.h:13
friend FArchive & operator<<(FArchive &Ar, FCurveKey &Key)
Definition Curve.h:15
float Time
Definition Curve.h:12
static constexpr UE_FORCEINLINE_HINT T Lerp(const T &A, const T &B, const U &Alpha)
Definition UnrealMathUtility.h:1116