Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Calculate the total amount for 5 seconds  (Read 683 times)
0 Members and 1 Guest are viewing this topic.
Offline 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..
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


« 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
Offline 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?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline rikqt

Senior Newbie





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

 Huh
Offline Grunnt

JGO Wizard


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

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

Offline 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.
Offline 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.. ??
Offline 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
Offline 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
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 801
Projects: 4
Exp: 16 years


Hand over your head.


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

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!