-1

I am building a game using Sprite Kit and I want to gradually increase the difficulty (starting at 1.0) based on the time since starting the game.

Someone suggested that I should use a logarithmic calculation for this but I'm unsure how to implement this in Objective-C.

- (float)difficulty
{
  timeSinceStart = ???; // I don't what kind of object this should be to make it play nice w/ `log`
  return log(???);
}

Update #1

I know that I need to use the log method but I'm uncertain what values I need to pass to it.

Kyle Decot
  • 20,715
  • 39
  • 142
  • 263
  • this isn't a programming question but more math problem. – John Riselvato Feb 19 '14 at 18:46
  • @JohnRiselvato I realized that it's mostly a math question but it's also a programming question because I don't know the objc method equivalents of the math involved. Also, I don't know what kind of object my "time since starting" should be. – Kyle Decot Feb 19 '14 at 18:48
  • The time value that you pass to `log()` will have to be of type `double`, or you will have to typecast it to a `double`. – Tyler Gaona Feb 19 '14 at 19:02

3 Answers3

1

Objective C is a superset of the C language, therefore you can use "math.h".

The function that computes the natural logarithm from "math.h" is double log(double x);

EDIT

Since you want the difficulty to increase as a function of time, you would pass the time as the argument to log(double x). How you would use that to calculate and change the "difficulty" is an entirely different question.

If you want to change the shape of the curve, either multiply the expression by a constant, as in 2*log(x) or multiply the parameter by a constant, as in log(2*x). You will have to look at the individual curves to see what will work best for your specific application.

Tyler Gaona
  • 479
  • 1
  • 4
  • 14
  • 3
    Note that you can calculate a logarithm of any base by doing `log(x)/log(base)`. – Richard J. Ross III Feb 19 '14 at 18:50
  • I see that I can use the `log` function but I'm uncertain what I need to pass to it in order to get a gradual curve. – Kyle Decot Feb 19 '14 at 18:51
  • I am unsure what you are asking. The natural logarithm is a single curve. Passing any value x to that function will return its y value. – Tyler Gaona Feb 19 '14 at 18:52
  • @TylerGaona well I want to have the initial difficulty be 1.0 and then gradually increase based on the time elapsed (which I assume) to be `x`. I guess what I don't know is how to effect how steep/gradual the curve is. – Kyle Decot Feb 19 '14 at 18:56
0

Since log(1.0) == 0 you probably want to do some scaling and translation. Use something like 1.0 + log(1.0 + c * time). At time zero this will give a difficulty of 1.0, and as time advances the difficulty will increase at a progressively slower pace whose rate is determined by c. Small values such as c = 0.01 will give a slow ramp-up, larger values will ramp-up faster.

pjs
  • 18,696
  • 4
  • 27
  • 56
0

@pjs gave a pretty clear answer. As to how to figure out the time: You probably want the amount of time spent actually playing, rather than elapsed time since launching the game.

So you will nee to track total time played, game after game.

I suggest you create an entry in NSUserDefaults. You can save and load double values to user defaults using the NSUserDefaults methods setDouble:forKey: and doubleForKey:

I would create an instance variable startPlayingTime, type double

When you start the game action running, capture the start time using

startPlayingTime = [NSDate timeIntervalSinceReferenceDate];

When the user passes the game/exits to the background, use

NSTimeInterval currentPlayTime =  [NSDate timeIntervalSinceReferenceDate] - startPlayingTime;

Then read the total time played from user defaults, add currentPlayTime to it, and save it back to user defaults.

You can then make your game difficulty based on

difficulty = log(1+ c * totalPlayTime);

(as explained by pjs, above) and pick some appropriate value for C.

Duncan C
  • 128,072
  • 22
  • 173
  • 272