ContactConstraintManager.cpp 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #include <Jolt.h>
  4. #include <Physics/Constraints/ContactConstraintManager.h>
  5. #include <Physics/Body/Body.h>
  6. #include <Physics/PhysicsUpdateContext.h>
  7. #include <Physics/PhysicsSettings.h>
  8. #include <Physics/IslandBuilder.h>
  9. #include <Core/TempAllocator.h>
  10. #ifdef JPH_DEBUG_RENDERER
  11. #include <Renderer/DebugRenderer.h>
  12. #endif // JPH_DEBUG_RENDERER
  13. namespace JPH {
  14. #ifdef JPH_DEBUG_RENDERER
  15. bool ContactConstraintManager::sDrawContactPoint = false;
  16. bool ContactConstraintManager::sDrawSupportingFaces = false;
  17. bool ContactConstraintManager::sDrawContactPointReduction = false;
  18. bool ContactConstraintManager::sDrawContactManifolds = false;
  19. #endif // JPH_DEBUG_RENDERER
  20. //#define JPH_MANIFOLD_CACHE_DEBUG
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  22. // ContactConstraintManager::WorldContactPoint
  23. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  24. void ContactConstraintManager::WorldContactPoint::CalculateNonPenetrationConstraintProperties(float inDeltaTime, const Body &inBody1, const Body &inBody2, Vec3Arg inWorldSpacePosition1, Vec3Arg inWorldSpacePosition2, Vec3Arg inWorldSpaceNormal)
  25. {
  26. // Calculate collision points relative to body
  27. Vec3 p = 0.5f * (inWorldSpacePosition1 + inWorldSpacePosition2);
  28. Vec3 r1 = p - inBody1.GetCenterOfMassPosition();
  29. Vec3 r2 = p - inBody2.GetCenterOfMassPosition();
  30. mNonPenetrationConstraint.CalculateConstraintProperties(inDeltaTime, inBody1, r1, inBody2, r2, inWorldSpaceNormal);
  31. }
  32. void ContactConstraintManager::WorldContactPoint::CalculateFrictionAndNonPenetrationConstraintProperties(float inDeltaTime, const Body &inBody1, const Body &inBody2, Vec3Arg inWorldSpacePosition1, Vec3Arg inWorldSpacePosition2, Vec3Arg inWorldSpaceNormal, Vec3Arg inWorldSpaceTangent1, Vec3Arg inWorldSpaceTangent2, float inCombinedRestitution, float inCombinedFriction, float inMinVelocityForRestitution)
  33. {
  34. // Calculate collision points relative to body
  35. Vec3 p = 0.5f * (inWorldSpacePosition1 + inWorldSpacePosition2);
  36. Vec3 r1 = p - inBody1.GetCenterOfMassPosition();
  37. Vec3 r2 = p - inBody2.GetCenterOfMassPosition();
  38. // Calculate velocity of collision points
  39. Vec3 v1 = inBody1.GetLinearVelocity() + inBody1.GetAngularVelocity().Cross(r1);
  40. Vec3 v2 = inBody2.GetLinearVelocity() + inBody2.GetAngularVelocity().Cross(r2);
  41. Vec3 relative_velocity = v2 - v1;
  42. float normal_velocity = relative_velocity.Dot(inWorldSpaceNormal);
  43. // How much the shapes are penetrating (> 0 if penetrating, < 0 if separated)
  44. float penetration = (inWorldSpacePosition1 - inWorldSpacePosition2).Dot(inWorldSpaceNormal);
  45. // If there is no penetration, this is a speculative contact and we will apply a bias to the contact constraint
  46. // so that the constraint becomes relative_velocity . contact normal > -penetration / delta_time
  47. // instead of relative_velocity . contact normal > 0
  48. // See: GDC 2013: "Physics for Game Programmers; Continuous Collision" - Erin Catto
  49. float speculative_contact_velocity_bias = max(0.0f, -penetration / inDeltaTime);
  50. // Determine if the velocity is big enough for restitution
  51. float normal_velocity_bias;
  52. if (inCombinedRestitution > 0.0f && normal_velocity < -inMinVelocityForRestitution)
  53. {
  54. // We have a velocity that is big enough for restitution. This is where speculative contacts don't work
  55. // great as we have to decide now if we're going to apply the restitution or not. If the relative
  56. // velocity is big enough for a hit, we apply the restitution (in the end, due to other constraints,
  57. // the objects may actually not collide and we will have applied restitution incorrectly). Another
  58. // artifact that occurs because of this approximation is that the object will bounce from its current
  59. // position rather than from a position where it is touching the other object. This causes the object
  60. // to appear to move faster for 1 frame (the opposite of time stealing).
  61. if (normal_velocity < -speculative_contact_velocity_bias)
  62. normal_velocity_bias = inCombinedRestitution * normal_velocity;
  63. else
  64. normal_velocity_bias = 0.0f;
  65. }
  66. else
  67. {
  68. // No restitution. We can safely apply our contact velocity bias.
  69. normal_velocity_bias = speculative_contact_velocity_bias;
  70. }
  71. mNonPenetrationConstraint.CalculateConstraintProperties(inDeltaTime, inBody1, r1, inBody2, r2, inWorldSpaceNormal, normal_velocity_bias);
  72. // Calculate friction part
  73. if (inCombinedFriction > 0.0f)
  74. {
  75. // Implement friction as 2 AxisContraintParts
  76. mFrictionConstraint1.CalculateConstraintProperties(inDeltaTime, inBody1, r1, inBody2, r2, inWorldSpaceTangent1);
  77. mFrictionConstraint2.CalculateConstraintProperties(inDeltaTime, inBody1, r1, inBody2, r2, inWorldSpaceTangent2);
  78. }
  79. else
  80. {
  81. // Turn off friction constraint
  82. mFrictionConstraint1.Deactivate();
  83. mFrictionConstraint2.Deactivate();
  84. }
  85. }
  86. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. // ContactConstraintManager::ContactConstraint
  88. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  89. #ifdef JPH_DEBUG_RENDERER
  90. void ContactConstraintManager::ContactConstraint::Draw(DebugRenderer *inRenderer, ColorArg inManifoldColor) const
  91. {
  92. if (mContactPoints.empty())
  93. return;
  94. // Get body transforms
  95. Mat44 transform_body1 = mBody1->GetCenterOfMassTransform();
  96. Mat44 transform_body2 = mBody2->GetCenterOfMassTransform();
  97. Vec3 prev_point = transform_body1 * Vec3::sLoadFloat3Unsafe(mContactPoints.back().mContactPoint->mPosition1);
  98. for (const WorldContactPoint &wcp : mContactPoints)
  99. {
  100. // Test if any lambda from the previous frame was transferred
  101. float radius = wcp.mNonPenetrationConstraint.GetTotalLambda() == 0.0f
  102. && wcp.mFrictionConstraint1.GetTotalLambda() == 0.0f
  103. && wcp.mFrictionConstraint2.GetTotalLambda() == 0.0f? 0.1f : 0.2f;
  104. Vec3 next_point = transform_body1 * Vec3::sLoadFloat3Unsafe(wcp.mContactPoint->mPosition1);
  105. inRenderer->DrawMarker(next_point, Color::sCyan, radius);
  106. inRenderer->DrawMarker(transform_body2 * Vec3::sLoadFloat3Unsafe(wcp.mContactPoint->mPosition2), Color::sPurple, radius);
  107. // Draw edge
  108. inRenderer->DrawArrow(prev_point, next_point, inManifoldColor, 0.05f);
  109. prev_point = next_point;
  110. }
  111. // Draw normal
  112. Vec3 wp = transform_body1 * Vec3::sLoadFloat3Unsafe(mContactPoints[0].mContactPoint->mPosition1);
  113. inRenderer->DrawArrow(wp, wp + mWorldSpaceNormal, Color::sRed, 0.05f);
  114. // Get tangents
  115. Vec3 t1, t2;
  116. GetTangents(t1, t2);
  117. // Draw tangents
  118. inRenderer->DrawLine(wp, wp + t1, Color::sGreen);
  119. inRenderer->DrawLine(wp, wp + t2, Color::sBlue);
  120. }
  121. #endif // JPH_DEBUG_RENDERER
  122. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  123. // ContactConstraintManager::CachedContactPoint
  124. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  125. void ContactConstraintManager::CachedContactPoint::SaveState(StateRecorder &inStream) const
  126. {
  127. inStream.Write(mPosition1);
  128. inStream.Write(mPosition2);
  129. inStream.Write(mNonPenetrationLambda);
  130. inStream.Write(mFrictionLambda);
  131. }
  132. void ContactConstraintManager::CachedContactPoint::RestoreState(StateRecorder &inStream)
  133. {
  134. inStream.Read(mPosition1);
  135. inStream.Read(mPosition2);
  136. inStream.Read(mNonPenetrationLambda);
  137. inStream.Read(mFrictionLambda);
  138. }
  139. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  140. // ContactConstraintManager::CachedManifold
  141. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  142. void ContactConstraintManager::CachedManifold::SaveState(StateRecorder &inStream) const
  143. {
  144. inStream.Write(mContactNormal);
  145. }
  146. void ContactConstraintManager::CachedManifold::RestoreState(StateRecorder &inStream)
  147. {
  148. inStream.Read(mContactNormal);
  149. }
  150. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  151. // ContactConstraintManager::CachedBodyPair
  152. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  153. void ContactConstraintManager::CachedBodyPair::SaveState(StateRecorder &inStream) const
  154. {
  155. inStream.Write(mDeltaPosition);
  156. inStream.Write(mDeltaRotation);
  157. }
  158. void ContactConstraintManager::CachedBodyPair::RestoreState(StateRecorder &inStream)
  159. {
  160. inStream.Read(mDeltaPosition);
  161. inStream.Read(mDeltaRotation);
  162. }
  163. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  164. // ContactConstraintManager::ManifoldCache
  165. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  166. void ContactConstraintManager::ManifoldCache::Init(uint inMaxBodyPairs, uint inMaxContactConstraints, uint inCachedManifoldsSize)
  167. {
  168. mAllocator.Init(inMaxBodyPairs * sizeof(BodyPairMap::KeyValue) + inCachedManifoldsSize);
  169. mCachedManifolds.Init(GetNextPowerOf2(inMaxContactConstraints));
  170. mCachedBodyPairs.Init(GetNextPowerOf2(inMaxBodyPairs));
  171. }
  172. void ContactConstraintManager::ManifoldCache::Clear()
  173. {
  174. JPH_PROFILE_FUNCTION();
  175. mCachedManifolds.Clear();
  176. mCachedBodyPairs.Clear();
  177. mAllocator.Clear();
  178. #ifdef JPH_ENABLE_ASSERTS
  179. // Mark as incomplete
  180. mIsFinalized = false;
  181. #endif
  182. }
  183. void ContactConstraintManager::ManifoldCache::Prepare(uint inExpectedNumBodyPairs, uint inExpectedNumManifolds)
  184. {
  185. // Minimum amount of buckets to use in the hash map
  186. constexpr uint32 cMinBuckets = 1024;
  187. // Use the next higher power of 2 of amount of objects in the cache from last frame to determine the amount of buckets in this frame
  188. mCachedManifolds.SetNumBuckets(min(max(cMinBuckets, GetNextPowerOf2(inExpectedNumManifolds)), mCachedManifolds.GetMaxBuckets()));
  189. mCachedBodyPairs.SetNumBuckets(min(max(cMinBuckets, GetNextPowerOf2(inExpectedNumBodyPairs)), mCachedBodyPairs.GetMaxBuckets()));
  190. }
  191. const ContactConstraintManager::MKeyValue *ContactConstraintManager::ManifoldCache::Find(const SubShapeIDPair &inKey, size_t inKeyHash) const
  192. {
  193. JPH_ASSERT(mIsFinalized);
  194. return mCachedManifolds.Find(inKey, inKeyHash);
  195. }
  196. ContactConstraintManager::MKeyValue *ContactConstraintManager::ManifoldCache::Create(ContactAllocator &ioContactAllocator, const SubShapeIDPair &inKey, size_t inKeyHash, int inNumContactPoints)
  197. {
  198. JPH_ASSERT(!mIsFinalized);
  199. MKeyValue *kv = mCachedManifolds.Create(ioContactAllocator, inKey, inKeyHash, CachedManifold::sGetRequiredExtraSize(inNumContactPoints));
  200. if (kv == nullptr)
  201. {
  202. JPH_ASSERT(false, "Out of cache space for manifold cache");
  203. return nullptr;
  204. }
  205. kv->GetValue().mNumContactPoints = uint16(inNumContactPoints);
  206. ++ioContactAllocator.mNumManifolds;
  207. return kv;
  208. }
  209. ContactConstraintManager::MKVAndCreated ContactConstraintManager::ManifoldCache::FindOrCreate(ContactAllocator &ioContactAllocator, const SubShapeIDPair &inKey, size_t inKeyHash, int inNumContactPoints)
  210. {
  211. MKeyValue *kv = const_cast<MKeyValue *>(mCachedManifolds.Find(inKey, inKeyHash));
  212. if (kv != nullptr)
  213. return { kv, false };
  214. return { Create(ioContactAllocator, inKey, inKeyHash, inNumContactPoints), true };
  215. }
  216. uint32 ContactConstraintManager::ManifoldCache::ToHandle(const MKeyValue *inKeyValue) const
  217. {
  218. JPH_ASSERT(!mIsFinalized);
  219. return mCachedManifolds.ToHandle(inKeyValue);
  220. }
  221. const ContactConstraintManager::MKeyValue *ContactConstraintManager::ManifoldCache::FromHandle(uint32 inHandle) const
  222. {
  223. JPH_ASSERT(mIsFinalized);
  224. return mCachedManifolds.FromHandle(inHandle);
  225. }
  226. const ContactConstraintManager::BPKeyValue *ContactConstraintManager::ManifoldCache::Find(const BodyPair &inKey, size_t inKeyHash) const
  227. {
  228. JPH_ASSERT(mIsFinalized);
  229. return mCachedBodyPairs.Find(inKey, inKeyHash);
  230. }
  231. ContactConstraintManager::BPKeyValue *ContactConstraintManager::ManifoldCache::Create(ContactAllocator &ioContactAllocator, const BodyPair &inKey, size_t inKeyHash)
  232. {
  233. JPH_ASSERT(!mIsFinalized);
  234. BPKeyValue *kv = mCachedBodyPairs.Create(ioContactAllocator, inKey, inKeyHash, 0);
  235. if (kv == nullptr)
  236. {
  237. JPH_ASSERT(false, "Out of cache space for body pair cache");
  238. return nullptr;
  239. }
  240. ++ioContactAllocator.mNumBodyPairs;
  241. return kv;
  242. }
  243. void ContactConstraintManager::ManifoldCache::GetAllBodyPairsSorted(vector<const BPKeyValue *> &outAll) const
  244. {
  245. JPH_ASSERT(mIsFinalized);
  246. mCachedBodyPairs.GetAllKeyValues(outAll);
  247. // Sort by key
  248. sort(outAll.begin(), outAll.end(), [](const BPKeyValue *inLHS, const BPKeyValue *inRHS) {
  249. return inLHS->GetKey() < inRHS->GetKey();
  250. });
  251. }
  252. void ContactConstraintManager::ManifoldCache::GetAllManifoldsSorted(const CachedBodyPair &inBodyPair, vector<const MKeyValue *> &outAll) const
  253. {
  254. JPH_ASSERT(mIsFinalized);
  255. // Iterate through the attached manifolds
  256. for (uint32 handle = inBodyPair.mFirstCachedManifold; handle != ManifoldMap::cInvalidHandle; handle = FromHandle(handle)->GetValue().mNextWithSameBodyPair)
  257. {
  258. const MKeyValue *kv = mCachedManifolds.FromHandle(handle);
  259. outAll.push_back(kv);
  260. }
  261. // Sort by key
  262. sort(outAll.begin(), outAll.end(), [](const MKeyValue *inLHS, const MKeyValue *inRHS) {
  263. return inLHS->GetKey() < inRHS->GetKey();
  264. });
  265. }
  266. void ContactConstraintManager::ManifoldCache::GetAllCCDManifoldsSorted(vector<const MKeyValue *> &outAll) const
  267. {
  268. mCachedManifolds.GetAllKeyValues(outAll);
  269. for (int i = (int)outAll.size() - 1; i >= 0; --i)
  270. if ((outAll[i]->GetValue().mFlags & (uint16)CachedManifold::EFlags::CCDContact) == 0)
  271. {
  272. outAll[i] = outAll.back();
  273. outAll.pop_back();
  274. }
  275. // Sort by key
  276. sort(outAll.begin(), outAll.end(), [](const MKeyValue *inLHS, const MKeyValue *inRHS) {
  277. return inLHS->GetKey() < inRHS->GetKey();
  278. });
  279. }
  280. void ContactConstraintManager::ManifoldCache::ContactPointRemovedCallbacks(ContactListener *inListener)
  281. {
  282. for (MKeyValue &kv : mCachedManifolds)
  283. if ((kv.GetValue().mFlags & uint16(CachedManifold::EFlags::ContactPersisted)) == 0)
  284. inListener->OnContactRemoved(kv.GetKey());
  285. }
  286. #ifdef JPH_ENABLE_ASSERTS
  287. void ContactConstraintManager::ManifoldCache::Finalize()
  288. {
  289. mIsFinalized = true;
  290. #ifdef JPH_MANIFOLD_CACHE_DEBUG
  291. Trace("ManifoldMap:");
  292. mCachedManifolds.TraceStats();
  293. Trace("BodyPairMap:");
  294. mCachedBodyPairs.TraceStats();
  295. #endif // JPH_MANIFOLD_CACHE_DEBUG
  296. }
  297. #endif
  298. void ContactConstraintManager::ManifoldCache::SaveState(StateRecorder &inStream) const
  299. {
  300. JPH_ASSERT(mIsFinalized);
  301. // Get contents of cache
  302. vector<const BPKeyValue *> all_bp;
  303. GetAllBodyPairsSorted(all_bp);
  304. // Write amount of body pairs
  305. size_t num_body_pairs = all_bp.size();
  306. inStream.Write(num_body_pairs);
  307. // Write all body pairs
  308. for (const BPKeyValue *bp_kv : all_bp)
  309. {
  310. // Write body pair key
  311. inStream.Write(bp_kv->GetKey());
  312. // Write body pair
  313. const CachedBodyPair &bp = bp_kv->GetValue();
  314. bp.SaveState(inStream);
  315. // Get attached manifolds
  316. vector<const MKeyValue *> all_m;
  317. GetAllManifoldsSorted(bp, all_m);
  318. // Write num manifolds
  319. size_t num_manifolds = all_m.size();
  320. inStream.Write(num_manifolds);
  321. // Write all manifolds
  322. for (const MKeyValue *m_kv : all_m)
  323. {
  324. // Write key
  325. inStream.Write(m_kv->GetKey());
  326. const CachedManifold &cm = m_kv->GetValue();
  327. JPH_ASSERT((cm.mFlags & (uint16)CachedManifold::EFlags::CCDContact) == 0);
  328. // Write amount of contacts
  329. inStream.Write(cm.mNumContactPoints);
  330. // Write manifold
  331. cm.SaveState(inStream);
  332. // Write contact points
  333. for (uint32 i = 0; i < cm.mNumContactPoints; ++i)
  334. cm.mContactPoints[i].SaveState(inStream);
  335. }
  336. }
  337. // Get CCD manifolds
  338. vector<const MKeyValue *> all_m;
  339. GetAllCCDManifoldsSorted(all_m);
  340. // Write num CCD manifolds
  341. size_t num_manifolds = all_m.size();
  342. inStream.Write(num_manifolds);
  343. // Write all CCD manifold keys
  344. for (const MKeyValue *m_kv : all_m)
  345. inStream.Write(m_kv->GetKey());
  346. }
  347. bool ContactConstraintManager::ManifoldCache::RestoreState(const ManifoldCache &inReadCache, StateRecorder &inStream)
  348. {
  349. JPH_ASSERT(!mIsFinalized);
  350. bool success = true;
  351. // Create a contact allocator for restoring the contact cache
  352. ContactAllocator contact_allocator(GetContactAllocator());
  353. // When validating, get all existing body pairs
  354. vector<const BPKeyValue *> all_bp;
  355. if (inStream.IsValidating())
  356. inReadCache.GetAllBodyPairsSorted(all_bp);
  357. // Read amount of body pairs
  358. size_t num_body_pairs;
  359. if (inStream.IsValidating())
  360. num_body_pairs = all_bp.size();
  361. inStream.Read(num_body_pairs);
  362. // Read entire cache
  363. for (size_t i = 0; i < num_body_pairs; ++i)
  364. {
  365. // Read key
  366. BodyPair body_pair_key;
  367. if (inStream.IsValidating() && i < all_bp.size())
  368. body_pair_key = all_bp[i]->GetKey();
  369. inStream.Read(body_pair_key);
  370. // Create new entry for this body pair
  371. size_t body_pair_hash = BodyPairHash {} (body_pair_key);
  372. BPKeyValue *bp_kv = Create(contact_allocator, body_pair_key, body_pair_hash);
  373. if (bp_kv == nullptr)
  374. {
  375. // Out of cache space
  376. success = false;
  377. break;
  378. }
  379. CachedBodyPair &bp = bp_kv->GetValue();
  380. // Read body pair
  381. if (inStream.IsValidating() && i < all_bp.size())
  382. memcpy(&bp, &all_bp[i]->GetValue(), sizeof(CachedBodyPair));
  383. bp.RestoreState(inStream);
  384. // When validating, get all existing manifolds
  385. vector<const MKeyValue *> all_m;
  386. if (inStream.IsValidating())
  387. inReadCache.GetAllManifoldsSorted(all_bp[i]->GetValue(), all_m);
  388. // Read amount of manifolds
  389. size_t num_manifolds;
  390. if (inStream.IsValidating())
  391. num_manifolds = all_m.size();
  392. inStream.Read(num_manifolds);
  393. uint32 handle = ManifoldMap::cInvalidHandle;
  394. for (size_t j = 0; j < num_manifolds; ++j)
  395. {
  396. // Read key
  397. SubShapeIDPair sub_shape_key;
  398. if (inStream.IsValidating() && j < all_m.size())
  399. sub_shape_key = all_m[j]->GetKey();
  400. inStream.Read(sub_shape_key);
  401. size_t sub_shape_key_hash = std::hash<SubShapeIDPair> {} (sub_shape_key);
  402. // Read amount of contact points
  403. uint16 num_contact_points;
  404. if (inStream.IsValidating() && j < all_m.size())
  405. num_contact_points = all_m[j]->GetValue().mNumContactPoints;
  406. inStream.Read(num_contact_points);
  407. // Read manifold
  408. MKeyValue *m_kv = Create(contact_allocator, sub_shape_key, sub_shape_key_hash, num_contact_points);
  409. if (m_kv == nullptr)
  410. {
  411. // Out of cache space
  412. success = false;
  413. break;
  414. }
  415. CachedManifold &cm = m_kv->GetValue();
  416. if (inStream.IsValidating() && j < all_m.size())
  417. {
  418. memcpy(&cm, &all_m[j]->GetValue(), CachedManifold::sGetRequiredTotalSize(num_contact_points));
  419. cm.mNumContactPoints = uint16(num_contact_points); // Restore num contact points
  420. }
  421. cm.RestoreState(inStream);
  422. cm.mNextWithSameBodyPair = handle;
  423. handle = ToHandle(m_kv);
  424. // Read contact points
  425. for (uint32 k = 0; k < num_contact_points; ++k)
  426. cm.mContactPoints[k].RestoreState(inStream);
  427. }
  428. bp.mFirstCachedManifold = handle;
  429. }
  430. // When validating, get all existing CCD manifolds
  431. vector<const MKeyValue *> all_m;
  432. if (inStream.IsValidating())
  433. inReadCache.GetAllCCDManifoldsSorted(all_m);
  434. // Read amount of CCD manifolds
  435. size_t num_manifolds;
  436. if (inStream.IsValidating())
  437. num_manifolds = all_m.size();
  438. inStream.Read(num_manifolds);
  439. for (size_t j = 0; j < num_manifolds; ++j)
  440. {
  441. // Read key
  442. SubShapeIDPair sub_shape_key;
  443. if (inStream.IsValidating() && j < all_m.size())
  444. sub_shape_key = all_m[j]->GetKey();
  445. inStream.Read(sub_shape_key);
  446. size_t sub_shape_key_hash = std::hash<SubShapeIDPair> {} (sub_shape_key);
  447. // Create CCD manifold
  448. MKeyValue *m_kv = Create(contact_allocator, sub_shape_key, sub_shape_key_hash, 0);
  449. if (m_kv == nullptr)
  450. {
  451. // Out of cache space
  452. success = false;
  453. break;
  454. }
  455. CachedManifold &cm = m_kv->GetValue();
  456. cm.mFlags |= (uint16)CachedManifold::EFlags::CCDContact;
  457. }
  458. #ifdef JPH_ENABLE_ASSERTS
  459. mIsFinalized = true;
  460. #endif
  461. return success;
  462. }
  463. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  464. // ContactConstraintManager
  465. ////////////////////////////////////////////////////////////////////////////////////////////////////////
  466. ContactConstraintManager::ContactConstraintManager(const PhysicsSettings &inPhysicsSettings) :
  467. mPhysicsSettings(inPhysicsSettings)
  468. {
  469. #ifdef JPH_ENABLE_ASSERTS
  470. // For the first frame mark this empty buffer as finalized
  471. mCache[mCacheWriteIdx ^ 1].Finalize();
  472. #endif
  473. }
  474. ContactConstraintManager::~ContactConstraintManager()
  475. {
  476. JPH_ASSERT(mConstraints == nullptr);
  477. }
  478. void ContactConstraintManager::Init(uint inMaxBodyPairs, uint inMaxContactConstraints)
  479. {
  480. mMaxConstraints = inMaxContactConstraints;
  481. // Calculate worst case cache usage
  482. uint cached_manifolds_size = inMaxContactConstraints * (sizeof(CachedManifold) + (MaxContactPoints - 1) * sizeof(CachedContactPoint));
  483. // Init the caches
  484. mCache[0].Init(inMaxBodyPairs, inMaxContactConstraints, cached_manifolds_size);
  485. mCache[1].Init(inMaxBodyPairs, inMaxContactConstraints, cached_manifolds_size);
  486. }
  487. void ContactConstraintManager::PrepareConstraintBuffer(PhysicsUpdateContext *inContext)
  488. {
  489. // Store context
  490. mUpdateContext = inContext;
  491. // Allocate temporary constraint buffer
  492. JPH_ASSERT(mConstraints == nullptr);
  493. mConstraints = (ContactConstraint *)inContext->mTempAllocator->Allocate(mMaxConstraints * sizeof(ContactConstraint));
  494. }
  495. // Get the orientation of body 2 in local space of body 1
  496. static JPH_INLINE void sGetRelativeOrientation(const Body &inBody1, const Body &inBody2, Vec3 &outDeltaPosition, Quat &outDeltaRotation)
  497. {
  498. Quat inv_r1 = inBody1.GetRotation().Conjugated();
  499. // Get relative rotation
  500. outDeltaRotation = inv_r1 * inBody2.GetRotation();
  501. // Ensure W > 0, we'll be discarding it to save storage space
  502. if (outDeltaRotation.GetW() < 0.0f)
  503. outDeltaRotation = -outDeltaRotation;
  504. // Get relative translation
  505. outDeltaPosition = inv_r1 * (inBody2.GetCenterOfMassPosition() - inBody1.GetCenterOfMassPosition());
  506. }
  507. void ContactConstraintManager::GetContactsFromCache(ContactAllocator &ioContactAllocator, Body &inBody1, Body &inBody2, bool &outPairHandled, bool &outContactFound)
  508. {
  509. JPH_PROFILE_FUNCTION();
  510. // Start with nothing found and not handled
  511. outContactFound = false;
  512. outPairHandled = false;
  513. // Swap bodies so that body 1 id < body 2 id
  514. Body *body1, *body2;
  515. if (inBody1.GetID() < inBody2.GetID())
  516. {
  517. body1 = &inBody1;
  518. body2 = &inBody2;
  519. }
  520. else
  521. {
  522. body1 = &inBody2;
  523. body2 = &inBody1;
  524. }
  525. // Find the cached body pair
  526. BodyPair body_pair_key(body1->GetID(), body2->GetID());
  527. size_t body_pair_hash = BodyPairHash {} (body_pair_key);
  528. const ManifoldCache &read_cache = mCache[mCacheWriteIdx ^ 1];
  529. const BPKeyValue *kv = read_cache.Find(body_pair_key, body_pair_hash);
  530. if (kv == nullptr)
  531. return;
  532. const CachedBodyPair &input_cbp = kv->GetValue();
  533. // Get old position delta
  534. Vec3 old_delta_position = Vec3::sLoadFloat3Unsafe(input_cbp.mDeltaPosition);
  535. // Reconstruct old quaternion delta
  536. Vec3 old_delta_rotation3 = Vec3::sLoadFloat3Unsafe(input_cbp.mDeltaRotation);
  537. Quat old_delta_rotation(Vec4(old_delta_rotation3, sqrt(max(0.0f, 1.0f - old_delta_rotation3.LengthSq()))));
  538. // Determine relative orientation
  539. Vec3 delta_position;
  540. Quat delta_rotation;
  541. sGetRelativeOrientation(*body1, *body2, delta_position, delta_rotation);
  542. // Check if bodies are still roughly in the same relative orientation
  543. if ((delta_position - old_delta_position).LengthSq() > mPhysicsSettings.mBodyPairCacheMaxDeltaPositionSq)
  544. return;
  545. if (delta_rotation.Dot(old_delta_rotation) < mPhysicsSettings.mBodyPairCacheCosMaxDeltaRotation)
  546. return;
  547. // The cache is valid, return that we've handled this body pair
  548. outPairHandled = true;
  549. // Copy the cached body pair to this frame
  550. ManifoldCache &write_cache = mCache[mCacheWriteIdx];
  551. BPKeyValue *output_bp_kv = write_cache.Create(ioContactAllocator, body_pair_key, body_pair_hash);
  552. if (output_bp_kv == nullptr)
  553. return; // Out of cache space
  554. CachedBodyPair *output_cbp = &output_bp_kv->GetValue();
  555. memcpy(output_cbp, &input_cbp, sizeof(CachedBodyPair));
  556. // If there were no contacts, we have handled the contact
  557. if (input_cbp.mFirstCachedManifold == ManifoldMap::cInvalidHandle)
  558. return;
  559. // A contact is available, start creating constraints
  560. outContactFound = true;
  561. // Get body transforms
  562. Mat44 transform_body1 = body1->GetCenterOfMassTransform();
  563. Mat44 transform_body2 = body2->GetCenterOfMassTransform();
  564. // Get time step
  565. float delta_time = mUpdateContext->mSubStepDeltaTime;
  566. // Copy manifolds
  567. uint32 output_handle = ManifoldMap::cInvalidHandle;
  568. uint32 input_handle = input_cbp.mFirstCachedManifold;
  569. do
  570. {
  571. JPH_PROFILE("Add Constraint From Cached Manifold");
  572. // Find the existing manifold
  573. const MKeyValue *input_kv = read_cache.FromHandle(input_handle);
  574. const SubShapeIDPair &input_key = input_kv->GetKey();
  575. const CachedManifold &input_cm = input_kv->GetValue();
  576. JPH_ASSERT(input_cm.mNumContactPoints > 0); // There should be contact points in this manifold!
  577. // Create room for manifold in write buffer and copy data
  578. size_t input_hash = std::hash<SubShapeIDPair> {} (input_key);
  579. MKeyValue *output_kv = write_cache.Create(ioContactAllocator, input_key, input_hash, input_cm.mNumContactPoints);
  580. if (output_kv == nullptr)
  581. break; // Out of cache space
  582. CachedManifold *output_cm = &output_kv->GetValue();
  583. memcpy(output_cm, &input_cm, CachedManifold::sGetRequiredTotalSize(input_cm.mNumContactPoints));
  584. // Link the object under the body pairs
  585. output_cm->mNextWithSameBodyPair = output_handle;
  586. output_handle = write_cache.ToHandle(output_kv);
  587. // Calculate default contact settings
  588. ContactSettings settings;
  589. settings.mCombinedFriction = mCombineFriction(*body1, *body2);
  590. settings.mCombinedRestitution = mCombineRestitution(*body1, *body2);
  591. // Calculate world space contact normal
  592. Vec3 world_space_normal = transform_body2.Multiply3x3(Vec3::sLoadFloat3Unsafe(output_cm->mContactNormal)).Normalized();
  593. // Call contact listener to update settings
  594. if (mContactListener != nullptr)
  595. {
  596. // Convert constraint to manifold structure for callback
  597. ContactManifold manifold;
  598. manifold.mWorldSpaceNormal = world_space_normal;
  599. manifold.mSubShapeID1 = input_key.GetSubShapeID1();
  600. manifold.mSubShapeID2 = input_key.GetSubShapeID2();
  601. manifold.mWorldSpaceContactPointsOn1.resize(output_cm->mNumContactPoints);
  602. manifold.mWorldSpaceContactPointsOn2.resize(output_cm->mNumContactPoints);
  603. float penetration_depth = -FLT_MAX;
  604. for (uint32 i = 0; i < output_cm->mNumContactPoints; ++i)
  605. {
  606. CachedContactPoint &ccp = output_cm->mContactPoints[i];
  607. manifold.mWorldSpaceContactPointsOn1[i] = transform_body1 * Vec3::sLoadFloat3Unsafe(ccp.mPosition1);
  608. manifold.mWorldSpaceContactPointsOn2[i] = transform_body2 * Vec3::sLoadFloat3Unsafe(ccp.mPosition2);
  609. penetration_depth = max(penetration_depth, (manifold.mWorldSpaceContactPointsOn1[0] - manifold.mWorldSpaceContactPointsOn2[0]).Dot(world_space_normal));
  610. }
  611. manifold.mPenetrationDepth = penetration_depth; // We don't have the penetration depth anymore, estimate it
  612. // Notify callback
  613. mContactListener->OnContactPersisted(*body1, *body2, manifold, settings);
  614. }
  615. // If one of the bodies is a sensor, don't actually create the constraint
  616. if (!body1->IsSensor() && !body2->IsSensor())
  617. {
  618. // Add contact constraint in world space for the solver
  619. uint32 constraint_idx = mNumConstraints++;
  620. if (constraint_idx >= mMaxConstraints)
  621. {
  622. JPH_ASSERT(false, "Out of contact constraints!");
  623. break;
  624. }
  625. ContactConstraint &constraint = mConstraints[constraint_idx];
  626. new (&constraint) ContactConstraint();
  627. constraint.mBody1 = body1;
  628. constraint.mBody2 = body2;
  629. constraint.mSortKey = input_hash;
  630. constraint.mWorldSpaceNormal = world_space_normal;
  631. constraint.mSettings = settings;
  632. constraint.mContactPoints.resize(output_cm->mNumContactPoints);
  633. for (uint32 i = 0; i < output_cm->mNumContactPoints; ++i)
  634. {
  635. CachedContactPoint &ccp = output_cm->mContactPoints[i];
  636. WorldContactPoint &wcp = constraint.mContactPoints[i];
  637. wcp.mNonPenetrationConstraint.SetTotalLambda(ccp.mNonPenetrationLambda);
  638. wcp.mFrictionConstraint1.SetTotalLambda(ccp.mFrictionLambda[0]);
  639. wcp.mFrictionConstraint2.SetTotalLambda(ccp.mFrictionLambda[1]);
  640. wcp.mContactPoint = &ccp;
  641. }
  642. // Calculate tangents
  643. Vec3 t1, t2;
  644. constraint.GetTangents(t1, t2);
  645. // Setup velocity constraint with contact settings potentially updated by callback
  646. float min_velocity_for_restitution = mPhysicsSettings.mMinVelocityForRestitution;
  647. for (WorldContactPoint &wcp : constraint.mContactPoints)
  648. {
  649. Vec3 p1 = transform_body1 * Vec3::sLoadFloat3Unsafe(wcp.mContactPoint->mPosition1);
  650. Vec3 p2 = transform_body2 * Vec3::sLoadFloat3Unsafe(wcp.mContactPoint->mPosition2);
  651. wcp.CalculateFrictionAndNonPenetrationConstraintProperties(delta_time, *body1, *body2, p1, p2, constraint.mWorldSpaceNormal, t1, t2, settings.mCombinedRestitution, settings.mCombinedFriction, min_velocity_for_restitution);
  652. }
  653. // Notify island builder
  654. mUpdateContext->mIslandBuilder->LinkContact(constraint_idx, body1->GetIndexInActiveBodiesInternal(), body2->GetIndexInActiveBodiesInternal());
  655. #ifdef JPH_DEBUG_RENDERER
  656. // Draw the manifold
  657. if (sDrawContactManifolds)
  658. constraint.Draw(DebugRenderer::sInstance, Color::sYellow);
  659. #endif // JPH_DEBUG_RENDERER
  660. }
  661. // Mark contact as persisted so that we won't fire OnContactRemoved callbacks
  662. input_cm.mFlags |= (uint16)CachedManifold::EFlags::ContactPersisted;
  663. // Fetch the next manifold
  664. input_handle = input_cm.mNextWithSameBodyPair;
  665. }
  666. while (input_handle != ManifoldMap::cInvalidHandle);
  667. output_cbp->mFirstCachedManifold = output_handle;
  668. }
  669. ContactConstraintManager::BodyPairHandle ContactConstraintManager::AddBodyPair(ContactAllocator &ioContactAllocator, const Body &inBody1, const Body &inBody2)
  670. {
  671. JPH_PROFILE_FUNCTION();
  672. // Swap bodies so that body 1 id < body 2 id
  673. const Body *body1, *body2;
  674. if (inBody1.GetID() < inBody2.GetID())
  675. {
  676. body1 = &inBody1;
  677. body2 = &inBody2;
  678. }
  679. else
  680. {
  681. body1 = &inBody2;
  682. body2 = &inBody1;
  683. }
  684. // Add an entry
  685. BodyPair body_pair_key(body1->GetID(), body2->GetID());
  686. size_t body_pair_hash = BodyPairHash {} (body_pair_key);
  687. BPKeyValue *body_pair_kv = mCache[mCacheWriteIdx].Create(ioContactAllocator, body_pair_key, body_pair_hash);
  688. if (body_pair_kv == nullptr)
  689. return nullptr; // Out of cache space
  690. CachedBodyPair *cbp = &body_pair_kv->GetValue();
  691. cbp->mFirstCachedManifold = ManifoldMap::cInvalidHandle;
  692. // Determine relative orientation
  693. Vec3 delta_position;
  694. Quat delta_rotation;
  695. sGetRelativeOrientation(*body1, *body2, delta_position, delta_rotation);
  696. // Store it
  697. delta_position.StoreFloat3(&cbp->mDeltaPosition);
  698. delta_rotation.GetXYZ().StoreFloat3(&cbp->mDeltaRotation);
  699. return cbp;
  700. }
  701. void ContactConstraintManager::AddContactConstraint(ContactAllocator &ioContactAllocator, BodyPairHandle inBodyPairHandle, Body &inBody1, Body &inBody2, const ContactManifold &inManifold)
  702. {
  703. JPH_PROFILE_FUNCTION();
  704. JPH_ASSERT(inManifold.mWorldSpaceNormal.IsNormalized());
  705. // Swap bodies so that body 1 id < body 2 id
  706. const ContactManifold *manifold;
  707. Body *body1, *body2;
  708. ContactManifold temp;
  709. if (inBody2.GetID() < inBody1.GetID())
  710. {
  711. body1 = &inBody2;
  712. body2 = &inBody1;
  713. temp = inManifold.SwapShapes();
  714. manifold = &temp;
  715. }
  716. else
  717. {
  718. body1 = &inBody1;
  719. body2 = &inBody2;
  720. manifold = &inManifold;
  721. }
  722. // Calculate hash
  723. SubShapeIDPair key { body1->GetID(), manifold->mSubShapeID1, body2->GetID(), manifold->mSubShapeID2 };
  724. size_t key_hash = std::hash<SubShapeIDPair> {} (key);
  725. // Determine number of contact points
  726. int num_contact_points = (int)manifold->mWorldSpaceContactPointsOn1.size();
  727. JPH_ASSERT(num_contact_points <= MaxContactPoints);
  728. JPH_ASSERT(num_contact_points == (int)manifold->mWorldSpaceContactPointsOn2.size());
  729. // Reserve space for new contact cache entry
  730. // Note that for dynamic vs dynamic we always require the first body to have a lower body id to get a consistent key
  731. // under which to look up the contact
  732. ManifoldCache &write_cache = mCache[mCacheWriteIdx];
  733. MKeyValue *new_manifold_kv = write_cache.Create(ioContactAllocator, key, key_hash, num_contact_points);
  734. if (new_manifold_kv == nullptr)
  735. return; // Out of cache space
  736. CachedManifold *new_manifold = &new_manifold_kv->GetValue();
  737. // Transform the world space normal to the space of body 2 (this is usually the static body)
  738. Mat44 inverse_transform_body2 = body2->GetInverseCenterOfMassTransform();
  739. inverse_transform_body2.Multiply3x3(manifold->mWorldSpaceNormal).Normalized().StoreFloat3(&new_manifold->mContactNormal);
  740. // Settings object that gets passed to the callback
  741. ContactSettings settings;
  742. settings.mCombinedFriction = mCombineFriction(*body1, *body2);
  743. settings.mCombinedRestitution = mCombineRestitution(*body1, *body2);
  744. // Get the contact points for the old cache entry
  745. const ManifoldCache &read_cache = mCache[mCacheWriteIdx ^ 1];
  746. const MKeyValue *old_manifold_kv = read_cache.Find(key, key_hash);
  747. const CachedContactPoint *ccp_start;
  748. const CachedContactPoint *ccp_end;
  749. if (old_manifold_kv != nullptr)
  750. {
  751. // Call point persisted listener
  752. if (mContactListener != nullptr)
  753. mContactListener->OnContactPersisted(*body1, *body2, *manifold, settings);
  754. // Fetch the contact points from the old manifold
  755. const CachedManifold *old_manifold = &old_manifold_kv->GetValue();
  756. ccp_start = old_manifold->mContactPoints;
  757. ccp_end = ccp_start + old_manifold->mNumContactPoints;
  758. // Mark contact as persisted so that we won't fire OnContactRemoved callbacks
  759. old_manifold->mFlags |= (uint16)CachedManifold::EFlags::ContactPersisted;
  760. }
  761. else
  762. {
  763. // Call point added listener
  764. if (mContactListener != nullptr)
  765. mContactListener->OnContactAdded(*body1, *body2, *manifold, settings);
  766. // No contact points available from old manifold
  767. ccp_start = nullptr;
  768. ccp_end = nullptr;
  769. }
  770. // Get inverse transform for body 1
  771. Mat44 inverse_transform_body1 = body1->GetInverseCenterOfMassTransform();
  772. // If one of the bodies is a sensor, don't actually create the constraint
  773. if (body1->IsSensor() || body2->IsSensor())
  774. {
  775. // Store the contact manifold in the cache
  776. for (int i = 0; i < num_contact_points; ++i)
  777. {
  778. // Convert to local space to the body
  779. Vec3 p1 = inverse_transform_body1 * manifold->mWorldSpaceContactPointsOn1[i];
  780. Vec3 p2 = inverse_transform_body2 * manifold->mWorldSpaceContactPointsOn2[i];
  781. // Create new contact point
  782. CachedContactPoint &cp = new_manifold->mContactPoints[i];
  783. p1.StoreFloat3(&cp.mPosition1);
  784. p2.StoreFloat3(&cp.mPosition2);
  785. // We don't use this, but reset them anyway for determinism check
  786. cp.mNonPenetrationLambda = 0.0f;
  787. cp.mFrictionLambda[0] = 0.0f;
  788. cp.mFrictionLambda[1] = 0.0f;
  789. }
  790. }
  791. else
  792. {
  793. // Add contact constraint
  794. uint32 constraint_idx = mNumConstraints++;
  795. if (constraint_idx >= mMaxConstraints)
  796. {
  797. JPH_ASSERT(false, "Out of contact constraints!");
  798. // Manifold has been created already, we're not filling it in, so we need to reset the contact number of points.
  799. // Note that we don't hook it up to the body pair cache so that it won't be used as a cache during the next simulation.
  800. new_manifold->mNumContactPoints = 0;
  801. return;
  802. }
  803. ContactConstraint &constraint = mConstraints[constraint_idx];
  804. new (&constraint) ContactConstraint();
  805. constraint.mWorldSpaceNormal = manifold->mWorldSpaceNormal;
  806. constraint.mBody1 = body1;
  807. constraint.mBody2 = body2;
  808. constraint.mSortKey = key_hash;
  809. constraint.mSettings = settings;
  810. // Notify island builder
  811. mUpdateContext->mIslandBuilder->LinkContact(constraint_idx, body1->GetIndexInActiveBodiesInternal(), body2->GetIndexInActiveBodiesInternal());
  812. // Get time step
  813. float delta_time = mUpdateContext->mSubStepDeltaTime;
  814. // Calculate tangents
  815. Vec3 t1, t2;
  816. constraint.GetTangents(t1, t2);
  817. constraint.mContactPoints.resize(num_contact_points);
  818. for (int i = 0; i < num_contact_points; ++i)
  819. {
  820. // Convert to world space and set positions
  821. WorldContactPoint &wcp = constraint.mContactPoints[i];
  822. Vec3 p1_ws = manifold->mWorldSpaceContactPointsOn1[i];
  823. Vec3 p2_ws = manifold->mWorldSpaceContactPointsOn2[i];
  824. // Convert to local space to the body
  825. Vec3 p1_ls = inverse_transform_body1 * p1_ws;
  826. Vec3 p2_ls = inverse_transform_body2 * p2_ws;
  827. // Check if we have a close contact point from last update
  828. bool lambda_set = false;
  829. for (const CachedContactPoint *ccp = ccp_start; ccp < ccp_end; ccp++)
  830. if (Vec3::sLoadFloat3Unsafe(ccp->mPosition1).IsClose(p1_ls, mPhysicsSettings.mContactPointPreserveLambdaMaxDistSq)
  831. && Vec3::sLoadFloat3Unsafe(ccp->mPosition2).IsClose(p2_ls, mPhysicsSettings.mContactPointPreserveLambdaMaxDistSq))
  832. {
  833. // Get lambdas from previous frame
  834. wcp.mNonPenetrationConstraint.SetTotalLambda(ccp->mNonPenetrationLambda);
  835. wcp.mFrictionConstraint1.SetTotalLambda(ccp->mFrictionLambda[0]);
  836. wcp.mFrictionConstraint2.SetTotalLambda(ccp->mFrictionLambda[1]);
  837. lambda_set = true;
  838. break;
  839. }
  840. if (!lambda_set)
  841. {
  842. wcp.mNonPenetrationConstraint.SetTotalLambda(0.0f);
  843. wcp.mFrictionConstraint1.SetTotalLambda(0.0f);
  844. wcp.mFrictionConstraint2.SetTotalLambda(0.0f);
  845. }
  846. // Create new contact point
  847. CachedContactPoint &cp = new_manifold->mContactPoints[i];
  848. p1_ls.StoreFloat3(&cp.mPosition1);
  849. p2_ls.StoreFloat3(&cp.mPosition2);
  850. wcp.mContactPoint = &cp;
  851. // Setup velocity constraint
  852. wcp.CalculateFrictionAndNonPenetrationConstraintProperties(delta_time, *body1, *body2, p1_ws, p2_ws, manifold->mWorldSpaceNormal, t1, t2, settings.mCombinedRestitution, settings.mCombinedFriction, mPhysicsSettings.mMinVelocityForRestitution);
  853. }
  854. #ifdef JPH_DEBUG_RENDERER
  855. // Draw the manifold
  856. if (sDrawContactManifolds)
  857. constraint.Draw(DebugRenderer::sInstance, Color::sOrange);
  858. #endif // JPH_DEBUG_RENDERER
  859. }
  860. // Store cached contact point in body pair cache
  861. CachedBodyPair *cbp = reinterpret_cast<CachedBodyPair *>(inBodyPairHandle);
  862. new_manifold->mNextWithSameBodyPair = cbp->mFirstCachedManifold;
  863. cbp->mFirstCachedManifold = write_cache.ToHandle(new_manifold_kv);
  864. }
  865. void ContactConstraintManager::OnCCDContactAdded(ContactAllocator &ioContactAllocator, const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &outSettings)
  866. {
  867. JPH_ASSERT(inManifold.mWorldSpaceNormal.IsNormalized());
  868. // Calculate contact settings
  869. outSettings.mCombinedFriction = mCombineFriction(inBody1, inBody2);
  870. outSettings.mCombinedRestitution = mCombineRestitution(inBody1, inBody2);
  871. // The remainder of this function only deals with calling contact callbacks, if there's no contact callback we also don't need to do this work
  872. if (mContactListener != nullptr)
  873. {
  874. // Swap bodies so that body 1 id < body 2 id
  875. const ContactManifold *manifold;
  876. const Body *body1, *body2;
  877. ContactManifold temp;
  878. if (inBody2.GetID() < inBody1.GetID())
  879. {
  880. body1 = &inBody2;
  881. body2 = &inBody1;
  882. temp = inManifold.SwapShapes();
  883. manifold = &temp;
  884. }
  885. else
  886. {
  887. body1 = &inBody1;
  888. body2 = &inBody2;
  889. manifold = &inManifold;
  890. }
  891. // Calculate hash
  892. SubShapeIDPair key { body1->GetID(), manifold->mSubShapeID1, body2->GetID(), manifold->mSubShapeID2 };
  893. size_t key_hash = std::hash<SubShapeIDPair> {} (key);
  894. // Check if we already created this contact this physics update
  895. ManifoldCache &write_cache = mCache[mCacheWriteIdx];
  896. MKVAndCreated new_manifold_kv = write_cache.FindOrCreate(ioContactAllocator, key, key_hash, 0);
  897. if (new_manifold_kv.second)
  898. {
  899. // This contact is new for this physics update, check if previous update we already had this contact.
  900. const ManifoldCache &read_cache = mCache[mCacheWriteIdx ^ 1];
  901. const MKeyValue *old_manifold_kv = read_cache.Find(key, key_hash);
  902. if (old_manifold_kv == nullptr)
  903. {
  904. // New contact
  905. mContactListener->OnContactAdded(*body1, *body2, *manifold, outSettings);
  906. }
  907. else
  908. {
  909. // Existing contact
  910. mContactListener->OnContactPersisted(*body1, *body2, *manifold, outSettings);
  911. // Mark contact as persisted so that we won't fire OnContactRemoved callbacks
  912. old_manifold_kv->GetValue().mFlags |= (uint16)CachedManifold::EFlags::ContactPersisted;
  913. }
  914. // Check if the cache is full
  915. if (new_manifold_kv.first != nullptr)
  916. {
  917. // We don't store any contact points in this manifold as it is not for caching impulses, we only need to know that the contact was created
  918. CachedManifold &new_manifold = new_manifold_kv.first->GetValue();
  919. new_manifold.mContactNormal = { 0, 0, 0 };
  920. new_manifold.mFlags |= (uint16)CachedManifold::EFlags::CCDContact;
  921. }
  922. }
  923. else
  924. {
  925. // Already found this contact this physics update.
  926. // Note that we can trigger OnContactPersisted multiple times per physics update, but otherwise we have no way of obtaining the settings
  927. mContactListener->OnContactPersisted(*body1, *body2, *manifold, outSettings);
  928. }
  929. }
  930. }
  931. void ContactConstraintManager::SortContacts(uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd) const
  932. {
  933. JPH_PROFILE_FUNCTION();
  934. sort(inConstraintIdxBegin, inConstraintIdxEnd, [this](uint32 inLHS, uint32 inRHS) {
  935. const ContactConstraint &lhs = mConstraints[inLHS];
  936. const ContactConstraint &rhs = mConstraints[inRHS];
  937. JPH_ASSERT(lhs.mSortKey != rhs.mSortKey, "Hash collision, ordering will be inconsistent");
  938. return lhs.mSortKey < rhs.mSortKey;
  939. });
  940. }
  941. void ContactConstraintManager::FinalizeContactCache(uint inExpectedNumBodyPairs, uint inExpectedNumManifolds)
  942. {
  943. JPH_PROFILE_FUNCTION();
  944. #ifdef JPH_ENABLE_ASSERTS
  945. // Mark cache as finalized
  946. ManifoldCache &old_write_cache = mCache[mCacheWriteIdx];
  947. old_write_cache.Finalize();
  948. // Check that the count of body pairs and manifolds that we tracked outside of the cache (to avoid contention on an atomic) is correct
  949. JPH_ASSERT(old_write_cache.GetNumBodyPairs() == inExpectedNumBodyPairs);
  950. JPH_ASSERT(old_write_cache.GetNumManifolds() == inExpectedNumManifolds);
  951. #endif
  952. // Buffers are now complete, make write buffer the read buffer
  953. mCacheWriteIdx ^= 1;
  954. // Use the amount of contacts from the last iteration to determine the amount of buckets to use in the hash map for the next iteration
  955. mCache[mCacheWriteIdx].Prepare(inExpectedNumBodyPairs, inExpectedNumManifolds);
  956. }
  957. void ContactConstraintManager::ContactPointRemovedCallbacks()
  958. {
  959. JPH_PROFILE_FUNCTION();
  960. // Get the read cache
  961. ManifoldCache &read_cache = mCache[mCacheWriteIdx ^ 1];
  962. // Call the actual callbacks
  963. if (mContactListener != nullptr)
  964. read_cache.ContactPointRemovedCallbacks(mContactListener);
  965. // We're done with the cache now
  966. read_cache.Clear();
  967. }
  968. void ContactConstraintManager::SetupVelocityConstraints(uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd, float inDeltaTime)
  969. {
  970. JPH_PROFILE_FUNCTION();
  971. float min_velocity_for_restitution = mPhysicsSettings.mMinVelocityForRestitution;
  972. for (const uint32 *constraint_idx = inConstraintIdxBegin; constraint_idx < inConstraintIdxEnd; ++constraint_idx)
  973. {
  974. ContactConstraint &constraint = mConstraints[*constraint_idx];
  975. // Fetch bodies
  976. Body &body1 = *constraint.mBody1;
  977. Body &body2 = *constraint.mBody2;
  978. // Get body transforms
  979. Mat44 transform_body1 = body1.GetCenterOfMassTransform();
  980. Mat44 transform_body2 = body2.GetCenterOfMassTransform();
  981. // Calculate tangents
  982. Vec3 t1, t2;
  983. constraint.GetTangents(t1, t2);
  984. // Setup velocity constraint
  985. for (WorldContactPoint &wcp : constraint.mContactPoints)
  986. {
  987. Vec3 p1 = transform_body1 * Vec3::sLoadFloat3Unsafe(wcp.mContactPoint->mPosition1);
  988. Vec3 p2 = transform_body2 * Vec3::sLoadFloat3Unsafe(wcp.mContactPoint->mPosition2);
  989. wcp.CalculateFrictionAndNonPenetrationConstraintProperties(inDeltaTime, body1, body2, p1, p2, constraint.mWorldSpaceNormal, t1, t2, constraint.mSettings.mCombinedRestitution, constraint.mSettings.mCombinedFriction, min_velocity_for_restitution);
  990. }
  991. }
  992. }
  993. void ContactConstraintManager::WarmStartVelocityConstraints(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio)
  994. {
  995. JPH_PROFILE_FUNCTION();
  996. for (const uint32 *constraint_idx = inConstraintIdxBegin; constraint_idx < inConstraintIdxEnd; ++constraint_idx)
  997. {
  998. ContactConstraint &constraint = mConstraints[*constraint_idx];
  999. // Fetch bodies
  1000. Body &body1 = *constraint.mBody1;
  1001. Body &body2 = *constraint.mBody2;
  1002. // Calculate tangents
  1003. Vec3 t1, t2;
  1004. constraint.GetTangents(t1, t2);
  1005. for (WorldContactPoint &wcp : constraint.mContactPoints)
  1006. {
  1007. // Warm starting: Apply impulse from last frame
  1008. if (wcp.mFrictionConstraint1.IsActive())
  1009. {
  1010. JPH_ASSERT(wcp.mFrictionConstraint2.IsActive());
  1011. wcp.mFrictionConstraint1.WarmStart(body1, body2, t1, inWarmStartImpulseRatio);
  1012. wcp.mFrictionConstraint2.WarmStart(body1, body2, t2, inWarmStartImpulseRatio);
  1013. }
  1014. wcp.mNonPenetrationConstraint.WarmStart(body1, body2, constraint.mWorldSpaceNormal, inWarmStartImpulseRatio);
  1015. }
  1016. }
  1017. }
  1018. bool ContactConstraintManager::SolveVelocityConstraints(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd)
  1019. {
  1020. JPH_PROFILE_FUNCTION();
  1021. bool any_impulse_applied = false;
  1022. for (const uint32 *constraint_idx = inConstraintIdxBegin; constraint_idx < inConstraintIdxEnd; ++constraint_idx)
  1023. {
  1024. ContactConstraint &constraint = mConstraints[*constraint_idx];
  1025. // Fetch bodies
  1026. Body &body1 = *constraint.mBody1;
  1027. Body &body2 = *constraint.mBody2;
  1028. // Calculate tangents
  1029. Vec3 t1, t2;
  1030. constraint.GetTangents(t1, t2);
  1031. // First apply all friction constraints (non-penetration is more important than friction)
  1032. for (WorldContactPoint &wcp : constraint.mContactPoints)
  1033. {
  1034. // Check if friction is enabled
  1035. if (wcp.mFrictionConstraint1.IsActive())
  1036. {
  1037. JPH_ASSERT(wcp.mFrictionConstraint2.IsActive());
  1038. // Calculate max impulse that can be applied. Note that we're using the non-penetration impulse from the previous iteration here.
  1039. // We do this because non-penetration is more important so is solved last (the last things that are solved in an iterative solver
  1040. // contribute the most).
  1041. float max_lambda_f = constraint.mSettings.mCombinedFriction * wcp.mNonPenetrationConstraint.GetTotalLambda();
  1042. // Solve friction velocities
  1043. // Note that what we're doing is not fully correct since the max force we can apply is 2 * max_lambda_f instead of max_lambda_f since we're solving axis independently
  1044. if (wcp.mFrictionConstraint1.SolveVelocityConstraint(body1, body2, t1, -max_lambda_f, max_lambda_f))
  1045. any_impulse_applied = true;
  1046. if (wcp.mFrictionConstraint2.SolveVelocityConstraint(body1, body2, t2, -max_lambda_f, max_lambda_f))
  1047. any_impulse_applied = true;
  1048. }
  1049. }
  1050. // Then apply all non-penetration constraints
  1051. for (WorldContactPoint &wcp : constraint.mContactPoints)
  1052. {
  1053. // Solve non penetration velocities
  1054. if (wcp.mNonPenetrationConstraint.SolveVelocityConstraint(body1, body2, constraint.mWorldSpaceNormal, 0.0f, FLT_MAX))
  1055. any_impulse_applied = true;
  1056. }
  1057. }
  1058. return any_impulse_applied;
  1059. }
  1060. void ContactConstraintManager::StoreAppliedImpulses(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd)
  1061. {
  1062. // Copy back total applied impulse to cache for the next frame
  1063. for (const uint32 *constraint_idx = inConstraintIdxBegin; constraint_idx < inConstraintIdxEnd; ++constraint_idx)
  1064. {
  1065. const ContactConstraint &constraint = mConstraints[*constraint_idx];
  1066. for (const WorldContactPoint &wcp : constraint.mContactPoints)
  1067. {
  1068. wcp.mContactPoint->mNonPenetrationLambda = wcp.mNonPenetrationConstraint.GetTotalLambda();
  1069. wcp.mContactPoint->mFrictionLambda[0] = wcp.mFrictionConstraint1.GetTotalLambda();
  1070. wcp.mContactPoint->mFrictionLambda[1] = wcp.mFrictionConstraint2.GetTotalLambda();
  1071. }
  1072. }
  1073. }
  1074. bool ContactConstraintManager::SolvePositionConstraints(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd)
  1075. {
  1076. JPH_PROFILE_FUNCTION();
  1077. bool any_impulse_applied = false;
  1078. float delta_time = mUpdateContext->mSubStepDeltaTime;
  1079. for (const uint32 *constraint_idx = inConstraintIdxBegin; constraint_idx < inConstraintIdxEnd; ++constraint_idx)
  1080. {
  1081. ContactConstraint &constraint = mConstraints[*constraint_idx];
  1082. // Fetch bodies
  1083. Body &body1 = *constraint.mBody1;
  1084. Body &body2 = *constraint.mBody2;
  1085. // Get transforms
  1086. Mat44 transform1 = body1.GetCenterOfMassTransform();
  1087. Mat44 transform2 = body2.GetCenterOfMassTransform();
  1088. for (WorldContactPoint &wcp : constraint.mContactPoints)
  1089. {
  1090. // Calculate new contact point positions in world space (the bodies may have moved)
  1091. Vec3 p1 = transform1 * Vec3::sLoadFloat3Unsafe(wcp.mContactPoint->mPosition1);
  1092. Vec3 p2 = transform2 * Vec3::sLoadFloat3Unsafe(wcp.mContactPoint->mPosition2);
  1093. // Calculate separation along the normal (negative if interpenetrating)
  1094. // Allow a little penetration by default (PhysicsSettings::mPenetrationSlop) to avoid jittering between contact/no-contact which wipes out the contact cache and warm start impulses
  1095. // Clamp penetration to a max PhysicsSettings::mMaxPenetrationDistance so that we don't apply a huge impulse if we're penetrating a lot
  1096. float separation = max((p2 - p1).Dot(constraint.mWorldSpaceNormal) + mPhysicsSettings.mPenetrationSlop, -mPhysicsSettings.mMaxPenetrationDistance);
  1097. // Only enforce constraint when separation < 0 (otherwise we're apart)
  1098. if (separation < 0.0f)
  1099. {
  1100. // Update constraint properties (bodies may have moved)
  1101. wcp.CalculateNonPenetrationConstraintProperties(delta_time, body1, body2, p1, p2, constraint.mWorldSpaceNormal);
  1102. // Solve position errors
  1103. if (wcp.mNonPenetrationConstraint.SolvePositionConstraint(body1, body2, constraint.mWorldSpaceNormal, separation, mPhysicsSettings.mBaumgarte))
  1104. any_impulse_applied = true;
  1105. }
  1106. }
  1107. }
  1108. return any_impulse_applied;
  1109. }
  1110. void ContactConstraintManager::RecycleConstraintBuffer()
  1111. {
  1112. // Reset constraint array
  1113. mNumConstraints = 0;
  1114. }
  1115. void ContactConstraintManager::FinishConstraintBuffer()
  1116. {
  1117. // Free constraints buffer
  1118. mUpdateContext->mTempAllocator->Free(mConstraints, mMaxConstraints * sizeof(ContactConstraint));
  1119. mConstraints = nullptr;
  1120. mNumConstraints = 0;
  1121. // Reset update context
  1122. mUpdateContext = nullptr;
  1123. }
  1124. void ContactConstraintManager::SaveState(StateRecorder &inStream) const
  1125. {
  1126. mCache[mCacheWriteIdx ^ 1].SaveState(inStream);
  1127. }
  1128. bool ContactConstraintManager::RestoreState(StateRecorder &inStream)
  1129. {
  1130. bool success = mCache[mCacheWriteIdx].RestoreState(mCache[mCacheWriteIdx ^ 1], inStream);
  1131. mCacheWriteIdx ^= 1;
  1132. mCache[mCacheWriteIdx].Clear();
  1133. return success;
  1134. }
  1135. } // JPH