SpringPart.h 4.8 KB

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