Calculate the total amount for 5 seconds

Happy New year folks,

I’m stuck in computing… here is what I have, simple stuffs:

totalAmount = 2250;
introTime = 5000;
currentValue = 0;

I’m trying to compute, the amount of value which should increase for a period of a 5s in a update method.

So, current Value, for a 1 second, should increase to 450

currentValue += totalAmount  / (introTime / 1000)

The problem is, I’m increasing currentValue for 450 each time update occurs…

Then divide the value with your update rate.

currentValue += totalAmount  / (introTime / delta)

With this I get 2133, instead 2250. Should I have a pass time variable and divide it by it instead of delta?

???

Could you explain what you are trying to achieve with this? It is kinda hard to understand what you are trying to do…

How do you know

 currentValue 

should be 450 each time your loop updates? If you know it’s going to be 450, why not just add 450 and then reset the value each time the program goes through the loop? I don’t entirely understand your question.

You lose precision with integer. 10/3 = 3, not 3.(3). Same here. currentValue should be a float. You should get getter for currentValue or something and cast it to an int from float. You can also use double, but floats are usually enough for simple tasks which don’t require some kind of incredible precision. I hear that floats are faster than doubles… Don’t know about that though. Anyway, use floats or doubles instead of ints for such computation like the one you are trying to achieve.

Alright, I’ll repeat… So game starts, and in a middle of the screen appears some total amount of value which player can earn for this match. What I want to achieve with this is to for those 5 seconds, the totalAmount will start increasing, from 0 to 2250, so the idea is something like incrementing the value from 0-100…

Well, when you get to 5s, totalAmount at that point should be 2250, because every second totalAmount value is increased for 450:
1s - 450
2s - 900
3s - 1350
4s - 1800
5s - 2250

EDIT: I might be wrong, it should not maybe be at that point 1s - 450, as I aim to compute the amount of the value it should increase for, to reach that 2250.
So it can even be 453…

You are right, I forgot that, I used int type for this variables… However, I’m still getting not precise number 2250, instead now I get the 2253, 2256, 2254, I assume it’s because I’m dividing with delta… ??

I’d suggest to count in fixed steps, e.g. 10 or 25, and increment whenever the time has advanced far enough. Everything else will give you rounding errors.

2500 / 10 = 250; 5s / 250 = 0.02s -> so increment by 25 every 0.02 seconds, until the 2500 is reached. A few milliseconds more or less until the 2500 isn’t that notable to the player, I assume.

One problem is that the amount of time per update may vary or be imprecise. If you base the calculation on the elapsed time you might get enough precision to arrive at the exact number 2250, but then again maybe not. How do you know, for example, that the 5 second point is the exact point at which the update happens?

Here is another idea. Make the update of the currentValue independent of the gameloop execution time. This can be done as follows:

(1) Keep track of elapsed time with every gameloop update.

(2) Don’t update the current value every update loop. Instead, test if the elapsed time has surpassed a defined required amount. Perhaps the updates only happen when a full second has elapsed, or this is done every half second or quarter second.

(3) Award the unit of current value that corresponds and reset the elapsed time.

Example:


    if (elapsedTime > 1000)
    {
        currentValue += 450;
        elapsedTime -= 1000;
    }

Or, if every 1/10th of a second:


    if (elapsedTime > 100)
    {
        currentValue += 45;
        elapsedTime -= 100;
    }

[Edit: this is the same idea as Varkas, basically. His post appeared while I was writing mine.]

Thank you guys on the tips… I’m going to sleep now, so I will leave this for tomorrow, hopefully I will be able to make this out.

and ofcource change those if statements to while statements.