Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
add comment

3 Answers

up vote 12 down vote accepted
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.

share|improve this answer
2  
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
3  
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 –  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
1  
@Isaiah remember to mark the answer as accepted, if this is perfect for you. –  Jari Komppa Sep 25 '11 at 5:08
show 2 more comments

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

share|improve this answer
1  
@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
add comment

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
add comment

Your Answer

 
discard

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.