ConstraintManager.h 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/Constraints/Constraint.h>
  6. #include <Jolt/Physics/PhysicsLock.h>
  7. #include <Jolt/Core/Mutex.h>
  8. JPH_NAMESPACE_BEGIN
  9. class IslandBuilder;
  10. class BodyManager;
  11. class StateRecorderFilter;
  12. #ifdef JPH_DEBUG_RENDERER
  13. class DebugRenderer;
  14. #endif // JPH_DEBUG_RENDERER
  15. /// A list of constraints
  16. using Constraints = Array<Ref<Constraint>>;
  17. /// A constraint manager manages all constraints of the same type
  18. class JPH_EXPORT ConstraintManager : public NonCopyable
  19. {
  20. public:
  21. JPH_OVERRIDE_NEW_DELETE
  22. #ifdef JPH_ENABLE_ASSERTS
  23. /// Constructor
  24. ConstraintManager(PhysicsLockContext inContext) : mLockContext(inContext) { }
  25. #endif // JPH_ENABLE_ASSERTS
  26. /// Add a new constraint. This is thread safe.
  27. void Add(Constraint **inConstraints, int inNumber);
  28. /// Remove a constraint. This is thread safe.
  29. void Remove(Constraint **inConstraint, int inNumber);
  30. /// Get a list of all constraints
  31. Constraints GetConstraints() const;
  32. /// Get total number of constraints
  33. inline uint32 GetNumConstraints() const { return uint32(mConstraints.size()); }
  34. /// Determine the active constraints of a subset of the constraints
  35. void GetActiveConstraints(uint32 inStartConstraintIdx, uint32 inEndConstraintIdx, Constraint **outActiveConstraints, uint32 &outNumActiveConstraints) const;
  36. /// Link bodies to form islands
  37. static void sBuildIslands(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, IslandBuilder &ioBuilder, BodyManager &inBodyManager);
  38. /// In order to have a deterministic simulation, we need to sort the constraints of an island before solving them
  39. static void sSortConstraints(Constraint **inActiveConstraints, uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd);
  40. /// Prior to solving the velocity constraints, you must call SetupVelocityConstraints once to precalculate values that are independent of velocity
  41. static void sSetupVelocityConstraints(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, float inDeltaTime);
  42. /// Apply last frame's impulses, must be called prior to SolveVelocityConstraints
  43. template <class ConstraintCallback>
  44. static void sWarmStartVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio, ConstraintCallback &ioCallback);
  45. /// This function is called multiple times to iteratively come to a solution that meets all velocity constraints
  46. static bool sSolveVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime);
  47. /// This function is called multiple times to iteratively come to a solution that meets all position constraints
  48. static bool sSolvePositionConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime, float inBaumgarte);
  49. #ifdef JPH_DEBUG_RENDERER
  50. /// Draw all constraints
  51. void DrawConstraints(DebugRenderer *inRenderer) const;
  52. /// Draw all constraint limits
  53. void DrawConstraintLimits(DebugRenderer *inRenderer) const;
  54. /// Draw all constraint reference frames
  55. void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const;
  56. #endif // JPH_DEBUG_RENDERER
  57. /// Save state of constraints
  58. void SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const;
  59. /// Restore the state of constraints. Returns false if failed.
  60. bool RestoreState(StateRecorder &inStream);
  61. /// Lock all constraints. This should only be done during PhysicsSystem::Update().
  62. void LockAllConstraints() { PhysicsLock::sLock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }
  63. void UnlockAllConstraints() { PhysicsLock::sUnlock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }
  64. private:
  65. #ifdef JPH_ENABLE_ASSERTS
  66. PhysicsLockContext mLockContext;
  67. #endif // JPH_ENABLE_ASSERTS
  68. Constraints mConstraints;
  69. mutable Mutex mConstraintsMutex;
  70. };
  71. JPH_NAMESPACE_END