UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SlateSprings.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6
10template< typename FloatType >
12{
13
14public:
15
20 {
21
22 public:
23
25 FloatType SpringConstant;
26
28 FloatType SpringLength;
29
31 FloatType DampConstant;
32
34 FloatType SnappingEpsilon;
35
39
40
43 : SpringConstant( FloatType( 20.0 ) ),
44 SpringLength( FloatType( 0.0 ) ),
45 DampConstant( FloatType( 0.5 ) ),
46 SnappingEpsilon( FloatType( 0.01 ) ),
48 {
49 }
50 };
51
52
53
58 : Position( 0.0 ),
59 Target( 0.0 ),
60 PreviousTarget( 0.0 )
61 {
62 }
63
64
68 TSpring1D( FloatType InPosition )
69 : Position( InPosition ),
70 Target( InPosition ),
71 PreviousTarget( InPosition )
72 {
73 }
74
75
82 {
83 Config = InConfig;
84 }
85
86
92 void SetPosition( FloatType InPosition )
93 {
94 Position = InPosition;
95 Target = InPosition;
96 PreviousTarget = InPosition;
97 }
98
99
105 FloatType GetPosition() const
106 {
107 return Position;
108 }
109
110
116 void SetTarget( FloatType InTarget )
117 {
118 Target = InTarget;
119 }
120
121
127 FloatType GetTarget() const
128 {
129 return Target;
130 }
131
133 bool IsAtRest()
134 {
135 return Target == Position;
136 }
137
143 void Tick( float InQuantum )
144 {
145 const float MaxQuantum = 1.0f / 8.0f;
146 if( InQuantum > MaxQuantum )
147 {
148 // If we are configured to reset the spring's state to the new position immediately upon
149 // a hitch, then we'll do that now.
150 if( Config.bSkipAnimationOnHitches )
151 {
152 Position = PreviousTarget = Target;
153 }
154 else
155 {
156 // Not asked to reset on large quantums, so we'll instead clamp the quantum so that
157 // the spring does not behave erratically. (slow motion)
159 }
160 }
161
162 const FloatType Disp = Target - Position;
163 const FloatType DispLength = FMath::Abs( Disp );
164 if( DispLength > Config.SnappingEpsilon )
165 {
166 const FloatType ForceDirection = Disp >= 0.0f ? 1.0f : -1.0f;
167 const FloatType TargetDisp = Target - PreviousTarget;
168 const FloatType VelocityOfTarget = TargetDisp * InQuantum;
169 const FloatType DistBetweenDisplacements = FMath::Abs( Disp - VelocityOfTarget );
170
171 const FloatType ForceAmount =
172 Config.SpringConstant * FMath::Max< FloatType >( 0.0, DispLength - Config.SpringLength ) +
174
175 // We use Min here to prevent overshoots
176 Position += ForceDirection * FMath::Min( DispLength, ForceAmount * InQuantum );
177 }
178
179 // Snap new position to target if we're close enough
180 if( FMath::Abs( Position - Target ) < Config.SnappingEpsilon )
181 {
182 Position = Target;
183 }
184
185 PreviousTarget = Target;
186 }
187
188private:
189
191 FSpringConfig Config;
192
194 FloatType Position;
195
197 FloatType Target;
198
200 FloatType PreviousTarget;
201
202};
203
206
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
return true
Definition ExternalRpcRegistry.cpp:601
TSpring1D< float > FFloatSpring1D
Definition SlateSprings.h:205
TSpring1D< double > FDoubleSpring1D
Definition SlateSprings.h:208
Definition SlateSprings.h:20
FSpringConfig()
Definition SlateSprings.h:42
FloatType DampConstant
Definition SlateSprings.h:31
FloatType SpringConstant
Definition SlateSprings.h:25
bool bSkipAnimationOnHitches
Definition SlateSprings.h:38
FloatType SpringLength
Definition SlateSprings.h:28
FloatType SnappingEpsilon
Definition SlateSprings.h:34
Definition SlateSprings.h:12
void SetPosition(FloatType InPosition)
Definition SlateSprings.h:92
void SetTarget(FloatType InTarget)
Definition SlateSprings.h:116
void SetConfig(const FSpringConfig &InConfig)
Definition SlateSprings.h:81
TSpring1D()
Definition SlateSprings.h:57
bool IsAtRest()
Definition SlateSprings.h:133
FloatType GetTarget() const
Definition SlateSprings.h:127
void Tick(float InQuantum)
Definition SlateSprings.h:143
FloatType GetPosition() const
Definition SlateSprings.h:105
TSpring1D(FloatType InPosition)
Definition SlateSprings.h:68