| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Core/StaticArray.h> |
| 8 |
#include <Jolt/Core/LockFreeHashMap.h> |
| 9 |
#include <Jolt/Physics/EPhysicsUpdateError.h> |
| 10 |
#include <Jolt/Physics/Body/BodyPair.h> |
| 11 |
#include <Jolt/Physics/Collision/Shape/SubShapeIDPair.h> |
| 12 |
#include <Jolt/Physics/Collision/ManifoldBetweenTwoFaces.h> |
| 13 |
#include <Jolt/Physics/Constraints/ConstraintPart/AxisConstraintPart.h> |
| 14 |
#include <Jolt/Physics/Constraints/ConstraintPart/DualAxisConstraintPart.h> |
| 15 |
#include <Jolt/Core/HashCombine.h> |
| 16 |
#include <Jolt/Core/NonCopyable.h> |
| 17 |
|
| 18 |
JPH_SUPPRESS_WARNINGS_STD_BEGIN |
| 19 |
#include <atomic> |
| 20 |
JPH_SUPPRESS_WARNINGS_STD_END |
| 21 |
|
| 22 |
JPH_NAMESPACE_BEGIN |
| 23 |
|
| 24 |
struct PhysicsSettings; |
| 25 |
class PhysicsUpdateContext; |
| 26 |
|
| 27 |
class JPH_EXPORT ContactConstraintManager : public NonCopyable |
| 28 |
{ |
| 29 |
public: |
| 30 |
JPH_OVERRIDE_NEW_DELETE |
| 31 |
|
| 32 |
|
| 33 |
explicit ContactConstraintManager(const PhysicsSettings &inPhysicsSettings); |
| 34 |
~ContactConstraintManager(); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
void Init(uint inMaxBodyPairs, uint inMaxContactConstraints); |
| 40 |
|
| 41 |
|
| 42 |
void SetContactListener(ContactListener *inListener) { mContactListener = inListener; } |
| 43 |
ContactListener * GetContactListener() const { return mContactListener; } |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
using CombineFunction = float (*)(const Body &inBody1, const SubShapeID &inSubShapeID1, const Body &inBody2, const SubShapeID &inSubShapeID2); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
void SetCombineFriction(CombineFunction inCombineFriction) { mCombineFriction = inCombineFriction; } |
| 53 |
CombineFunction GetCombineFriction() const { return mCombineFriction; } |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
void SetCombineRestitution(CombineFunction inCombineRestitution) { mCombineRestitution = inCombineRestitution; } |
| 58 |
CombineFunction GetCombineRestitution() const { return mCombineRestitution; } |
| 59 |
|
| 60 |
|
| 61 |
uint32 GetMaxConstraints() const { return mMaxConstraints; } |
| 62 |
|
| 63 |
|
| 64 |
inline ValidateResult ValidateContactPoint(const Body &inBody1, const Body &inBody2, RVec3Arg inBaseOffset, const CollideShapeResult &inCollisionResult) const |
| 65 |
{ |
| 66 |
if (mContactListener == nullptr) |
| 67 |
return ValidateResult::AcceptAllContactsForThisBodyPair; |
| 68 |
|
| 69 |
return mContactListener->OnContactValidate(inBody1, inBody2, inBaseOffset, inCollisionResult); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
void PrepareConstraintBuffer(PhysicsUpdateContext *inContext); |
| 74 |
|
| 75 |
|
| 76 |
static const int MaxContactPoints = 4; |
| 77 |
|
| 78 |
|
| 79 |
class ContactAllocator : public LFHMAllocatorContext |
| 80 |
{ |
| 81 |
public: |
| 82 |
using LFHMAllocatorContext::LFHMAllocatorContext; |
| 83 |
|
| 84 |
uint mNumBodyPairs = 0; |
| 85 |
uint mNumManifolds = 0; |
| 86 |
EPhysicsUpdateError mErrors = EPhysicsUpdateError::None; |
| 87 |
}; |
| 88 |
|
| 89 |
|
| 90 |
ContactAllocator GetContactAllocator() { return mCache[mCacheWriteIdx].GetContactAllocator(); } |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
void GetContactsFromCache(ContactAllocator &ioContactAllocator, Body &inBody1, Body &inBody2, bool &outPairHandled, bool &outConstraintCreated); |
| 96 |
|
| 97 |
|
| 98 |
using BodyPairHandle = void *; |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
BodyPairHandle AddBodyPair(ContactAllocator &ioContactAllocator, const Body &inBody1, const Body &inBody2); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
bool AddContactConstraint(ContactAllocator &ioContactAllocator, BodyPairHandle inBodyPair, Body &inBody1, Body &inBody2, const ContactManifold &inManifold); |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
void FinalizeContactCacheAndCallContactPointRemovedCallbacks(uint inExpectedNumBodyPairs, uint inExpectedNumManifolds); |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
bool WereBodiesInContact(const BodyID &inBody1ID, const BodyID &inBody2ID) const; |
| 156 |
|
| 157 |
|
| 158 |
uint32 GetNumConstraints() const { return min<uint32>(mNumConstraints, mMaxConstraints); } |
| 159 |
|
| 160 |
|
| 161 |
void SortContacts(uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd) const; |
| 162 |
|
| 163 |
|
| 164 |
inline void GetAffectedBodies(uint32 inConstraintIdx, const Body *&outBody1, const Body *&outBody2) const |
| 165 |
{ |
| 166 |
const ContactConstraint &constraint = mConstraints[inConstraintIdx]; |
| 167 |
outBody1 = constraint.mBody1; |
| 168 |
outBody2 = constraint.mBody2; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
template <class MotionPropertiesCallback> |
| 173 |
void WarmStartVelocityConstraints(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio, MotionPropertiesCallback &ioCallback); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
bool SolveVelocityConstraints(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd); |
| 206 |
|
| 207 |
|
| 208 |
void StoreAppliedImpulses(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd) const; |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
bool SolvePositionConstraints(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd); |
| 227 |
|
| 228 |
|
| 229 |
void RecycleConstraintBuffer(); |
| 230 |
|
| 231 |
|
| 232 |
void FinishConstraintBuffer(); |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
void OnCCDContactAdded(ContactAllocator &ioContactAllocator, const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &outSettings); |
| 241 |
|
| 242 |
#ifdef JPH_DEBUG_RENDERER |
| 243 |
|
| 244 |
static bool sDrawContactPoint; |
| 245 |
static bool sDrawSupportingFaces; |
| 246 |
static bool sDrawContactPointReduction; |
| 247 |
static bool sDrawContactManifolds; |
| 248 |
#endif |
| 249 |
|
| 250 |
|
| 251 |
void SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const; |
| 252 |
|
| 253 |
|
| 254 |
bool RestoreState(StateRecorder &inStream, const StateRecorderFilter *inFilter); |
| 255 |
|
| 256 |
private: |
| 257 |
|
| 258 |
class CachedContactPoint |
| 259 |
{ |
| 260 |
public: |
| 261 |
|
| 262 |
void SaveState(StateRecorder &inStream) const; |
| 263 |
void RestoreState(StateRecorder &inStream); |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
Float3 mPosition1; |
| 268 |
Float3 mPosition2; |
| 269 |
|
| 270 |
|
| 271 |
float mNonPenetrationLambda; |
| 272 |
Vector<2> mFrictionLambda; |
| 273 |
}; |
| 274 |
|
| 275 |
static_assert(sizeof(CachedContactPoint) == 36, "Unexpected size"); |
| 276 |
static_assert(alignof(CachedContactPoint) == 4, "Assuming 4 byte aligned"); |
| 277 |
|
| 278 |
|
| 279 |
class CachedManifold |
| 280 |
{ |
| 281 |
public: |
| 282 |
|
| 283 |
static int sGetRequiredExtraSize(int inNumContactPoints) { return max(0, inNumContactPoints - 1) * sizeof(CachedContactPoint); } |
| 284 |
|
| 285 |
|
| 286 |
static int sGetRequiredTotalSize(int inNumContactPoints) { return sizeof(CachedManifold) + sGetRequiredExtraSize(inNumContactPoints); } |
| 287 |
|
| 288 |
|
| 289 |
void SaveState(StateRecorder &inStream) const; |
| 290 |
void RestoreState(StateRecorder &inStream); |
| 291 |
|
| 292 |
|
| 293 |
uint32 mNextWithSameBodyPair; |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
Float3 mContactNormal; |
| 298 |
|
| 299 |
|
| 300 |
enum class EFlags : uint16 |
| 301 |
{ |
| 302 |
ContactPersisted = 1, |
| 303 |
CCDContact = 2 |
| 304 |
}; |
| 305 |
|
| 306 |
|
| 307 |
mutable atomic<uint16> mFlags { 0 }; |
| 308 |
|
| 309 |
|
| 310 |
uint16 mNumContactPoints; |
| 311 |
|
| 312 |
|
| 313 |
CachedContactPoint mContactPoints[1]; |
| 314 |
}; |
| 315 |
|
| 316 |
static_assert(sizeof(CachedManifold) == 56, "This structure is expect to not contain any waste due to alignment"); |
| 317 |
static_assert(alignof(CachedManifold) == 4, "Assuming 4 byte aligned"); |
| 318 |
|
| 319 |
|
| 320 |
using ManifoldMap = LockFreeHashMap<SubShapeIDPair, CachedManifold>; |
| 321 |
using MKeyValue = ManifoldMap::KeyValue; |
| 322 |
using MKVAndCreated = std::pair<MKeyValue *, bool>; |
| 323 |
|
| 324 |
|
| 325 |
class CachedBodyPair |
| 326 |
{ |
| 327 |
public: |
| 328 |
|
| 329 |
void SaveState(StateRecorder &inStream) const; |
| 330 |
void RestoreState(StateRecorder &inStream); |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
Float3 mDeltaPosition; |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
Float3 mDeltaRotation; |
| 339 |
|
| 340 |
|
| 341 |
uint32 mFirstCachedManifold; |
| 342 |
}; |
| 343 |
|
| 344 |
static_assert(sizeof(CachedBodyPair) == 28, "Unexpected size"); |
| 345 |
static_assert(alignof(CachedBodyPair) == 4, "Assuming 4 byte aligned"); |
| 346 |
|
| 347 |
|
| 348 |
using BodyPairMap = LockFreeHashMap<BodyPair, CachedBodyPair>; |
| 349 |
using BPKeyValue = BodyPairMap::KeyValue; |
| 350 |
|
| 351 |
|
| 352 |
class ManifoldCache |
| 353 |
{ |
| 354 |
public: |
| 355 |
|
| 356 |
void Init(uint inMaxBodyPairs, uint inMaxContactConstraints, uint inCachedManifoldsSize); |
| 357 |
|
| 358 |
|
| 359 |
void Clear(); |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
void Prepare(uint inExpectedNumBodyPairs, uint inExpectedNumManifolds); |
| 364 |
|
| 365 |
|
| 366 |
ContactAllocator GetContactAllocator() { return ContactAllocator(mAllocator, cAllocatorBlockSize); } |
| 367 |
|
| 368 |
|
| 369 |
const MKeyValue * Find(const SubShapeIDPair &inKey, uint64 inKeyHash) const; |
| 370 |
MKeyValue * Create(ContactAllocator &ioContactAllocator, const SubShapeIDPair &inKey, uint64 inKeyHash, int inNumContactPoints); |
| 371 |
MKVAndCreated FindOrCreate(ContactAllocator &ioContactAllocator, const SubShapeIDPair &inKey, uint64 inKeyHash, int inNumContactPoints); |
| 372 |
uint32 ToHandle(const MKeyValue *inKeyValue) const; |
| 373 |
const MKeyValue * FromHandle(uint32 inHandle) const; |
| 374 |
|
| 375 |
|
| 376 |
const BPKeyValue * Find(const BodyPair &inKey, uint64 inKeyHash) const; |
| 377 |
BPKeyValue * Create(ContactAllocator &ioContactAllocator, const BodyPair &inKey, uint64 inKeyHash); |
| 378 |
void GetAllBodyPairsSorted(Array<const BPKeyValue *> &outAll) const; |
| 379 |
void GetAllManifoldsSorted(const CachedBodyPair &inBodyPair, Array<const MKeyValue *> &outAll) const; |
| 380 |
void GetAllCCDManifoldsSorted(Array<const MKeyValue *> &outAll) const; |
| 381 |
void ContactPointRemovedCallbacks(ContactListener *inListener); |
| 382 |
|
| 383 |
#ifdef JPH_ENABLE_ASSERTS |
| 384 |
|
| 385 |
uint GetNumManifolds() const { return mCachedManifolds.GetNumKeyValues(); } |
| 386 |
|
| 387 |
|
| 388 |
uint GetNumBodyPairs() const { return mCachedBodyPairs.GetNumKeyValues(); } |
| 389 |
|
| 390 |
|
| 391 |
void Finalize(); |
| 392 |
#endif |
| 393 |
|
| 394 |
|
| 395 |
void SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const; |
| 396 |
bool RestoreState(const ManifoldCache &inReadCache, StateRecorder &inStream, const StateRecorderFilter *inFilter); |
| 397 |
|
| 398 |
private: |
| 399 |
|
| 400 |
static constexpr uint32 cAllocatorBlockSize = 4096; |
| 401 |
|
| 402 |
|
| 403 |
LFHMAllocator mAllocator; |
| 404 |
|
| 405 |
|
| 406 |
ManifoldMap mCachedManifolds { mAllocator }; |
| 407 |
|
| 408 |
|
| 409 |
BodyPairMap mCachedBodyPairs { mAllocator }; |
| 410 |
|
| 411 |
#ifdef JPH_ENABLE_ASSERTS |
| 412 |
bool mIsFinalized = false; |
| 413 |
#endif |
| 414 |
}; |
| 415 |
|
| 416 |
ManifoldCache mCache[2]; |
| 417 |
int mCacheWriteIdx = 0; |
| 418 |
|
| 419 |
|
| 420 |
class WorldContactPoint |
| 421 |
{ |
| 422 |
public: |
| 423 |
|
| 424 |
void CalculateNonPenetrationConstraintProperties(const Body &inBody1, float inInvMass1, float inInvInertiaScale1, const Body &inBody2, float inInvMass2, float inInvInertiaScale2, RVec3Arg inWorldSpacePosition1, RVec3Arg inWorldSpacePosition2, Vec3Arg inWorldSpaceNormal); |
| 425 |
|
| 426 |
template <EMotionType Type1, EMotionType Type2> |
| 427 |
JPH_INLINE void TemplatedCalculateFrictionAndNonPenetrationConstraintProperties(float inDeltaTime, Vec3Arg inGravity, const Body &inBody1, const Body &inBody2, float inInvM1, float inInvM2, Mat44Arg inInvI1, Mat44Arg inInvI2, RVec3Arg inWorldSpacePosition1, RVec3Arg inWorldSpacePosition2, Vec3Arg inWorldSpaceNormal, Vec3Arg inWorldSpaceTangent1, Vec3Arg inWorldSpaceTangent2, const ContactSettings &inSettings, float inMinVelocityForRestitution); |
| 428 |
|
| 429 |
|
| 430 |
AxisConstraintPart mNonPenetrationConstraint; |
| 431 |
AxisConstraintPart mFrictionConstraint1; |
| 432 |
AxisConstraintPart mFrictionConstraint2; |
| 433 |
|
| 434 |
|
| 435 |
CachedContactPoint * mContactPoint; |
| 436 |
}; |
| 437 |
|
| 438 |
using WorldContactPoints = StaticArray<WorldContactPoint, MaxContactPoints>; |
| 439 |
|
| 440 |
|
| 441 |
class ContactConstraint |
| 442 |
{ |
| 443 |
public: |
| 444 |
#ifdef JPH_DEBUG_RENDERER |
| 445 |
|
| 446 |
void Draw(DebugRenderer *inRenderer, ColorArg inManifoldColor) const; |
| 447 |
#endif |
| 448 |
|
| 449 |
|
| 450 |
JPH_INLINE Vec3 GetWorldSpaceNormal() const |
| 451 |
{ |
| 452 |
return Vec3::sLoadFloat3Unsafe(mWorldSpaceNormal); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
JPH_INLINE void GetTangents(Vec3 &outTangent1, Vec3 &outTangent2) const |
| 457 |
{ |
| 458 |
Vec3 ws_normal = GetWorldSpaceNormal(); |
| 459 |
outTangent1 = ws_normal.GetNormalizedPerpendicular(); |
| 460 |
outTangent2 = ws_normal.Cross(outTangent1); |
| 461 |
} |
| 462 |
|
| 463 |
Body * mBody1; |
| 464 |
Body * mBody2; |
| 465 |
uint64 mSortKey; |
| 466 |
Float3 mWorldSpaceNormal; |
| 467 |
float mCombinedFriction; |
| 468 |
float mInvMass1; |
| 469 |
float mInvInertiaScale1; |
| 470 |
float mInvMass2; |
| 471 |
float mInvInertiaScale2; |
| 472 |
WorldContactPoints mContactPoints; |
| 473 |
}; |
| 474 |
|
| 475 |
public: |
| 476 |
|
| 477 |
static constexpr uint cMaxContactConstraintsLimit = ~uint(0) / sizeof(ContactConstraint); |
| 478 |
|
| 479 |
|
| 480 |
static constexpr uint cMaxBodyPairsLimit = ~uint(0) / sizeof(BodyPairMap::KeyValue); |
| 481 |
|
| 482 |
private: |
| 483 |
|
| 484 |
template <EMotionType Type1, EMotionType Type2> |
| 485 |
JPH_INLINE void TemplatedCalculateFrictionAndNonPenetrationConstraintProperties(ContactConstraint &ioConstraint, const ContactSettings &inSettings, float inDeltaTime, Vec3Arg inGravity, RMat44Arg inTransformBody1, RMat44Arg inTransformBody2, const Body &inBody1, const Body &inBody2); |
| 486 |
|
| 487 |
|
| 488 |
inline void CalculateFrictionAndNonPenetrationConstraintProperties(ContactConstraint &ioConstraint, const ContactSettings &inSettings, float inDeltaTime, Vec3Arg inGravity, RMat44Arg inTransformBody1, RMat44Arg inTransformBody2, const Body &inBody1, const Body &inBody2); |
| 489 |
|
| 490 |
|
| 491 |
template <EMotionType Type1, EMotionType Type2> |
| 492 |
bool TemplatedAddContactConstraint(ContactAllocator &ioContactAllocator, BodyPairHandle inBodyPairHandle, Body &inBody1, Body &inBody2, const ContactManifold &inManifold); |
| 493 |
|
| 494 |
|
| 495 |
template <EMotionType Type1, EMotionType Type2> |
| 496 |
JPH_INLINE static void sWarmStartConstraint(ContactConstraint &ioConstraint, MotionProperties *ioMotionProperties1, MotionProperties *ioMotionProperties2, float inWarmStartImpulseRatio); |
| 497 |
|
| 498 |
|
| 499 |
template <EMotionType Type1, EMotionType Type2> |
| 500 |
JPH_INLINE static bool sSolveVelocityConstraint(ContactConstraint &ioConstraint, MotionProperties *ioMotionProperties1, MotionProperties *ioMotionProperties2); |
| 501 |
|
| 502 |
|
| 503 |
const PhysicsSettings & mPhysicsSettings; |
| 504 |
|
| 505 |
|
| 506 |
ContactListener * mContactListener = nullptr; |
| 507 |
|
| 508 |
|
| 509 |
CombineFunction mCombineFriction = [](const Body &inBody1, const SubShapeID &, const Body &inBody2, const SubShapeID &) { return sqrt(inBody1.GetFriction() * inBody2.GetFriction()); }; |
| 510 |
CombineFunction mCombineRestitution = [](const Body &inBody1, const SubShapeID &, const Body &inBody2, const SubShapeID &) { return max(inBody1.GetRestitution(), inBody2.GetRestitution()); }; |
| 511 |
|
| 512 |
|
| 513 |
ContactConstraint * mConstraints = nullptr; |
| 514 |
uint32 mMaxConstraints = 0; |
| 515 |
atomic<uint32> mNumConstraints { 0 }; |
| 516 |
|
| 517 |
|
| 518 |
PhysicsUpdateContext * mUpdateContext; |
| 519 |
}; |
| 520 |
|
| 521 |
JPH_NAMESPACE_END |