SpringPart.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. JPH_NAMESPACE_BEGIN
  6. #ifndef JPH_PLATFORM_DOXYGEN // Somehow Doxygen gets confused and thinks the parameters to CalculateSpringProperties belong to this macro
  7. JPH_MSVC_SUPPRESS_WARNING(4723) // potential divide by 0 - caused by line: outEffectiveMass = 1.0f / inInvEffectiveMass, note that JPH_NAMESPACE_BEGIN already pushes the warning state
  8. #endif // !JPH_PLATFORM_DOXYGEN
  9. /// Class used in other constraint parts to calculate the required bias factor in the lagrange multiplier for creating springs
  10. class SpringPart
  11. {
  12. public:
  13. /// Calculate spring properties
  14. ///
  15. /// @param inDeltaTime Time step
  16. /// @param inInvEffectiveMass Inverse effective mass K
  17. /// @param inBias Bias term (b) for the constraint impulse: lambda = J v + b
  18. /// @param inC Value of the constraint equation (C). Set to zero if you don't want to drive the constraint to zero with a spring.
  19. /// @param inFrequency Oscillation frequency (Hz). Set to zero if you don't want to drive the constraint to zero with a spring.
  20. /// @param inDamping Damping factor (0 = no damping, 1 = critical damping). Set to zero if you don't want to drive the constraint to zero with a spring.
  21. /// @param outEffectiveMass On return, this contains the new effective mass K^-1
  22. inline void CalculateSpringProperties(float inDeltaTime, float inInvEffectiveMass, float inBias, float inC, float inFrequency, float inDamping, float &outEffectiveMass)
  23. {
  24. outEffectiveMass = 1.0f / inInvEffectiveMass;
  25. // Soft constraints as per: Soft Contraints: Reinventing The Spring - Erin Catto - GDC 2011
  26. if (inFrequency > 0.0f)
  27. {
  28. // Calculate angular frequency
  29. float omega = 2.0f * JPH_PI * inFrequency;
  30. // Calculate spring constant k and drag constant c (page 45)
  31. float k = outEffectiveMass * Square(omega);
  32. float c = 2.0f * outEffectiveMass * inDamping * omega;
  33. // Note that the calculation of beta and gamma below are based on the solution of an implicit Euler integration scheme
  34. // This scheme is unconditionally stable but has built in damping, so even when you set the damping ratio to 0 there will still
  35. // be damping. See page 16 and 32.
  36. // Calculate softness (gamma in the slides)
  37. // See page 34 and note that the gamma needs to be divided by delta time since we're working with impulses rather than forces:
  38. // softness = 1 / (dt * (c + dt * k))
  39. mSoftness = 1.0f / (inDeltaTime * (c + inDeltaTime * k));
  40. // Calculate bias factor (baumgarte stabilization):
  41. // beta = dt * k / (c + dt * k) = dt * k^2 * softness
  42. // b = beta / dt * C = dt * k * softness * C
  43. mBias = inBias + inDeltaTime * k * mSoftness * inC;
  44. // Update the effective mass, see post by Erin Catto: http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=4&t=1354
  45. //
  46. // Newton's Law:
  47. // M * (v2 - v1) = J^T * lambda
  48. //
  49. // Velocity constraint with softness and Baumgarte:
  50. // J * v2 + softness * lambda + b = 0
  51. //
  52. // where b = beta * C / dt
  53. //
  54. // We know everything except v2 and lambda.
  55. //
  56. // First solve Newton's law for v2 in terms of lambda:
  57. //
  58. // v2 = v1 + M^-1 * J^T * lambda
  59. //
  60. // Substitute this expression into the velocity constraint:
  61. //
  62. // J * (v1 + M^-1 * J^T * lambda) + softness * lambda + b = 0
  63. //
  64. // Now collect coefficients of lambda:
  65. //
  66. // (J * M^-1 * J^T + softness) * lambda = - J * v1 - b
  67. //
  68. // Now we define:
  69. //
  70. // K = J * M^-1 * J^T + softness
  71. //
  72. // So our new effective mass is K^-1
  73. outEffectiveMass = 1.0f / (inInvEffectiveMass + mSoftness);
  74. }
  75. else
  76. {
  77. mSoftness = 0.0f;
  78. mBias = inBias;
  79. }
  80. }
  81. /// Returns if this spring is active
  82. inline bool IsActive() const
  83. {
  84. return mSoftness != 0.0f;
  85. }
  86. /// Get total bias b, including supplied bias and bias for spring: lambda = J v + b
  87. inline float GetBias(float inTotalLambda) const
  88. {
  89. // Remainder of post by Erin Catto: http://www.bulletphysics.org/Bullet/phpBB3/viewtopic.php?f=4&t=1354
  90. //
  91. // Each iteration we are not computing the whole impulse, we are computing an increment to the impulse and we are updating the velocity.
  92. // Also, as we solve each constraint we get a perfect v2, but then some other constraint will come along and mess it up.
  93. // So we want to patch up the constraint while acknowledging the accumulated impulse and the damaged velocity.
  94. // To help with that we use P for the accumulated impulse and lambda as the update. Mathematically we have:
  95. //
  96. // M * (v2new - v2damaged) = J^T * lambda
  97. // J * v2new + softness * (total_lambda + lambda) + b = 0
  98. //
  99. // If we solve this we get:
  100. //
  101. // v2new = v2damaged + M^-1 * J^T * lambda
  102. // J * (v2damaged + M^-1 * J^T * lambda) + softness * total_lambda + softness * lambda + b = 0
  103. //
  104. // (J * M^-1 * J^T + softness) * lambda = -(J * v2damaged + softness * total_lambda + b)
  105. //
  106. // So our lagrange multiplier becomes:
  107. //
  108. // lambda = -K^-1 (J v + softness * total_lambda + b)
  109. //
  110. // So we return the bias: softness * total_lambda + b
  111. return mSoftness * inTotalLambda + mBias;
  112. }
  113. private:
  114. float mBias = 0.0f;
  115. float mSoftness = 0.0f;
  116. };
  117. JPH_NAMESPACE_END