UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PBDRigidParticles.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
6#include "Chaos/Rotation.h"
7
8#if defined(_MSC_VER)
9#pragma warning(push)
10#pragma warning(disable:4946)
11#endif
12
13namespace Chaos
14{
15template<class T, int d>
16class TPBDRigidsEvolution;
17
19
20template<class T, int d>
22{
23 friend class TPBDRigidsEvolution<T, d>;
24
25 public:
28 using TRigidParticles<T, d>::Sleeping;
29
39 , MP(MoveTemp(Other.MP))
40 , MQ(MoveTemp(Other.MQ))
41 , MPreV(MoveTemp(Other.MPreV))
42 , MPreW(MoveTemp(Other.MPreW))
43 , MSolverBodyIndex(MoveTemp(Other.MSolverBodyIndex))
44 {
47 }
48
50 {
51 }
52
61
62 UE_DEPRECATED(5.4, "Use GetP instead")
63 FORCEINLINE const TVector<T, d>& P(const int32 index) const { return MP[index]; }
64 UE_DEPRECATED(5.4, "Use GetP or SetP instead")
65 FORCEINLINE TVector<T, d>& P(const int32 index) { return MP[index]; }
66 FORCEINLINE const TVector<T, d>& GetP(const int32 index) const { return MP[index]; }
67 FORCEINLINE void SetP(const int32 index, const TVector<T, d>& InP) { MP[index] = InP; }
68
69 UE_DEPRECATED(5.4, "Use GetQ instead")
70 FORCEINLINE const TRotation<T, d> Q(const int32 index) const { return TRotation<T, d>(MQ[index]); }
71 UE_DEPRECATED(5.4, "Use GetQ or SetQ instead")
72 FORCEINLINE TRotation<T, d> Q(const int32 index) { return TRotation<T, d>(MQ[index]); }
73 FORCEINLINE const TRotation<T, d> GetQ(const int32 index) const { return TRotation<T, d>(MQ[index]); }
74 FORCEINLINE void SetQ(const int32 index, const TRotation<T, d>& InQ) { MQ[index] = TRotation<FRealSingle, d>(InQ); }
75 FORCEINLINE const TRotation<FRealSingle, d> GetQf(const int32 index) const { return MQ[index]; }
76 FORCEINLINE void SetQf(const int32 index, const TRotation<FRealSingle, d>& InQ) { MQ[index] = InQ; }
77
78 UE_DEPRECATED(5.4, "Use GetPreV instead")
79 const TVector<T, d> PreV(const int32 index) const { return TVector<T, d>(MPreV[index]); }
80 UE_DEPRECATED(5.4, "Use GetPreV or SetPreV instead")
81 TVector<T, d> PreV(const int32 index) { return TVector<T, d>(MPreV[index]); }
82 const TVector<T, d> GetPreV(const int32 index) const { return TVector<T, d>(MPreV[index]); }
83 void SetPreV(const int32 index, const TVector<T, d>& InPreV) { MPreV[index] = TVector<FRealSingle, d>(InPreV); }
84 const TVector<FRealSingle, d> GetPreVf(const int32 index) const { return MPreV[index]; }
85 void SetPreVf(const int32 index, const TVector<FRealSingle, d>& InPreV) { MPreV[index] = InPreV; }
86
87 UE_DEPRECATED(5.4, "Use GetPreW instead")
88 const TVector<T, d> PreW(const int32 index) const { return MPreW[index]; }
89 UE_DEPRECATED(5.4, "Use GetPreW or SetPreW instead")
90 TVector<T, d> PreW(const int32 index) { return MPreW[index]; }
91 const TVector<T, d> GetPreW(const int32 index) const { return MPreW[index]; }
92 void SetPreW(const int32 index, const TVector<T, d>& InPreW) { MPreW[index] = TVector<FRealSingle, d>(InPreW); }
93 const TVector<FRealSingle, d> GetPreWf(const int32 index) const { return MPreW[index]; }
94 void SetPreWf(const int32 index, const TVector<FRealSingle, d>& InPreW) { MPreW[index] = InPreW; }
95
96 // World-space center of mass location
97 const TVector<T, d> XCom(const int32 index) const { return this->GetX(index) + this->GetR(index).RotateVector(CenterOfMass(index)); }
98 const TVector<T, d> PCom(const int32 index) const { return this->GetP(index) + this->GetQ(index).RotateVector(CenterOfMass(index)); }
99
100 // World-space center of mass rotation
101 const TRotation<T, d> RCom(const int32 index) const { return this->GetR(index) * RotationOfMass(index); }
102 const TRotation<T, d> QCom(const int32 index) const { return this->GetQ(index) * RotationOfMass(index); }
103
105 {
106 SetQ(index, InQCom * RotationOfMass(index).Inverse());
107 SetP(index, InPCom - GetQ(index) * CenterOfMass(index));
108 }
109
110 // The index into an FSolverBodyContainer (for dynamic particles only), or INDEX_NONE.
111 // \see FSolverBodyContainer
112 int32 SolverBodyIndex(const int32 index) const { return MSolverBodyIndex[index]; }
113 void SetSolverBodyIndex(const int32 index, const int32 InSolverBodyIndex) { MSolverBodyIndex[index] = InSolverBodyIndex; }
114
115 // Must be reinterpret cast instead of static_cast as it's a forward declare
117 const THandleType* Handle(int32 Index) const { return reinterpret_cast<const THandleType*>(TGeometryParticles<T,d>::Handle(Index)); }
118
119 //cannot be reference because double pointer would allow for badness, but still useful to have non const access to handle
121
122 void SetSleeping(int32 Index, bool bSleeping)
123 {
124 if (Sleeping(Index) && bSleeping == false)
125 {
126 SetPreV(Index, this->GetV(Index));
127 SetPreW(Index, this->GetW(Index));
128 }
129 else if (bSleeping)
130 {
131 //being put to sleep, so zero out velocities
132 this->SetV(Index, FVec3(0));
133 this->SetW(Index, FVec3(0));
134 }
135
137 if (CurrentlySleeping != bSleeping)
138 {
139 TGeometryParticleHandle<T, d>* Particle = reinterpret_cast<TGeometryParticleHandle<T, d>*>(this->Handle(Index));
140 this->AddSleepData(Particle, bSleeping);
141 }
142
143 // Dynamic -> Sleeping or Sleeping -> Dynamic
145 {
147 }
148
149 // Possible for code to set a Dynamic to Kinematic State then to Sleeping State
150 if (this->ObjectState(Index) == EObjectStateType::Kinematic && bSleeping)
151 {
153 }
154
155 if (bSleeping)
156 {
158 }
159 }
160
162 {
163 const EObjectStateType CurrentState = this->ObjectState(Index);
164
165 if (CurrentState == EObjectStateType::Uninitialized)
166 {
167 // When the state is first initialized, treat it like a static.
168 this->InvM(Index) = 0.0f;
169 this->InvI(Index) = TVec3<FRealSingle>(0);
170 }
171
173 {
174 // Transitioning from dynamic to static or kinematic, set inverse mass and inertia tensor to zero.
175 this->InvM(Index) = 0.0f;
176 this->InvI(Index) = TVec3<FRealSingle>(0);
177 }
179 {
180 // Transitioning from kinematic or static to dynamic, compute the inverses.
181 this->InvM(Index) = FMath::IsNearlyZero(this->M(Index)) ? 0.0f : 1.f / this->M(Index);
182 this->InvI(Index) = (this->I(Index).IsNearlyZero() || FMath::IsNearlyZero(this->M(Index))) ? TVec3<FRealSingle>::ZeroVector :
184 1.f / this->I(Index)[0],
185 1.f / this->I(Index)[1],
186 1.f / this->I(Index)[2]);
187
188 this->SetP(Index, this->GetX(Index));
189 this->SetQf(Index, this->GetRf(Index));
190 }
192 {
193 SetSleeping(Index, true);
194 return;
195 }
196
197 const bool bCurrentSleeping = this->ObjectState(Index) == EObjectStateType::Sleeping;
200 {
201 TGeometryParticleHandle<T, d>* Particle = reinterpret_cast<TGeometryParticleHandle<T, d>*>(this->Handle(Index));
202 this->AddSleepData(Particle, bNewSleeping);
203 }
204
205 this->ObjectState(Index) = InObjectState;
206 }
207
217
218 FString ToString(int32 index) const
219 {
221 return FString::Printf(TEXT("%s, MP:%s, MQ:%s, MPreV:%s, MPreW:%s"), *BaseString, *GetP(index).ToString(), *GetQ(index).ToString(), *GetPreV(index).ToString(), *GetPreW(index).ToString());
222 }
223
224 virtual void Serialize(FChaosArchive& Ar) override
225 {
227
228 Ar << MP;
229
231 if (Ar.CustomVer(FUE5ReleaseStreamObjectVersion::GUID) >= FUE5ReleaseStreamObjectVersion::SinglePrecisonParticleDataPT)
232 {
233 Ar << MQ << MPreV << MPreW;
234 }
235 else
236 {
238 QDouble.Resize(MQ.Num());
239 for (int32 Index = 0; Index < MQ.Num(); ++Index)
240 {
242 }
244 PreVDouble.Resize(MPreV.Num());
245 for (int32 Index = 0; Index < MPreV.Num(); ++Index)
246 {
248 }
250 PreWDouble.Resize(MPreW.Num());
251 for (int32 Index = 0; Index < MPreW.Num(); ++Index)
252 {
254 }
255
256 Ar << QDouble << PreVDouble << PreWDouble;
257
258 MQ.Resize(QDouble.Num());
259 for (int32 Index = 0; Index < QDouble.Num(); ++Index)
260 {
262 }
263 MPreV.Resize(PreVDouble.Num());
264 for (int32 Index = 0; Index < PreVDouble.Num(); ++Index)
265 {
267 }
268 MPreW.Resize(PreWDouble.Num());
269 for (int32 Index = 0; Index < PreWDouble.Num(); ++Index)
270 {
272 }
273 }
274 }
275
276 private:
281 TArrayCollectionArray<int32> MSolverBodyIndex; // Transient for use in constraint solver
282};
283
285
286template <typename T, int d>
288{
289 Particles.Serialize(Ar);
290 return Ar;
291}
292}
293
294#if defined(_MSC_VER)
295#pragma warning(pop)
296#endif
#define FORCEINLINE
Definition AndroidPlatform.h:140
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
#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
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition ChaosArchive.h:167
Definition ArrayCollectionArray.h:15
void Resize(const int Num) override
Definition ArrayCollectionArray.h:79
int32 AddArray(TArrayCollectionArrayBase *Array)
Definition ArrayCollection.h:36
Definition ParticleHandle.h:436
FORCEINLINE THandleType * Handle(int32 Index) const
Definition GeometryParticles.h:474
EParticleType MParticleType
Definition GeometryParticles.h:637
void SetV(const int32 Index, const TVector< T, d > &InV)
Definition KinematicGeometryParticles.h:39
const TVector< T, d > GetW(const int32 Index) const
Definition KinematicGeometryParticles.h:47
const TVector< T, d > GetV(const int32 Index) const
Definition KinematicGeometryParticles.h:38
void SetW(const int32 Index, const TVector< T, d > &InW)
Definition KinematicGeometryParticles.h:48
Definition ParticleHandle.h:987
Definition PBDRigidParticles.h:22
const TVector< T, d > PreW(const int32 index) const
Definition PBDRigidParticles.h:88
FORCEINLINE const TVector< T, d > & GetP(const int32 index) const
Definition PBDRigidParticles.h:66
TPBDRigidParticleHandle< T, d > THandleType
Definition PBDRigidParticles.h:116
void SetSleepType(int32 Index, ESleepType InSleepType)
Definition PBDRigidParticles.h:208
FORCEINLINE void SetQf(const int32 index, const TRotation< FRealSingle, d > &InQ)
Definition PBDRigidParticles.h:76
void SetObjectState(int32 Index, EObjectStateType InObjectState)
Definition PBDRigidParticles.h:161
const TVector< FRealSingle, d > GetPreVf(const int32 index) const
Definition PBDRigidParticles.h:84
void SetPreWf(const int32 index, const TVector< FRealSingle, d > &InPreW)
Definition PBDRigidParticles.h:94
FORCEINLINE const TRotation< FRealSingle, d > GetQf(const int32 index) const
Definition PBDRigidParticles.h:75
FORCEINLINE const TVector< T, d > & P(const int32 index) const
Definition PBDRigidParticles.h:63
void SetTransformPQCom(const int32 index, const TVector< T, d > &InPCom, const TRotation< T, d > &InQCom)
Definition PBDRigidParticles.h:104
void SetPreVf(const int32 index, const TVector< FRealSingle, d > &InPreV)
Definition PBDRigidParticles.h:85
FORCEINLINE void SetQ(const int32 index, const TRotation< T, d > &InQ)
Definition PBDRigidParticles.h:74
FORCEINLINE const TRotation< T, d > GetQ(const int32 index) const
Definition PBDRigidParticles.h:73
TPBDRigidParticles()
Definition PBDRigidParticles.h:30
const TVector< T, d > XCom(const int32 index) const
Definition PBDRigidParticles.h:97
THandleType * Handle(int32 Index)
Definition PBDRigidParticles.h:120
FORCEINLINE const TRotation< T, d > Q(const int32 index) const
Definition PBDRigidParticles.h:70
const TVector< T, d > GetPreV(const int32 index) const
Definition PBDRigidParticles.h:82
TPBDRigidParticles(const TPBDRigidParticles< T, d > &Other)=delete
void SetPreV(const int32 index, const TVector< T, d > &InPreV)
Definition PBDRigidParticles.h:83
FORCEINLINE void SetP(const int32 index, const TVector< T, d > &InP)
Definition PBDRigidParticles.h:67
void SetPreW(const int32 index, const TVector< T, d > &InPreW)
Definition PBDRigidParticles.h:92
int32 SolverBodyIndex(const int32 index) const
Definition PBDRigidParticles.h:112
const TVector< T, d > PreV(const int32 index) const
Definition PBDRigidParticles.h:79
const TRotation< T, d > RCom(const int32 index) const
Definition PBDRigidParticles.h:101
const TRotation< T, d > QCom(const int32 index) const
Definition PBDRigidParticles.h:102
const TVector< T, d > GetPreW(const int32 index) const
Definition PBDRigidParticles.h:91
FString ToString(int32 index) const
Definition PBDRigidParticles.h:218
const THandleType * Handle(int32 Index) const
Definition PBDRigidParticles.h:117
TPBDRigidParticles(TPBDRigidParticles< T, d > &&Other)
Definition PBDRigidParticles.h:37
const TVector< FRealSingle, d > GetPreWf(const int32 index) const
Definition PBDRigidParticles.h:93
virtual ~TPBDRigidParticles()
Definition PBDRigidParticles.h:49
void SetSleeping(int32 Index, bool bSleeping)
Definition PBDRigidParticles.h:122
const TVector< T, d > PCom(const int32 index) const
Definition PBDRigidParticles.h:98
void RegisterArrays()
Definition PBDRigidParticles.h:53
void SetSolverBodyIndex(const int32 index, const int32 InSolverBodyIndex)
Definition PBDRigidParticles.h:113
virtual void Serialize(FChaosArchive &Ar) override
Definition PBDRigidParticles.h:224
Definition Particles.h:22
const TVector< T, d > & GetX(const int32 Index) const
Definition Particles.h:156
Definition RigidParticles.h:78
FORCEINLINE ESleepType SleepType(const int32 Index) const
Definition RigidParticles.h:244
FORCEINLINE const TVec3< FRealSingle > & InvI(const int32 Index) const
Definition RigidParticles.h:177
FORCEINLINE const TRotation< T, d > & RotationOfMass(const int32 Index) const
Definition RigidParticles.h:192
virtual void Serialize(FChaosArchive &Ar) override
Definition RigidParticles.h:297
FORCEINLINE const T M(const int32 Index) const
Definition RigidParticles.h:183
FORCEINLINE void AddSleepData(TGeometryParticleHandle< T, d > *Particle, bool Sleeping)
Definition RigidParticles.h:258
FORCEINLINE const T InvM(const int32 Index) const
Definition RigidParticles.h:186
FORCEINLINE const EObjectStateType ObjectState(const int32 Index) const
Definition RigidParticles.h:276
FORCEINLINE FString ToString(int32 Index) const
Definition RigidParticles.h:288
FORCEINLINE const TVec3< FRealSingle > & I(const int32 Index) const
Definition RigidParticles.h:174
Definition Rotation.h:41
const TArrayCollectionArray< TRotation< FRealSingle, d > > & GetR() const
Definition SimpleGeometryParticles.h:58
FORCEINLINE const TRotation< FRealSingle, d > GetRf(const int32 Index) const
Definition SimpleGeometryParticles.h:56
Definition Vector.h:1000
Definition Vector.h:41
virtual CORE_API void UsingCustomVersion(const struct FGuid &Guid)
Definition Archive.cpp:590
CORE_API int32 CustomVer(const struct FGuid &Key) const
Definition Archive.cpp:602
Definition SkeletalMeshComponent.h:307
FChaosArchive & operator<<(FChaosArchive &Ar, FRigidParticleControlFlags &Flags)
Definition RigidParticleControlFlags.cpp:15
EObjectStateType
Definition ObjectState.h:10
CHAOS_API void EnsureSleepingObjectState(EObjectStateType ObjectState)
Definition PBDRigidParticles.cpp:5
TPBDRigidParticles< FReal, 3 > FPBDRigidParticles
Definition PhysicsProxy.h:23
TVector< FReal, 3 > FVec3
Definition Core.h:17
ESleepType
Definition RigidParticles.h:23
U16 Index
Definition radfft.cpp:71
static UE_FORCEINLINE_HINT bool IsNearlyZero(float Value, float ErrorTolerance=UE_SMALL_NUMBER)
Definition UnrealMathUtility.h:407
CORE_API static const FGuid GUID
Definition UE5ReleaseStreamObjectVersion.h:22