Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (804) Games in Android Showcase (237) games submitted by our members Games in WIP (867) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Calculate the total amount for 5 seconds  (Read 4000 times) 0 Members and 1 Guest are viewing this topic.
rikqt

Senior Newbie

 « Posted 2014-01-01 14:12:47 »

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

 1 `currentValue += totalAmount  / (introTime / 1000)`

The problem is, I'm increasing currentValue for 450 each time update occurs..
Riven

« JGO Overlord »

Medals: 1371
Projects: 4
Exp: 16 years

 « Reply #1 - Posted 2014-01-01 14:24:53 »

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

...

The problem is, I'm increasing currentValue for 450 each time update occurs..
Then divide the value with your update rate.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
rikqt

Senior Newbie

 « Reply #2 - Posted 2014-01-01 14:28:57 »

 1 `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?
rikqt

Senior Newbie

 « Reply #3 - Posted 2014-01-01 14:58:04 »

Grunnt

JGO Kernel

Medals: 143
Projects: 8
Exp: 5 years

Complex != complicated

 « Reply #4 - Posted 2014-01-01 16:47:06 »

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

Opiop
 « Reply #5 - Posted 2014-01-01 17:04:01 »

How do you know
 1 ` 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.
trollwarrior1
 « Reply #6 - Posted 2014-01-01 19:43:56 »

 1 `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?

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

Senior Newbie

 « Reply #7 - Posted 2014-01-01 23:17:38 »

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

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

How do you know
 1 ` 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.

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

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.. ??
Varkas
 « Reply #8 - Posted 2014-01-02 01:14:08 »

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.

if (error) throw new Brick(); // Blog (german): http://gedankenweber.wordpress.com
philfrei
 « Reply #9 - Posted 2014-01-02 01:18:33 »

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:
 1  2  3  4  5 `    if (elapsedTime > 1000)    {        currentValue += 450;        elapsedTime -= 1000;    }`

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

 1  2  3  4  5 `    if (elapsedTime > 100)    {        currentValue += 45;        elapsedTime -= 100;    }`

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

rikqt

Senior Newbie

 « Reply #10 - Posted 2014-01-02 01:26:09 »

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

« JGO Overlord »

Medals: 1371
Projects: 4
Exp: 16 years

 « Reply #11 - Posted 2014-01-02 06:59:35 »

and ofcource change those if statements to while statements.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Pages: [1]
 ignore  |  Print

 Riven (463 views) 2019-09-04 15:33:17 hadezbladez (5322 views) 2018-11-16 13:46:03 hadezbladez (2249 views) 2018-11-16 13:41:33 hadezbladez (5597 views) 2018-11-16 13:35:35 hadezbladez (1164 views) 2018-11-16 13:32:03 EgonOlsen (4600 views) 2018-06-10 19:43:48 EgonOlsen (5474 views) 2018-06-10 19:43:44 EgonOlsen (3138 views) 2018-06-10 19:43:20 DesertCoockie (4033 views) 2018-05-13 18:23:11 nelsongames (4782 views) 2018-04-24 18:15:36
 A NON-ideal modular configuration for Eclipse with JavaFXby philfrei2019-12-19 19:35:12Java Gaming Resourcesby philfrei2019-05-14 16:15:13Deployment and Packagingby philfrei2019-05-08 15:15:36Deployment and Packagingby philfrei2019-05-08 15:13:34Deployment and Packagingby philfrei2019-02-17 20:25:53Deployment and Packagingby mudlee2018-08-22 18:09:50Java Gaming Resourcesby gouessej2018-08-22 08:19:41Deployment and Packagingby gouessej2018-08-22 08:04:08
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org