1
\$\begingroup\$

Math has never been my strong suit and so I'm having trouble figuring out how to write this formula.

When you buy something in my game, the amount it costs to purchase that item will be taken off from your total coin amount one by one. It's a loop that repeats itself till the amount that needs to be taken off is 0. I have that system set in place, but I need to figure out a way to calculate the speed of decrease, or the time between each coin, making sure that each purchase will last the same amount of time, regardless of how much it costs.

What I figured out is that I need to come up with a formula in which the time it takes between each coins should become smaller/shorter the more coins it costs.

e.g. 10 coins taken from your total coin amount should take as long 200 coins. 10 coins -> 0.1 seconds between coins -> 1 second total 200 coins -> 0.005 seconds between coins -> 1 second total 500 coins -> 0.002 seconds between coins -> 1 second total And etc.

It would be greatly appreciated if anyone could push me in the right direction and/or give a little explanation to whether this is possible or not.

Thank you

\$\endgroup\$
3
\$\begingroup\$

The answer to your current question is:

timeSpentOnOneCoin = timeSpentTargetValue * 1 / coinsSpentTargetValue 

Where:

  • timeSpentOnCoin - the value you are looking for
  • timeSpentTargetValue - your desired total time
  • 1 - you are spending one coin at a time
  • coinsSpentTargetValue - amount of coins spent by user on a purchase

However, I assume that the point of this is to combine displaying rapidly changing numbers with some other timed event of a fixed duration (sound or other animation).

In that case you should consider that the number of coins displayed to the user will be updated according to user's current framerate (let's say 60 times a second) - therefore subtracting coins one by one is pointless, instead you should subtract a number of coins proportional to the time that passed between frames.

So the formula for this looks like this:

coinsSpentThisFrame = coinsSpentTargetValue / timeSpentTargetValue * timeSpentThisFrame

You will need to calculate that number on every frame of your animation, as the number of frames per second and the time between them isn't a fixed number.

\$\endgroup\$
0
\$\begingroup\$

Simply divide the desired total time by the number of coins, and you get the time per coin.

Edge cases you might want to be aware of:

  • When you do a transfer of 0 coins for any reason, it gives you a division by zero. You might want to handle that case separately.
  • With a very large number of coins, you might get a shorter time between coins than the timing system of your platform can handle. You might want to test how your platform behaves in that case. A possible workaround might be to transfer coins in larger packets when the transfer amount exceeds a certain threshold.
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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