ContactConstraintManager.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/StaticArray.h>
  5. #include <Jolt/Core/LockFreeHashMap.h>
  6. #include <Jolt/Physics/Body/BodyPair.h>
  7. #include <Jolt/Physics/Collision/Shape/SubShapeIDPair.h>
  8. #include <Jolt/Physics/Collision/ManifoldBetweenTwoFaces.h>
  9. #include <Jolt/Physics/Constraints/ConstraintPart/AxisConstraintPart.h>
  10. #include <Jolt/Physics/Constraints/ConstraintPart/DualAxisConstraintPart.h>
  11. #include <Jolt/Core/HashCombine.h>
  12. #include <Jolt/Core/NonCopyable.h>
  13. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  14. #include <atomic>
  15. JPH_SUPPRESS_WARNINGS_STD_END
  16. JPH_NAMESPACE_BEGIN
  17. struct PhysicsSettings;
  18. class PhysicsUpdateContext;
  19. class ContactConstraintManager : public NonCopyable
  20. {
  21. public:
  22. JPH_OVERRIDE_NEW_DELETE
  23. /// Constructor
  24. explicit ContactConstraintManager(const PhysicsSettings &inPhysicsSettings);
  25. ~ContactConstraintManager();
  26. /// Initialize the system.
  27. /// @param inMaxBodyPairs Maximum amount of body pairs to process (anything else will fall through the world), this number should generally be much higher than the max amount of contact points as there will be lots of bodies close that are not actually touching
  28. /// @param inMaxContactConstraints Maximum amount of contact constraints to process (anything else will fall through the world)
  29. void Init(uint inMaxBodyPairs, uint inMaxContactConstraints);
  30. /// Listener that is notified whenever a contact point between two bodies is added/updated/removed
  31. void SetContactListener(ContactListener *inListener) { mContactListener = inListener; }
  32. ContactListener * GetContactListener() const { return mContactListener; }
  33. /// Callback function to combine the restitution or friction of two bodies
  34. /// Note that when merging manifolds (when PhysicsSettings::mUseManifoldReduction is true) you will only get a callback for the merged manifold.
  35. /// It is not possible in that case to get all sub shape ID pairs that were colliding, you'll get the first encountered pair.
  36. using CombineFunction = float (*)(const Body &inBody1, const SubShapeID &inSubShapeID1, const Body &inBody2, const SubShapeID &inSubShapeID2);
  37. /// Set the function that combines the friction of two bodies and returns it
  38. /// Default method is the geometric mean: sqrt(friction1 * friction2).
  39. void SetCombineFriction(CombineFunction inCombineFriction) { mCombineFriction = inCombineFriction; }
  40. /// Set the function that combines the restitution of two bodies and returns it
  41. /// Default method is max(restitution1, restitution1)
  42. void SetCombineRestitution(CombineFunction inCombineRestitution) { mCombineRestitution = inCombineRestitution; }
  43. /// Get the max number of contact constraints that are allowed
  44. uint32 GetMaxConstraints() const { return mMaxConstraints; }
  45. /// Check with the listener if inBody1 and inBody2 could collide, returns false if not
  46. inline ValidateResult ValidateContactPoint(const Body &inBody1, const Body &inBody2, const CollideShapeResult &inCollisionResult) const
  47. {
  48. if (mContactListener == nullptr)
  49. return ValidateResult::AcceptAllContactsForThisBodyPair;
  50. return mContactListener->OnContactValidate(inBody1, inBody2, inCollisionResult);
  51. }
  52. /// Sets up the constraint buffer. Should be called before starting collision detection.
  53. void PrepareConstraintBuffer(PhysicsUpdateContext *inContext);
  54. /// Max 4 contact points are needed for a stable manifold
  55. static const int MaxContactPoints = 4;
  56. /// Contacts are allocated in a lock free hash map
  57. class ContactAllocator : public LFHMAllocatorContext
  58. {
  59. public:
  60. using LFHMAllocatorContext::LFHMAllocatorContext;
  61. uint mNumBodyPairs = 0; ///< Total number of body pairs added using this allocator
  62. uint mNumManifolds = 0; ///< Total number of manifolds added using this allocator
  63. };
  64. /// Get a new allocator context for storing contacts. Note that you should call this once and then add multiple contacts using the context.
  65. ContactAllocator GetContactAllocator() { return mCache[mCacheWriteIdx].GetContactAllocator(); }
  66. /// Check if the contact points from the previous frame are reusable and if so copy them.
  67. /// When the cache was usable and the pair has been handled: outPairHandled = true.
  68. /// When a contact constraint was produced: outConstraintCreated = true.
  69. void GetContactsFromCache(ContactAllocator &ioContactAllocator, Body &inBody1, Body &inBody2, bool &outPairHandled, bool &outConstraintCreated);
  70. /// Handle used to keep track of the current body pair
  71. using BodyPairHandle = void *;
  72. /// Create a handle for a colliding body pair so that contact constraints can be added between them.
  73. /// Needs to be called once per body pair per frame before calling AddContactConstraint.
  74. BodyPairHandle AddBodyPair(ContactAllocator &ioContactAllocator, const Body &inBody1, const Body &inBody2);
  75. /// Add a contact constraint for this frame.
  76. ///
  77. /// @param ioContactAllocator The allocator that reserves memory for the contacts
  78. /// @param inBodyPair The handle for the contact cache for this body pair
  79. /// @param inBody1 The first body that is colliding
  80. /// @param inBody2 The second body that is colliding
  81. /// @param inManifold The manifold that describes the collision
  82. /// @return true if a contact constraint was created (can be false in the case of a sensor)
  83. ///
  84. /// This is using the approach described in 'Modeling and Solving Constraints' by Erin Catto presented at GDC 2009 (and later years with slight modifications).
  85. /// We're using the formulas from slide 50 - 53 combined.
  86. ///
  87. /// Euler velocity integration:
  88. ///
  89. /// v1' = v1 + M^-1 P
  90. ///
  91. /// Impulse:
  92. ///
  93. /// P = J^T lambda
  94. ///
  95. /// Constraint force:
  96. ///
  97. /// lambda = -K^-1 J v1
  98. ///
  99. /// Inverse effective mass:
  100. ///
  101. /// K = J M^-1 J^T
  102. ///
  103. /// Constraint equation (limits movement in 1 axis):
  104. ///
  105. /// C = (p2 - p1) . n
  106. ///
  107. /// Jacobian (for position constraint)
  108. ///
  109. /// J = [-n, -r1 x n, n, r2 x n]
  110. ///
  111. /// n = contact normal (pointing away from body 1).
  112. /// p1, p2 = positions of collision on body 1 and 2.
  113. /// r1, r2 = contact point relative to center of mass of body 1 and body 2 (r1 = p1 - x1, r2 = p2 - x2).
  114. /// v1, v2 = (linear velocity, angular velocity): 6 vectors containing linear and angular velocity for body 1 and 2.
  115. /// M = mass matrix, a diagonal matrix of the mass and inertia with diagonal [m1, I1, m2, I2].
  116. bool AddContactConstraint(ContactAllocator &ioContactAllocator, BodyPairHandle inBodyPair, Body &inBody1, Body &inBody2, const ContactManifold &inManifold);
  117. /// Finalizes the contact cache, the contact cache that was generated during the calls to AddContactConstraint in this update
  118. /// will be used from now on to read from.
  119. /// inExpectedNumBodyPairs / inExpectedNumManifolds are the amount of body pairs / manifolds found in the previous step and is used to determine the amount of buckets the contact cache hash map will use.
  120. void FinalizeContactCache(uint inExpectedNumBodyPairs, uint inExpectedNumManifolds);
  121. /// Notifies the listener of any contact points that were removed. Needs to be callsed after FinalizeContactCache().
  122. void ContactPointRemovedCallbacks();
  123. /// Get the number of contact constraints that were found
  124. uint32 GetNumConstraints() const { return min<uint32>(mNumConstraints, mMaxConstraints); }
  125. /// Sort contact constraints deterministically
  126. void SortContacts(uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd) const;
  127. /// AddContactConstraint will also setup the velocity constraints for the first sub step. For subsequent sub steps this function must be called prior to warm starting the constraint.
  128. void SetupVelocityConstraints(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime);
  129. /// Apply last frame's impulses as an initial guess for this frame's impulses
  130. void WarmStartVelocityConstraints(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio);
  131. /// Solve velocity constraints, when almost nothing changes this should only apply very small impulses
  132. /// since we're warm starting with the total impulse applied in the last frame above.
  133. ///
  134. /// Friction wise we're using the Coulomb friction model which says that:
  135. ///
  136. /// |F_T| <= mu |F_N|
  137. ///
  138. /// Where F_T is the tangential force, F_N is the normal force and mu is the friction coefficient
  139. ///
  140. /// In impulse terms this becomes:
  141. ///
  142. /// |lambda_T| <= mu |lambda_N|
  143. ///
  144. /// And the constraint that needs to be applied is exactly the same as a non penetration constraint
  145. /// except that we use a tangent instead of a normal. The tangent should point in the direction of the
  146. /// tangential velocity of the point:
  147. ///
  148. /// J = [-T, -r1 x T, T, r2 x T]
  149. ///
  150. /// Where T is the tangent.
  151. ///
  152. /// See slide 42 and 43.
  153. ///
  154. /// Restitution is implemented as a velocity bias (see slide 41):
  155. ///
  156. /// b = e v_n^-
  157. ///
  158. /// e = the restitution coefficient, v_n^- is the normal velocity prior to the collision
  159. ///
  160. /// Restitution is only applied when v_n^- is large enough and the points are moving towards collision
  161. bool SolveVelocityConstraints(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd);
  162. /// Save back the lambdas to the contact cache for the next warm start
  163. void StoreAppliedImpulses(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd) const;
  164. /// Solve position constraints.
  165. /// This is using the approach described in 'Modeling and Solving Constraints' by Erin Catto presented at GDC 2007.
  166. /// On slide 78 it is suggested to split up the Baumgarte stabilization for positional drift so that it does not
  167. /// actually add to the momentum. We combine an Euler velocity integrate + a position integrate and then discard the velocity
  168. /// change.
  169. ///
  170. /// Constraint force:
  171. ///
  172. /// lambda = -K^-1 b
  173. ///
  174. /// Baumgarte stabilization:
  175. ///
  176. /// b = beta / dt C
  177. ///
  178. /// beta = baumgarte stabilization factor.
  179. /// dt = delta time.
  180. bool SolvePositionConstraints(const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd);
  181. /// Recycle the constraint buffer. Should be called between collision simulation steps.
  182. void RecycleConstraintBuffer();
  183. /// Terminate the constraint buffer. Should be called after simulation ends.
  184. void FinishConstraintBuffer();
  185. /// Called by continuous collision detection to notify the contact listener that a contact was added
  186. /// @param ioContactAllocator The allocator that reserves memory for the contacts
  187. /// @param inBody1 The first body that is colliding
  188. /// @param inBody2 The second body that is colliding
  189. /// @param inManifold The manifold that describes the collision
  190. /// @param outSettings The calculated contact settings (may be overridden by the contact listener)
  191. void OnCCDContactAdded(ContactAllocator &ioContactAllocator, const Body &inBody1, const Body &inBody2, const ContactManifold &inManifold, ContactSettings &outSettings);
  192. #ifdef JPH_DEBUG_RENDERER
  193. // Drawing properties
  194. static bool sDrawContactPoint;
  195. static bool sDrawSupportingFaces;
  196. static bool sDrawContactPointReduction;
  197. static bool sDrawContactManifolds;
  198. #endif // JPH_DEBUG_RENDERER
  199. /// Saving state for replay
  200. void SaveState(StateRecorder &inStream) const;
  201. /// Restoring state for replay. Returns false when failed.
  202. bool RestoreState(StateRecorder &inStream);
  203. private:
  204. /// Local space contact point, used for caching impulses
  205. class CachedContactPoint
  206. {
  207. public:
  208. /// Saving / restoring state for replay
  209. void SaveState(StateRecorder &inStream) const;
  210. void RestoreState(StateRecorder &inStream);
  211. /// Local space positions on body 1 and 2.
  212. /// Note: these values are read through sLoadFloat3Unsafe.
  213. Float3 mPosition1;
  214. Float3 mPosition2;
  215. /// Total applied impulse during the last update that it was used
  216. float mNonPenetrationLambda;
  217. Vector<2> mFrictionLambda;
  218. };
  219. static_assert(sizeof(CachedContactPoint) == 36, "Unexpected size");
  220. static_assert(alignof(CachedContactPoint) == 4, "Assuming 4 byte aligned");
  221. /// A single cached manifold
  222. class CachedManifold
  223. {
  224. public:
  225. /// Calculate size in bytes needed beyond the size of the class to store inNumContactPoints
  226. static int sGetRequiredExtraSize(int inNumContactPoints) { return max(0, inNumContactPoints - 1) * sizeof(CachedContactPoint); }
  227. /// Calculate total class size needed for storing inNumContactPoints
  228. static int sGetRequiredTotalSize(int inNumContactPoints) { return sizeof(CachedManifold) + sGetRequiredExtraSize(inNumContactPoints); }
  229. /// Saving / restoring state for replay
  230. void SaveState(StateRecorder &inStream) const;
  231. void RestoreState(StateRecorder &inStream);
  232. /// Handle to next cached contact points in ManifoldCache::mCachedManifolds for the same body pair
  233. uint32 mNextWithSameBodyPair;
  234. /// Contact normal in the space of 2.
  235. /// Note: this value is read through sLoadFloat3Unsafe.
  236. Float3 mContactNormal;
  237. /// Flags for this cached manifold
  238. enum class EFlags : uint16
  239. {
  240. ContactPersisted = 1, ///< If this cache entry was reused in the next simulation update
  241. CCDContact = 2 ///< This is a cached manifold reported by continuous collision detection and was only used to create a contact callback
  242. };
  243. /// @see EFlags
  244. mutable atomic<uint16> mFlags { 0 };
  245. /// Number of contact points in the array below
  246. uint16 mNumContactPoints;
  247. /// Contact points that this manifold consists of
  248. CachedContactPoint mContactPoints[1];
  249. };
  250. static_assert(sizeof(CachedManifold) == 56, "This structure is expect to not contain any waste due to alignment");
  251. static_assert(alignof(CachedManifold) == 4, "Assuming 4 byte aligned");
  252. /// Define a map that maps SubShapeIDPair -> manifold
  253. using ManifoldMap = LockFreeHashMap<SubShapeIDPair, CachedManifold>;
  254. using MKeyValue = ManifoldMap::KeyValue;
  255. using MKVAndCreated = pair<MKeyValue *, bool>;
  256. /// Start of list of contact points for a particular pair of bodies
  257. class CachedBodyPair
  258. {
  259. public:
  260. /// Saving / restoring state for replay
  261. void SaveState(StateRecorder &inStream) const;
  262. void RestoreState(StateRecorder &inStream);
  263. /// Local space position difference from Body A to Body B.
  264. /// Note: this value is read through sLoadFloat3Unsafe
  265. Float3 mDeltaPosition;
  266. /// Local space rotation difference from Body A to Body B, fourth component of quaternion is not stored but is guaranteed >= 0.
  267. /// Note: this value is read through sLoadFloat3Unsafe
  268. Float3 mDeltaRotation;
  269. /// Handle to first manifold in ManifoldCache::mCachedManifolds
  270. uint32 mFirstCachedManifold;
  271. };
  272. static_assert(sizeof(CachedBodyPair) == 28, "Unexpected size");
  273. static_assert(alignof(CachedBodyPair) == 4, "Assuming 4 byte aligned");
  274. /// Define a map that maps BodyPair -> CachedBodyPair
  275. using BodyPairMap = LockFreeHashMap<BodyPair, CachedBodyPair>;
  276. using BPKeyValue = BodyPairMap::KeyValue;
  277. /// Holds all caches that are needed to quickly find cached body pairs / manifolds
  278. class ManifoldCache
  279. {
  280. public:
  281. /// Initialize the cache
  282. void Init(uint inMaxBodyPairs, uint inMaxContactConstraints, uint inCachedManifoldsSize);
  283. /// Reset all entries from the cache
  284. void Clear();
  285. /// Prepare cache before creating new contacts.
  286. /// inExpectedNumBodyPairs / inExpectedNumManifolds are the amount of body pairs / manifolds found in the previous step and is used to determine the amount of buckets the contact cache hash map will use.
  287. void Prepare(uint inExpectedNumBodyPairs, uint inExpectedNumManifolds);
  288. /// Get a new allocator context for storing contacts. Note that you should call this once and then add multiple contacts using the context.
  289. ContactAllocator GetContactAllocator() { return ContactAllocator(mAllocator, cAllocatorBlockSize); }
  290. /// Find / create cached entry for SubShapeIDPair -> CachedManifold
  291. const MKeyValue * Find(const SubShapeIDPair &inKey, uint64 inKeyHash) const;
  292. MKeyValue * Create(ContactAllocator &ioContactAllocator, const SubShapeIDPair &inKey, uint64 inKeyHash, int inNumContactPoints);
  293. MKVAndCreated FindOrCreate(ContactAllocator &ioContactAllocator, const SubShapeIDPair &inKey, uint64 inKeyHash, int inNumContactPoints);
  294. uint32 ToHandle(const MKeyValue *inKeyValue) const;
  295. const MKeyValue * FromHandle(uint32 inHandle) const;
  296. /// Find / create entry for BodyPair -> CachedBodyPair
  297. const BPKeyValue * Find(const BodyPair &inKey, uint64 inKeyHash) const;
  298. BPKeyValue * Create(ContactAllocator &ioContactAllocator, const BodyPair &inKey, uint64 inKeyHash);
  299. void GetAllBodyPairsSorted(Array<const BPKeyValue *> &outAll) const;
  300. void GetAllManifoldsSorted(const CachedBodyPair &inBodyPair, Array<const MKeyValue *> &outAll) const;
  301. void GetAllCCDManifoldsSorted(Array<const MKeyValue *> &outAll) const;
  302. void ContactPointRemovedCallbacks(ContactListener *inListener);
  303. #ifdef JPH_ENABLE_ASSERTS
  304. /// Get the amount of manifolds in the cache
  305. uint GetNumManifolds() const { return mCachedManifolds.GetNumKeyValues(); }
  306. /// Get the amount of body pairs in the cache
  307. uint GetNumBodyPairs() const { return mCachedBodyPairs.GetNumKeyValues(); }
  308. /// Before a cache is finalized you can only do Create(), after only Find() or Clear()
  309. void Finalize();
  310. #endif
  311. /// Saving / restoring state for replay
  312. void SaveState(StateRecorder &inStream) const;
  313. bool RestoreState(const ManifoldCache &inReadCache, StateRecorder &inStream);
  314. private:
  315. /// Block size used when allocating new blocks in the contact cache
  316. static constexpr uint32 cAllocatorBlockSize = 4096;
  317. /// Allocator used by both mCachedManifolds and mCachedBodyPairs, this makes it more likely that a body pair and its manifolds are close in memory
  318. LFHMAllocator mAllocator;
  319. /// Simple hash map for SubShapeIDPair -> CachedManifold
  320. ManifoldMap mCachedManifolds { mAllocator };
  321. /// Simple hash map for BodyPair -> CachedBodyPair
  322. BodyPairMap mCachedBodyPairs { mAllocator };
  323. #ifdef JPH_ENABLE_ASSERTS
  324. bool mIsFinalized = false; ///< Marks if this buffer is complete
  325. #endif
  326. };
  327. ManifoldCache mCache[2]; ///< We have one cache to read from and one to write to
  328. int mCacheWriteIdx = 0; ///< Which cache we're currently writing to
  329. /// World space contact point, used for solving penetrations
  330. class WorldContactPoint
  331. {
  332. public:
  333. /// Calculate constraint properties below
  334. void CalculateNonPenetrationConstraintProperties(float inDeltaTime, const Body &inBody1, const Body &inBody2, Vec3Arg inWorldSpacePosition1, Vec3Arg inWorldSpacePosition2, Vec3Arg inWorldSpaceNormal);
  335. template <EMotionType Type1, EMotionType Type2>
  336. JPH_INLINE void CalculateFrictionAndNonPenetrationConstraintProperties(float inDeltaTime, const Body &inBody1, const Body &inBody2, Mat44Arg inInvI1, Mat44Arg inInvI2, Vec3Arg inWorldSpacePosition1, Vec3Arg inWorldSpacePosition2, Vec3Arg inWorldSpaceNormal, Vec3Arg inWorldSpaceTangent1, Vec3Arg inWorldSpaceTangent2, float inCombinedRestitution, float inCombinedFriction, float inMinVelocityForRestitution);
  337. /// The constraint parts
  338. AxisConstraintPart mNonPenetrationConstraint;
  339. AxisConstraintPart mFrictionConstraint1;
  340. AxisConstraintPart mFrictionConstraint2;
  341. /// Contact cache
  342. CachedContactPoint * mContactPoint;
  343. };
  344. using WorldContactPoints = StaticArray<WorldContactPoint, MaxContactPoints>;
  345. /// Contact constraint class, used for solving penetrations
  346. class ContactConstraint
  347. {
  348. public:
  349. #ifdef JPH_DEBUG_RENDERER
  350. /// Draw the state of the contact constraint
  351. void Draw(DebugRenderer *inRenderer, ColorArg inManifoldColor) const;
  352. #endif // JPH_DEBUG_RENDERER
  353. /// Get the tangents for this contact constraint
  354. JPH_INLINE void GetTangents(Vec3 &outTangent1, Vec3 &outTangent2) const
  355. {
  356. outTangent1 = mWorldSpaceNormal.GetNormalizedPerpendicular();
  357. outTangent2 = mWorldSpaceNormal.Cross(outTangent1);
  358. }
  359. Vec3 mWorldSpaceNormal;
  360. Body * mBody1;
  361. Body * mBody2;
  362. uint64 mSortKey;
  363. float mCombinedFriction;
  364. float mCombinedRestitution;
  365. WorldContactPoints mContactPoints;
  366. };
  367. /// Internal helper function to calculate the friction and non-penetration constraint properties. Templated to the motion type to reduce the amount of branches and calculations.
  368. template <EMotionType Type1, EMotionType Type2>
  369. JPH_INLINE void TemplatedCalculateFrictionAndNonPenetrationConstraintProperties(ContactConstraint &ioConstraint, float inDeltaTime, Mat44Arg inTransformBody1, Mat44Arg inTransformBody2, const Body &inBody1, const Body &inBody2, Mat44Arg inInvI1, Mat44Arg inInvI2);
  370. /// Internal helper function to calculate the friction and non-penetration constraint properties.
  371. inline void CalculateFrictionAndNonPenetrationConstraintProperties(ContactConstraint &ioConstraint, float inDeltaTime, Mat44Arg inTransformBody1, Mat44Arg inTransformBody2, const Body &inBody1, const Body &inBody2);
  372. /// Internal helper function to add a contact constraint. Templated to the motion type to reduce the amount of branches and calculations.
  373. template <EMotionType Type1, EMotionType Type2>
  374. bool TemplatedAddContactConstraint(ContactAllocator &ioContactAllocator, BodyPairHandle inBodyPairHandle, Body &inBody1, Body &inBody2, const ContactManifold &inManifold, Mat44Arg inInvI1, Mat44Arg inInvI2);
  375. /// Internal helper function to warm start contact constraint. Templated to the motion type to reduce the amount of branches and calculations.
  376. template <EMotionType Type1, EMotionType Type2>
  377. JPH_INLINE static void sWarmStartConstraint(ContactConstraint &ioConstraint, MotionProperties *ioMotionProperties1, MotionProperties *ioMotionProperties2, float inWarmStartImpulseRatio);
  378. /// Internal helper function to solve a single contact constraint. Templated to the motion type to reduce the amount of branches and calculations.
  379. template <EMotionType Type1, EMotionType Type2>
  380. JPH_INLINE static bool sSolveVelocityConstraint(ContactConstraint &ioConstraint, MotionProperties *ioMotionProperties1, MotionProperties *ioMotionProperties2);
  381. /// The main physics settings instance
  382. const PhysicsSettings & mPhysicsSettings;
  383. /// Listener that is notified whenever a contact point between two bodies is added/updated/removed
  384. ContactListener * mContactListener = nullptr;
  385. /// Functions that are used to combine friction and restitution of 2 bodies
  386. CombineFunction mCombineFriction = [](const Body &inBody1, const SubShapeID &, const Body &inBody2, const SubShapeID &) { return sqrt(inBody1.GetFriction() * inBody2.GetFriction()); };
  387. CombineFunction mCombineRestitution = [](const Body &inBody1, const SubShapeID &, const Body &inBody2, const SubShapeID &) { return max(inBody1.GetRestitution(), inBody2.GetRestitution()); };
  388. /// The constraints that were added this frame
  389. ContactConstraint * mConstraints = nullptr;
  390. uint32 mMaxConstraints = 0;
  391. atomic<uint32> mNumConstraints { 0 };
  392. /// Context used for this physics update
  393. PhysicsUpdateContext * mUpdateContext;
  394. };
  395. JPH_NAMESPACE_END