Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a sprite that I want to speed up as it gets closer to another object. I really want this to flow well and don't want a series of if statements. All I can think of is to take the distance and have some equation pump out a number that slowly rises as the distance decreases.

But for the life of me I can't think of a way to do this, though I'm sure it's possible. My mathematics knowledge is just too lacking to think of the tools I need for this.

Here's a pseudocode breakdown:


Than I'd could do something like:

speed = speed*1.8

I've thought about it and I think this would be similar to gravitation, I've tried some googleing, is there anyone that can point me in the right direction

share|improve this question
up vote 12 down vote accepted
speed = constant_factor / distance

With constant_factor at 60, you get:


If you want to damp the curve a bit, add an exponentiation:

speed = (constant_factor / distance) ^ (1 / damping_factor)

With constant_factor at 80, and damping_factor at 2, you get:


One potential issue is that this will approach infinity as distance approaches zero. If you want to clamp the maximum speed, add an adjustment to distance:

speed = (constant_factor / (distance + clamp_adjustment)) ^ (1 / damping_factor)

Tune as you see appropriate from there.

share|improve this answer
you can also use some_constant_value / (some_other_constant_value + distance) to control maximum result of your function – Ali.S Sep 23 '11 at 20:17
Good call, added that to my post. – ZorbaTHut Sep 23 '11 at 20:43
Additionally, you could square the distance before dividing to get an effect more similar to that of gravity. The change in speed falls off more quickly at increased distances. – chaosTechnician Sep 23 '11 at 20:45
That's actually identical to a damping_factor of 0.5. (1/X) ^ 2 = 1/X * 1/X = 1/(X^2). My general experience is that inverse square behavior doesn't make for great gameplay unless you're actually making a gravity-based game, though :) – ZorbaTHut Sep 23 '11 at 22:13
@Isaiah remember to mark the answer as accepted, if this is perfect for you. – Jari Komppa Sep 25 '11 at 5:08

If talking about gravity, you could consider the gravity equation.. from wikipedia:


  • F is the force between the masses,
  • G is the gravitational constant,
  • m1 is the first mass,
  • m2 is the second mass, and
  • r is the distance between the masses.

share|improve this answer
@Isaiah: The key part is 1/r^2, which gives you "a number that rises as the distance decreases". To apply this force F to your speed, use physics: speed = speed + F/mass (because F = mv; v = F/m | where v = velocity (speed) and m is mass of object that force is being applied to.) – Leftium Sep 25 '11 at 9:12

On a more abstract level, to get an output that decreases as the input increases, you have 2 obvious options:

  • Reciprocal, ie. f(x) = 1/x. So, 50 becomes 1/50, 20 becomes 1/20, and so on. As ZorbaTHut pointed out, the '1' can be any constant, and that changes the shape of the curve but not the direction. As x grows, the curve flattens out, and as x approaches 0, the curve becomes infinitely steep. If x is positive, the output is also positive.
  • Subtraction. ie. f(x) = 100 - x. So 50 becomes 50, 20 becomes 80, and so on. The slope here is constant and directly proportional to x. The 100 I used is again just an arbitrary constant, but if you want all your outputs to be positive then the constant must be larger than any possible value of x.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.