SpringPart.h 5.0 KB

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