ConstraintManager.h 5.0 KB

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