Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// 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.
- /// </summary>
- /// <param name="curAngle">the angle the spring is currently at</param>
- /// <param name="curVel">the current velocity with which we're moving to target</param>
- /// <param name="bounceCount">the number of allowed bounces remaining before reaching current target</param>
- /// <param name="bounceDamp">the damping applied for each successive bounce around target</param>
- /// <param name="accel">the acceleration we apply when moving toward a target</param>
- /// <param name="targetAngle">the angle to which you want the spring to</param>
- /// <param name="angleExtent">the hard stops for the spring angle</param>
- /// <param name="deltaTime">the amount of time passed since last update of this spring</param>
- /// <returns>Number of "arcs" remaining (number of bounces remaining + 1 if it hasn't reached target), 0 if stable-state at target</returns>
- static public int FakeSpring(ref float curAngle, ref float curVel, ref int bounceCount, float bounceDamp, float accel, float targetAngle, float angleExtent, float deltaTime)
- {
- // (quick fall-through for when we're already there / there's nothing to do)
- if (MathManager.IsNear(curAngle, targetAngle, 2.0f) && MathManager.IsNearZero(curVel)) { return 0; }
- angleExtent = Mathf.Abs(angleExtent); // (just in case)
- bool reachedTarget = false;
- if (curAngle <= targetAngle)
- {
- curVel += accel * deltaTime;
- curAngle += curVel * deltaTime;
- if (curAngle >= targetAngle)
- {
- if (bounceCount <= 0)
- {
- curVel = 0;
- curAngle = targetAngle;
- reachedTarget = true;
- }
- else
- {
- bounceCount--;
- curVel *= bounceDamp;
- }
- }
- }
- else // (curAngle > targetAngle)
- {
- curVel -= accel * deltaTime;
- curAngle += curVel * deltaTime;
- if (curAngle <= targetAngle)
- {
- if (bounceCount <= 0)
- {
- curVel = 0;
- curAngle = targetAngle;
- reachedTarget = true;
- }
- else
- {
- bounceCount--;
- curVel *= bounceDamp;
- }
- }
- }
- if (curAngle < -angleExtent)
- {
- curAngle = -angleExtent;
- // (we don't auto-reach-target because we COULD have bounced to max while aiming for a not-quite-max angle)
- if (targetAngle <= -angleExtent) { reachedTarget = true; }
- curVel = 0.0f;
- }
- if (curAngle > angleExtent)
- {
- curAngle = angleExtent;
- // (we don't auto-reach-target because we COULD have bounced to max while aiming for a not-quite-max angle)
- if (targetAngle >= angleExtent) { reachedTarget = true; }
- curVel = 0.0f;
- }
- int arcCount = bounceCount + 1;
- if (reachedTarget) { arcCount--; }
- return arcCount;
- }
Advertisement
Add Comment
Please, Sign In to add comment