Advertisement
ShaunJS

Approach()

Jun 4th, 2016
18,325
-1
Never
3
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// Approach(a, b, amount)
  2. // Moves "a" towards "b" by "amount" and returns the result
  3. // Nice bcause it will not overshoot "b", and works in both directions
  4. // Examples:
  5. //      speed = Approach(speed, max_speed, acceleration);
  6. //      hp = Approach(hp, 0, damage_amount);
  7. //      hp = Approach(hp, max_hp, heal_amount);
  8. //      x = Approach(x, target_x, move_speed);
  9. //      y = Approach(y, target_y, move_speed);
  10.  
  11. if (argument0 < argument1)
  12. {
  13.     argument0 += argument2;
  14.     if (argument0 > argument1)
  15.         return argument1;
  16. }
  17. else
  18. {
  19.     argument0 -= argument2;
  20.     if (argument0 < argument1)
  21.         return argument1;
  22. }
  23. return argument0;
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement