ConstraintManager.h 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Physics/Constraints/Constraint.h>
  5. #include <Physics/PhysicsLock.h>
  6. #include <Core/Mutex.h>
  7. namespace JPH {
  8. class IslandBuilder;
  9. class BodyManager;
  10. #ifdef JPH_DEBUG_RENDERER
  11. class DebugRenderer;
  12. #endif // JPH_DEBUG_RENDERER
  13. /// A constraint manager manages all constraints of the same type
  14. class ConstraintManager : public NonCopyable
  15. {
  16. public:
  17. /// Add a new constraint. This is thread safe.
  18. /// Note that the inConstraints array is allowed to have nullptrs, these will be ignored.
  19. void Add(Constraint **inConstraints, int inNumber);
  20. /// Remove a constraint. This is thread safe.
  21. /// Note that the inConstraints array is allowed to have nullptrs, these will be ignored.
  22. void Remove(Constraint **inConstraint, int inNumber);
  23. /// Get total number of constraints
  24. inline uint32 GetNumConstraints() const { return (uint32)mConstraints.size(); }
  25. /// Determine the active constraints of a subset of the constraints
  26. void GetActiveConstraints(uint32 inStartConstraintIdx, uint32 inEndConstraintIdx, Constraint **outActiveConstraints, uint32 &outNumActiveConstraints) const;
  27. /// Link bodies to form islands
  28. void BuildIslands(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, IslandBuilder &ioBuilder, BodyManager &inBodyManager);
  29. /// In order to have a deterministic simulation, we need to sort the constraints of an island before solving them
  30. void SortConstraints(Constraint **inActiveConstraints, uint32 *inConstraintIdxBegin, uint32 *inConstraintIdxEnd) const;
  31. /// Prior to solving the velocity constraints, you must call SetupVelocityConstraints once to precalculate values that are independent of velocity
  32. void SetupVelocityConstraints(Constraint **inActiveConstraints, uint32 inNumActiveConstraints, float inDeltaTime);
  33. /// Same as above, but applies to a limited amount of constraints only
  34. void SetupVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime);
  35. /// Apply last frame's impulses, must be called prior to SolveVelocityConstraints
  36. void WarmStartVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inWarmStartImpulseRatio);
  37. /// This function is called multiple times to iteratively come to a solution that meets all velocity constraints
  38. bool SolveVelocityConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime);
  39. /// This function is called multiple times to iteratively come to a solution that meets all position constraints
  40. bool SolvePositionConstraints(Constraint **inActiveConstraints, const uint32 *inConstraintIdxBegin, const uint32 *inConstraintIdxEnd, float inDeltaTime, float inBaumgarte);
  41. #ifdef JPH_DEBUG_RENDERER
  42. /// Draw all constraints
  43. void DrawConstraints(DebugRenderer *inRenderer) const;
  44. /// Draw all constraint limits
  45. void DrawConstraintLimits(DebugRenderer *inRenderer) const;
  46. /// Draw all constraint reference frames
  47. void DrawConstraintReferenceFrame(DebugRenderer *inRenderer) const;
  48. #endif // JPH_DEBUG_RENDERER
  49. /// Save state of constraints
  50. void SaveState(StateRecorder &inStream) const;
  51. /// Restore the state of constraints. Returns false if failed.
  52. bool RestoreState(StateRecorder &inStream);
  53. /// Lock all constraints. This should only be done during PhysicsSystem::Update().
  54. void LockAllConstraints() { PhysicsLock::sLock(mConstraintsMutex, EPhysicsLockTypes::ConstraintsList); }
  55. void UnlockAllConstraints() { PhysicsLock::sUnlock(mConstraintsMutex, EPhysicsLockTypes::ConstraintsList); }
  56. private:
  57. using Constraints = vector<Ref<Constraint>>;
  58. Constraints mConstraints;
  59. mutable Mutex mConstraintsMutex;
  60. };
  61. } // JPH