Java-Gaming.org
 Featured games (81) games approved by the League of Dukes Games in Showcase (487) Games in Android Showcase (112) games submitted by our members Games in WIP (553) 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 667 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
« League of Dukes »

JGO Overlord

Medals: 783
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 Wizard

Medals: 66
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..

opiop65

JGO Kernel

Medals: 154
Projects: 7
Exp: 3 years

JumpButton Studios

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

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
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
« League of Dukes »

JGO Overlord

Medals: 783
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

You cannot reply to this message, because it is very, very old.

 Add your game by posting it in the WIP section, or publish it in Showcase. The first screenshot will be displayed as a thumbnail.
 TehJavaDev (16 views) 2014-08-28 18:26:30 CopyableCougar4 (25 views) 2014-08-22 19:31:30 atombrot (38 views) 2014-08-19 09:29:53 Tekkerue (35 views) 2014-08-16 06:45:27 Tekkerue (32 views) 2014-08-16 06:22:17 Tekkerue (20 views) 2014-08-16 06:20:21 Tekkerue (31 views) 2014-08-16 06:12:11 Rayexar (66 views) 2014-08-11 02:49:23 BurntPizza (44 views) 2014-08-09 21:09:32 BurntPizza (34 views) 2014-08-08 02:01:56
 List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27Resources for WIP games2014-08-01 16:20:17Resources for WIP games2014-08-01 16:19:50List of Learning Resources2014-07-31 16:29:50List of Learning Resources2014-07-31 16:26:06List of Learning Resources2014-07-31 11:54:12HotSpot Optionsby dleskov2014-07-08 01:59: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