Guest User

Untitled

a guest
Jul 27th, 2017
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. /// <summary>
  2. /// Does a fake simulation of a spring that can be swung to an arbitrary target, with hard max clamps and a defined number of bounces on reaching a non-max value.
  3. /// </summary>
  4. /// <param name="curAngle">the angle the spring is currently at</param>
  5. /// <param name="curVel">the current velocity with which we're moving to target</param>
  6. /// <param name="bounceCount">the number of allowed bounces remaining before reaching current target</param>
  7. /// <param name="bounceDamp">the damping applied for each successive bounce around target</param>
  8. /// <param name="accel">the acceleration we apply when moving toward a target</param>
  9. /// <param name="targetAngle">the angle to which you want the spring to</param>
  10. /// <param name="angleExtent">the hard stops for the spring angle</param>
  11. /// <param name="deltaTime">the amount of time passed since last update of this spring</param>
  12. /// <returns>Number of "arcs" remaining (number of bounces remaining + 1 if it hasn't reached target), 0 if stable-state at target</returns>
  13. static public int FakeSpring(ref float curAngle, ref float curVel, ref int bounceCount, float bounceDamp, float accel, float targetAngle, float angleExtent, float deltaTime)
  14. {
  15. // (quick fall-through for when we're already there / there's nothing to do)
  16. if (MathManager.IsNear(curAngle, targetAngle, 2.0f) && MathManager.IsNearZero(curVel)) { return 0; }
  17.  
  18. angleExtent = Mathf.Abs(angleExtent); // (just in case)
  19.  
  20. bool reachedTarget = false;
  21. if (curAngle <= targetAngle)
  22. {
  23. curVel += accel * deltaTime;
  24. curAngle += curVel * deltaTime;
  25.  
  26. if (curAngle >= targetAngle)
  27. {
  28. if (bounceCount <= 0)
  29. {
  30. curVel = 0;
  31. curAngle = targetAngle;
  32.  
  33. reachedTarget = true;
  34. }
  35. else
  36. {
  37. bounceCount--;
  38. curVel *= bounceDamp;
  39. }
  40. }
  41. }
  42. else // (curAngle > targetAngle)
  43. {
  44. curVel -= accel * deltaTime;
  45. curAngle += curVel * deltaTime;
  46.  
  47. if (curAngle <= targetAngle)
  48. {
  49. if (bounceCount <= 0)
  50. {
  51. curVel = 0;
  52. curAngle = targetAngle;
  53.  
  54. reachedTarget = true;
  55. }
  56. else
  57. {
  58. bounceCount--;
  59. curVel *= bounceDamp;
  60. }
  61. }
  62. }
  63.  
  64. if (curAngle < -angleExtent)
  65. {
  66. curAngle = -angleExtent;
  67. // (we don't auto-reach-target because we COULD have bounced to max while aiming for a not-quite-max angle)
  68. if (targetAngle <= -angleExtent) { reachedTarget = true; }
  69. curVel = 0.0f;
  70. }
  71. if (curAngle > angleExtent)
  72. {
  73. curAngle = angleExtent;
  74. // (we don't auto-reach-target because we COULD have bounced to max while aiming for a not-quite-max angle)
  75. if (targetAngle >= angleExtent) { reachedTarget = true; }
  76. curVel = 0.0f;
  77. }
  78.  
  79. int arcCount = bounceCount + 1;
  80. if (reachedTarget) { arcCount--; }
  81.  
  82. return arcCount;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment