ConstraintManager.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. ///
  19. /// WARNING: This class is an internal part of PhysicsSystem, it has no functions that can be called by users of the library.
  20. /// Its functionality is exposed through PhysicsSystem and BodyInterface.
  21. class JPH_EXPORT ConstraintManager : public NonCopyable
  22. {
  23. public:
  24. JPH_OVERRIDE_NEW_DELETE
  25. #ifdef JPH_ENABLE_ASSERTS
  26. /// Constructor
  27. ConstraintManager(PhysicsLockContext inContext) : mLockContext(inContext) { }
  28. #endif // JPH_ENABLE_ASSERTS
  29. /// Add a new constraint. This is thread safe.
  30. void Add(Constraint **inConstraints, int inNumber);
  31. /// Remove a constraint. This is thread safe.
  32. void Remove(Constraint **inConstraint, int inNumber);
  33. /// Get a list of all constraints
  34. Constraints GetConstraints() const;
  35. /// Get total number of constraints
  36. inline uint32 GetNumConstraints() const { return uint32(mConstraints.size()); }
  37. /// Determine the active constraints of a subset of the constraints
  38. void GetActiveConstraints(uint32 inStartConstraintIdx, uint32 inEndConstraintIdx, Constraint **outActiveConstraints, uint32 &outNumActiveConstraints) const;
  39. /// Link bodies to form islands
  40. static void sBuildIslands(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, IslandBuilder &ioBuilder, BodyManager &inBodyManager);
  41. /// In order to have a deterministic simulation, we need to sort the constraints of an island before solving them
  42. static void sSortConstraints(Constraint **inActiveConstraints, uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd);
  43. /// Prior to solving the velocity constraints, you must call SetupVelocityConstraints once to precalculate values that are independent of velocity
  44. static void sSetupVelocityConstraints(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, float inDeltaTime);
  45. /// Apply last frame's impulses, must be called prior to SolveVelocityConstraints
  46. template <class ConstraintCallback>
  47. static void sWarmStartVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio, ConstraintCallback &ioCallback);
  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. #ifdef JPH_DEBUG_RENDERER
  53. /// Draw all constraints
  54. void DrawConstraints(DebugRenderer *inRenderer) const;
  55. /// Draw all constraint limits
  56. void DrawConstraintLimits(DebugRenderer *inRenderer) const;
  57. /// Draw all constraint reference frames
  58. void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const;
  59. #endif // JPH_DEBUG_RENDERER
  60. /// Save state of constraints
  61. void SaveState(StateRecorder &inStream, const StateRecorderFilter *inFilter) const;
  62. /// Restore the state of constraints. Returns false if failed.
  63. bool RestoreState(StateRecorder &inStream);
  64. /// Lock all constraints. This should only be done during PhysicsSystem::Update().
  65. void LockAllConstraints() { PhysicsLock::sLock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }
  66. void UnlockAllConstraints() { PhysicsLock::sUnlock(mConstraintsMutex JPH_IF_ENABLE_ASSERTS(, mLockContext, EPhysicsLockTypes::ConstraintsList)); }
  67. private:
  68. #ifdef JPH_ENABLE_ASSERTS
  69. PhysicsLockContext mLockContext;
  70. #endif // JPH_ENABLE_ASSERTS
  71. Constraints mConstraints;
  72. mutable Mutex mConstraintsMutex;
  73. };
  74. JPH_NAMESPACE_END