ConstraintManager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /// Note that the inConstraints array is allowed to have nullptrs, these will be ignored.
  28. void Add(Constraint **inConstraints, int inNumber);
  29. /// Remove a constraint. This is thread safe.
  30. /// Note that the inConstraints array is allowed to have nullptrs, these will be ignored.
  31. void Remove(Constraint **inConstraint, int inNumber);
  32. /// Get a list of all constraints
  33. Constraints GetConstraints() const;
  34. /// Get total number of constraints
  35. inline uint32 GetNumConstraints() const { return uint32(mConstraints.size()); }
  36. /// Determine the active constraints of a subset of the constraints
  37. void GetActiveConstraints(uint32 inStartConstraintIdx, uint32 inEndConstraintIdx, Constraint **outActiveConstraints, uint32 &outNumActiveConstraints) const;
  38. /// Link bodies to form islands
  39. static void sBuildIslands(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, IslandBuilder &ioBuilder, BodyManager &inBodyManager);
  40. /// In order to have a deterministic simulation, we need to sort the constraints of an island before solving them
  41. static void sSortConstraints(Constraint **inActiveConstraints, uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd);
  42. /// Prior to solving the velocity constraints, you must call SetupVelocityConstraints once to precalculate values that are independent of velocity
  43. static void sSetupVelocityConstraints(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, float inDeltaTime);
  44. /// Apply last frame's impulses, must be called prior to SolveVelocityConstraints
  45. static void sWarmStartVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio);
  46. /// Same as above but also calculates the number of velocity steps
  47. static void sWarmStartVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio, int &ioNumVelocitySteps);
  48. /// This function is called multiple times to iteratively come to a solution that meets all velocity constraints
  49. static bool sSolveVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime);
  50. /// This function is called multiple times to iteratively come to a solution that meets all position constraints
  51. static bool sSolvePositionConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime, float inBaumgarte);
  52. /// Same as above but also calculates the number of position steps
  53. static bool sSolvePositionConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime, float inBaumgarte, int &ioNumPositionSteps);
  54. #ifdef JPH_DEBUG_RENDERER
  55. /// Draw all constraints
  56. void DrawConstraints(DebugRenderer *inRenderer) const;
  57. /// Draw all constraint limits
  58. void DrawConstraintLimits(DebugRenderer *inRenderer) const;
  59. /// Draw all constraint reference frames
  60. void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const;
  61. #endif // JPH_DEBUG_RENDERER
  62. /// Save state of constraints
  63. void SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const;
  64. /// Restore the state of constraints. Returns false if failed.
  65. bool RestoreState(StateRecorder &inStream);
  66. /// Lock all constraints. This should only be done during PhysicsSystem::Update().
  67. void LockAllConstraints() { PhysicsLock::sLock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }
  68. void UnlockAllConstraints() { PhysicsLock::sUnlock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }
  69. private:
  70. #ifdef JPH_ENABLE_ASSERTS
  71. PhysicsLockContext mLockContext;
  72. #endif // JPH_ENABLE_ASSERTS
  73. Constraints mConstraints;
  74. mutable Mutex mConstraintsMutex;
  75. };
  76. JPH_NAMESPACE_END