/// <summary> /// Adds or subtracts from the given number towards the target, but will never pass it. First aligns number to the nearest integer /// </summary> /// <param name="number"></param> /// <param name="target"></param> /// <param name="amount"></param> /// <returns></returns> public static float Approach(this float number, float target, int amount) { double decimalAmount = 0; if (number > target) { decimalAmount = number - Math.Floor(number); } else if (number < target) { decimalAmount = Math.Ceiling(number) - number; } if (decimalAmount == 0) { decimalAmount = 1; } return(number.Approach(target, decimalAmount)); }
private static void TryCorrectPosition(Rectangle tryPosition, TFirst moveableObject, ICollidable collidable, IMovingCollidable movingCollidable, bool correctX, bool correctY, bool correctPastOriginal = false) { float newX = tryPosition.Center.X; float newY = tryPosition.Center.Y; float deltaX = 0, deltaY = 0; if (correctPastOriginal) { newX = newX.Approach(moveableObject.Motion.FrameStartPosition.Center.X, 1); newY = newY.Approach(moveableObject.Motion.FrameStartPosition.Center.Y, 1); deltaX = (newX - moveableObject.Motion.FrameStartPosition.Center.X).Unit(); deltaY = (newY - moveableObject.Motion.FrameStartPosition.Center.Y).Unit(); if (deltaX == 0 && deltaY == 0) { if (movingCollidable == null) { deltaY = -1; } else { deltaY = (newY - movingCollidable.Position.Center.Y).Unit(); } } } while (collidable.DetectCollision(tryPosition, true)) { float newX2 = newX, newY2 = newY; if (correctPastOriginal) { newX2 = newX + deltaX; newY2 = newY + deltaY; } else { if (correctX) { newX2 = newX.Approach(moveableObject.Motion.FrameStartPosition.Center.X, 1); } if (correctY) { newY2 = newY.Approach(moveableObject.Motion.FrameStartPosition.Center.Y, 1); } } if (newX2 == newX && newY2 == newY) { newX = newX2; newY = newY2; break; } else { newX = newX2; newY = newY2; } tryPosition.Center = new Vector2(newX, newY); } }
/// <summary> /// Adds or subtracts from the given number towards the target, but will never pass it. /// </summary> /// <param name="number"></param> /// <param name="target"></param> /// <param name="amount"></param> /// <returns></returns> public static float Approach(this float number, float target, double amount) { return(number.Approach(target, (float)amount)); }