# Equation to make small number big and big number small (gravity)

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:

50->(function)->1.2
20->(function)->1.8


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

speed = constant_factor / distance


With constant_factor at 60, you get:

50->function->1.2
20->function->3.0


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:

50->function->1.26
20->function->2.0


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.

• you can also use some_constant_value / (some_other_constant_value + distance) to control maximum result of your function Sep 23 '11 at 20:17
• Good call, added that to my post. 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. en.wikipedia.org/wiki/Inverse-square_law 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 :) Sep 23 '11 at 22:13
• @Isaiah remember to mark the answer as accepted, if this is perfect for you. Sep 25 '11 at 5:08

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.

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

• 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.

http://en.wikipedia.org/wiki/Newton%27s_law_of_universal_gravitation

• @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.) Sep 25 '11 at 9:12

1 times 10 to the power of 3 is 1000, which is big.

1000 times 10 to the power of negative 3 is 1, which is small.

• This answer would be better if it explained how you'd use these facts about exponentiation to solve OP's speed-deternination problem. Apr 10 '20 at 18:36