2
0

CalculateSolverSteps.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2023 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Physics/PhysicsSettings.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// @cond INTERNAL
  8. /// Internal class used to calculate the total number of velocity and position steps
  9. class CalculateSolverSteps
  10. {
  11. public:
  12. /// Constructor
  13. JPH_INLINE explicit CalculateSolverSteps(const PhysicsSettings &inSettings) : mSettings(inSettings) { }
  14. /// Combine the number of velocity and position steps for this body/constraint with the current values
  15. template <class Type>
  16. JPH_INLINE void operator () (const Type *inObject)
  17. {
  18. uint num_velocity_steps = inObject->GetNumVelocityStepsOverride();
  19. mNumVelocitySteps = max(mNumVelocitySteps, num_velocity_steps);
  20. mApplyDefaultVelocity |= num_velocity_steps == 0;
  21. uint num_position_steps = inObject->GetNumPositionStepsOverride();
  22. mNumPositionSteps = max(mNumPositionSteps, num_position_steps);
  23. mApplyDefaultPosition |= num_position_steps == 0;
  24. }
  25. /// Must be called after all bodies/constraints have been processed
  26. JPH_INLINE void Finalize()
  27. {
  28. // If we have a default velocity/position step count, take the max of the default and the overrides
  29. if (mApplyDefaultVelocity)
  30. mNumVelocitySteps = max(mNumVelocitySteps, mSettings.mNumVelocitySteps);
  31. if (mApplyDefaultPosition)
  32. mNumPositionSteps = max(mNumPositionSteps, mSettings.mNumPositionSteps);
  33. }
  34. /// Get the results of the calculation
  35. JPH_INLINE uint GetNumPositionSteps() const { return mNumPositionSteps; }
  36. JPH_INLINE uint GetNumVelocitySteps() const { return mNumVelocitySteps; }
  37. private:
  38. const PhysicsSettings & mSettings;
  39. uint mNumVelocitySteps = 0;
  40. uint mNumPositionSteps = 0;
  41. bool mApplyDefaultVelocity = false;
  42. bool mApplyDefaultPosition = false;
  43. };
  44. /// @endcond
  45. /// @cond INTERNAL
  46. /// Dummy class to replace the steps calculator when we don't need the result
  47. class DummyCalculateSolverSteps
  48. {
  49. public:
  50. template <class Type>
  51. JPH_INLINE void operator () (const Type *) const
  52. {
  53. /* Nothing to do */
  54. }
  55. };
  56. /// @endcond
  57. JPH_NAMESPACE_END