ConstraintManager.h 4.4 KB

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