UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
CharacterGroundConstraintContainer.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Chaos/Core.h"
5
11
12namespace ChaosTest
13{
14 class CharacterGroundConstraintContainerTest;
15}
16
17namespace Chaos
18{
19 namespace CVars
20 {
23 }
24
25 class FCharacterGroundConstraintContainer;
26 namespace Private
27 {
28 class FCharacterGroundConstraintContainerSolver;
29 }
30
34 class FCharacterGroundConstraintHandle final : public TIntrusiveConstraintHandle<FCharacterGroundConstraintHandle>
35 {
36 public:
39
44
46 {
47 static FConstraintHandleTypeID STypeID(TEXT("FCharacterGroundConstraintHandle"), &FIndexedConstraintHandle::StaticType());
48 return STypeID;
49 }
50
51 ESyncState GetSyncState() const { return SyncState; }
53
54 EResimType GetResimType() const { return ResimType; }
55 bool GetEnabledDuringResim() const { return bEnabledDuringResim; }
56 void SetEnabledDuringResim(bool bEnabled) { bEnabledDuringResim = bEnabled; }
57
58 virtual void SetEnabled(bool InEnabled) override { bDisabled = !InEnabled; }
59 virtual bool IsEnabled() const { return !bDisabled; }
60
63 const FCharacterGroundConstraintSettings& GetSettings() const { return Settings; }
64 const FCharacterGroundConstraintDynamicData& GetData() const { return Data; }
65
67 {
68 if (!CharacterParticle || bDisabled)
69 {
70 return;
71 }
72
73 Data = InData;
74
75 FVec3 NewLocalCharacterPosition = ComputeLocalCharacterPosition();
76
77 // If the movement target is close to zero and the character position has not
78 // changed by much then it gets clamped to zero and we recompute the delta
79 // position based on the previous target to avoid drift
82 if ((InData.TargetDeltaPosition.SizeSquared() > InputMovementThresholdSq) || ((NewLocalCharacterPosition - LocalCharacterPosition).SizeSquared() > MovementThresholdSq))
83 {
84 LocalCharacterPosition = NewLocalCharacterPosition;
85 }
86 else
87 {
88 if (GroundParticle)
89 {
90 Data.TargetDeltaPosition = GroundParticle->GetR() * LocalCharacterPosition + GroundParticle->GetX() - CharacterParticle->GetX() + Data.TargetDeltaPosition;
91 }
92 else
93 {
94 Data.TargetDeltaPosition = LocalCharacterPosition - CharacterParticle->GetX() + Data.TargetDeltaPosition;
95 }
96 }
97
98 SetDirtyFlag(EDirtyDataFlags::Data);
99 }
100
102 {
103 Settings = InSettings;
104 SetDirtyFlag(EDirtyDataFlags::Settings);
105 }
106
108 {
109 SetDirtyFlag(EDirtyDataFlags::Settings);
110 return Settings;
111 }
112
113 // Declared final so that TPBDConstraintGraphRuleImpl::AddToGraph() does not need to hit vtable
114 virtual FParticlePair GetConstrainedParticles() const override final { return { CharacterParticle, GroundParticle }; }
115 FGeometryParticleHandle* GetCharacterParticle() const { return CharacterParticle; }
116 FGeometryParticleHandle* GetGroundParticle() const { return GroundParticle; }
117
119 {
120 if (InGroundParticle != GroundParticle)
121 {
123 {
125 {
126 if (PBDRigid->Disabled())
127 {
128 InGroundParticle = nullptr;
129 }
130 }
131 }
132
133 if (GroundParticle)
134 {
135 GroundParticle->RemoveConstraintHandle(this);
136 }
137 GroundParticle = InGroundParticle;
138 if (GroundParticle)
139 {
140 GroundParticle->AddConstraintHandle(this);
141 }
142 LocalCharacterPosition = ComputeLocalCharacterPosition();
143
144 SetDirtyFlag(EDirtyDataFlags::GroundParticle);
145 }
146 }
147
148 // Get the force applied by the solver due to this constraint. Units are ML/T^2
149 FVec3 GetSolverAppliedForce() const { return SolverAppliedForce; }
150
151 // Get the torque applied by the solver due to this constraint. Units are ML^2/T^2
152 FVec3 GetSolverAppliedTorque() const { return SolverAppliedTorque; }
153
155 {
156 return IsFlagSet(EDirtyDataFlags::GroundParticle);
157 }
158
160 {
161 return IsFlagSet(EDirtyDataFlags::Settings);
162 }
163
164 bool HasDataChanged() const
165 {
166 return IsFlagSet(EDirtyDataFlags::Data);
167 }
168
170 {
171 DirtyFlags = 0;
172 }
173
174 private:
175 friend class FCharacterGroundConstraintContainer; // For creation
176 friend class FCharacterGroundConstraintProxy; // For setting the data from the game thread constraint
177 friend class Private::FCharacterGroundConstraintContainerSolver; // For setting the solver force and torque
178 friend class ChaosTest::CharacterGroundConstraintContainerTest; // For testing internals
179
180 enum class EDirtyDataFlags : uint8
181 {
182 GroundParticle = 1 << 0,
183 Settings = 1 << 1,
184 Data = 1 << 2
185 };
186 uint8 DirtyFlags;
187 bool IsFlagSet(EDirtyDataFlags Flag) const
188 {
189 return (DirtyFlags & (uint8)Flag) == (uint8)Flag;
190 }
191 void SetDirtyFlag(EDirtyDataFlags Flag)
192 {
193 DirtyFlags |= (uint8)Flag;
194 }
195
196 FVec3 ComputeLocalCharacterPosition()
197 {
198 if (!CharacterParticle || bDisabled)
199 {
200 return FVec3::ZeroVector;
201 }
202
203 FVec3 LocalPos = CharacterParticle->GetX();
204
205 if (GroundParticle)
206 {
207 LocalPos = GroundParticle->GetR().Inverse() * (LocalPos - GroundParticle->GetX());
208 }
209
210 return LocalPos;
211 }
212
213 FCharacterGroundConstraintSettings Settings;
214 FCharacterGroundConstraintDynamicData Data;
215 FVec3 SolverAppliedForce = FVec3::ZeroVector;
216 FVec3 SolverAppliedTorque = FVec3::ZeroVector;
217 FVec3 LocalCharacterPosition = FVec3::ZeroVector;
218 FGeometryParticleHandle* CharacterParticle;
219 FGeometryParticleHandle* GroundParticle;
220 bool bDisabled = false;
221 bool bEnabledDuringResim;
223 ESyncState SyncState = ESyncState::InSync;
224 };
225
228 {
229 public:
233
236
237 int32 NumConstraints() const { return Constraints.Num(); }
238
242 FGeometryParticleHandle* CharacterParticle,
243 FGeometryParticleHandle* GroundParticle = nullptr);
244
246
249
250 FCharacterGroundConstraintHandle* GetConstraint(int32 ConstraintIndex) { check(ConstraintIndex < NumConstraints()); return Constraints[ConstraintIndex]; }
251 const FCharacterGroundConstraintHandle* GetConstraint(int32 ConstraintIndex) const { check(ConstraintIndex < NumConstraints()); return Constraints[ConstraintIndex]; }
252
254 // FConstraintContainer Implementation
257 virtual int32 GetNumConstraints() const override final { return Constraints.Num(); }
258 virtual void ResetConstraints() override final {}
259 CHAOS_API virtual void AddConstraintsToGraph(Private::FPBDIslandManager& IslandManager) override final;
260 CHAOS_API virtual void PrepareTick() override final;
265
267 // Required API from FConstraintContainer
268 CHAOS_API void SetConstraintEnabled(int32 ConstraintIndex, bool bEnabled);
269 bool IsConstraintEnabled(int32 ConstraintIndex) const { check(ConstraintIndex < NumConstraints()); return Constraints[ConstraintIndex]->IsEnabled(); }
270
271 private:
272 CHAOS_API bool CanEvaluate(const FCharacterGroundConstraintHandle* Constraint) const;
273
276 };
277}
constexpr auto MakeArrayView(OtherRangeType &&Other)
Definition ArrayView.h:873
#define check(expr)
Definition AssertionMacros.h:314
#define TEXT(x)
Definition Platform.h:1272
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
uint8_t uint8
Definition binka_ue_file_header.h:8
Container class for all character ground constraints on the physics thread.
Definition CharacterGroundConstraintContainer.h:228
int32 NumConstraints() const
Definition CharacterGroundConstraintContainer.h:237
FConstConstraints GetConstConstraints() const
Definition CharacterGroundConstraintContainer.h:248
CHAOS_API FCharacterGroundConstraintContainer()
Definition CharacterGroundConstraintContainer.cpp:19
FCharacterGroundConstraintHandle * GetConstraint(int32 ConstraintIndex)
Definition CharacterGroundConstraintContainer.h:250
CHAOS_API void RemoveConstraint(FCharacterGroundConstraintHandle *Constraint)
Definition CharacterGroundConstraintContainer.cpp:79
virtual int32 GetNumConstraints() const override final
Definition CharacterGroundConstraintContainer.h:257
virtual CHAOS_API void AddConstraintsToGraph(Private::FPBDIslandManager &IslandManager) override final
Definition CharacterGroundConstraintContainer.cpp:107
CHAOS_API FCharacterGroundConstraintHandle * AddConstraint(const FCharacterGroundConstraintSettings &InConstraintSettings, const FCharacterGroundConstraintDynamicData &InConstraintData, FGeometryParticleHandle *CharacterParticle, FGeometryParticleHandle *GroundParticle=nullptr)
Definition CharacterGroundConstraintContainer.cpp:50
virtual CHAOS_API ~FCharacterGroundConstraintContainer()
Definition CharacterGroundConstraintContainer.cpp:25
bool IsConstraintEnabled(int32 ConstraintIndex) const
Definition CharacterGroundConstraintContainer.h:269
virtual CHAOS_API void PrepareTick() override final
Definition CharacterGroundConstraintContainer.cpp:131
const FCharacterGroundConstraintHandle * GetConstraint(int32 ConstraintIndex) const
Definition CharacterGroundConstraintContainer.h:251
CHAOS_API void SetConstraintEnabled(int32 ConstraintIndex, bool bEnabled)
Definition CharacterGroundConstraintContainer.cpp:29
virtual CHAOS_API TUniquePtr< FConstraintContainerSolver > CreateGroupSolver(const int32 Priority) override final
Definition CharacterGroundConstraintContainer.cpp:102
virtual CHAOS_API void UnprepareTick() override final
Definition CharacterGroundConstraintContainer.cpp:135
virtual void ResetConstraints() override final
Definition CharacterGroundConstraintContainer.h:258
virtual CHAOS_API TUniquePtr< FConstraintContainerSolver > CreateSceneSolver(const int32 Priority) override final
Definition CharacterGroundConstraintContainer.cpp:97
virtual CHAOS_API void OnDisableParticle(FGeometryParticleHandle *DisabledParticle) override final
Definition CharacterGroundConstraintContainer.cpp:176
virtual CHAOS_API void OnEnableParticle(FGeometryParticleHandle *EnabledParticle) override final
Definition CharacterGroundConstraintContainer.cpp:205
FConstraints GetConstraints()
Definition CharacterGroundConstraintContainer.h:247
virtual CHAOS_API void DisconnectConstraints(const TSet< TGeometryParticleHandle< FReal, 3 > * > &RemovedParticles) override final
Definition CharacterGroundConstraintContainer.cpp:139
Definition CharacterGroundConstraintSettings.h:32
FVec3 TargetDeltaPosition
World space ground normal.
Definition CharacterGroundConstraintSettings.h:40
Definition CharacterGroundConstraintContainer.h:35
virtual bool IsEnabled() const
Definition CharacterGroundConstraintContainer.h:59
const FCharacterGroundConstraintDynamicData & GetData() const
Definition CharacterGroundConstraintContainer.h:64
FCharacterGroundConstraintHandle()
Definition CharacterGroundConstraintContainer.h:40
void SetGroundParticle(FGeometryParticleHandle *InGroundParticle)
Definition CharacterGroundConstraintContainer.h:118
bool GetEnabledDuringResim() const
Definition CharacterGroundConstraintContainer.h:55
void SetEnabledDuringResim(bool bEnabled)
Definition CharacterGroundConstraintContainer.h:56
FCharacterGroundConstraintSettings & GetSettings_Mutable()
Definition CharacterGroundConstraintContainer.h:107
void SetData(const FCharacterGroundConstraintDynamicData &InData)
Definition CharacterGroundConstraintContainer.h:66
FVec3 GetSolverAppliedForce() const
Definition CharacterGroundConstraintContainer.h:149
virtual void SetEnabled(bool InEnabled) override
Definition CharacterGroundConstraintContainer.h:58
bool HaveSettingsChanged() const
Definition CharacterGroundConstraintContainer.h:159
ESyncState GetSyncState() const
Definition CharacterGroundConstraintContainer.h:51
friend class ChaosTest::CharacterGroundConstraintContainerTest
Definition CharacterGroundConstraintContainer.h:178
virtual FParticlePair GetConstrainedParticles() const override final
Definition CharacterGroundConstraintContainer.h:114
friend class FCharacterGroundConstraintContainer
Definition CharacterGroundConstraintContainer.h:175
bool HasGroundParticleChanged() const
Definition CharacterGroundConstraintContainer.h:154
const FCharacterGroundConstraintSettings & GetSettings() const
Definition CharacterGroundConstraintContainer.h:63
void SetSyncState(ESyncState InSyncState)
Definition CharacterGroundConstraintContainer.h:52
static const FConstraintHandleTypeID & StaticType()
Definition CharacterGroundConstraintContainer.h:45
FVec3 GetSolverAppliedTorque() const
Definition CharacterGroundConstraintContainer.h:152
void SetSettings(const FCharacterGroundConstraintSettings &InSettings)
Definition CharacterGroundConstraintContainer.h:101
FGeometryParticleHandle * GetCharacterParticle() const
Definition CharacterGroundConstraintContainer.h:115
void ClearDirtyFlags()
Definition CharacterGroundConstraintContainer.h:169
FGeometryParticleHandle * GetGroundParticle() const
Definition CharacterGroundConstraintContainer.h:116
EResimType GetResimType() const
Definition CharacterGroundConstraintContainer.h:54
bool HasDataChanged() const
Definition CharacterGroundConstraintContainer.h:164
Definition CharacterGroundConstraintProxy.h:18
Definition CharacterGroundConstraintSettings.h:9
A type id for constraint handles to support safe up/down casting (including intermediate classes in t...
Definition ConstraintHandle.h:49
static const FConstraintHandleTypeID & StaticType()
Definition IndexedConstraintContainer.h:50
Definition PBDConstraintContainer.h:19
Definition CharacterGroundConstraintContainerSolver.h:13
Definition IslandManager.h:453
Definition ParticleHandle.h:436
void AddConstraintHandle(FConstraintHandle *InConstraintHandle)
Definition ParticleHandle.h:837
void RemoveConstraintHandle(FConstraintHandle *InConstraintHandle)
Definition ParticleHandle.h:842
const TPBDRigidParticleHandleImp< T, d, bPersistent > * CastToRigidParticle() const
Definition ParticleHandle.h:1697
const TVector< T, d > & GetX() const
Definition ParticleHandle.h:558
const TRotation< T, d > GetR() const
Definition ParticleHandle.h:568
Base class for constraints that are allocated at permanent memory addresses and inherit the handle.
Definition ConstraintHandle.h:256
Definition ObjectPool.h:25
Definition ParticleHandle.h:987
Definition Constraints.Build.cs:6
Definition ArrayView.h:139
Definition Array.h:670
Definition UniquePtr.h:107
Definition CharacterGroundConstraintContainer.h:13
float Chaos_CharacterGroundConstraint_ExternalMovementThreshold
Definition CharacterGroundConstraintContainer.cpp:15
float Chaos_CharacterGroundConstraint_InputMovementThreshold
Definition CharacterGroundConstraintContainer.cpp:12
Definition SkeletalMeshComponent.h:307
ESyncState
Definition GeometryParticlesfwd.h:29
EResimType
Definition GeometryParticles.h:143
FRealDouble FReal
Definition Real.h:22
TVector< FReal, 3 > FVec3
Definition Core.h:17
TGeometryParticleHandle< FReal, 3 > FGeometryParticleHandle
Definition ParticleHandleFwd.h:24
Definition OverriddenPropertySet.cpp:45