Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (113)
games submitted by our members
Games in WIP (563)
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  
  [libGDX] Calculate seconds based on number of ticks  (Read 150 times)
0 Members and 1 Guest are viewing this topic.
Offline kingroka123

JGO Ninja


Medals: 29
Projects: 5
Exp: 1 year


Gamer's Helmet


« Posted 2014-05-07 23:23:56 »

I am coding a better time feature for my game and it mostly works, except when I try to calculate minutes.

Here is my code. There are 1440 ticks (ticks happen every second), 24 hours, and 60 minutes in every day.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
public static void time() {
      tick++;
      minute=(tick/hour)-60;
      if(hour==13){
         toggle_time();
      }
      if(hour>24){
         hour=1;
         tick=0;
      }
     
     
      if (tick % 60 == 0) {
         hour = Math.round(tick / 60);
         
      }
      if(hour==0){
         hour=1;
      }
      if(hour==10){
         sun_set=true;
         sun_rise=false;
      }
      if(hour==20){
         sun_rise=true;
         sun_set=false;
      }
     
      rot=-(float) (hour/2*30);
      System.out.println("Time: H: "+hour+"; M: "+ minute+"; Tick: "+ tick);

   }


which outputs something like:

1  
2  
3  
4  
5  
6  
Time: H: 15.0; M: 3.9333344; Tick: 959.0
Time: H: 16.0; M: 4.0; Tick: 960.0
Time: H: 16.0; M: 0.0625; Tick: 961.0
Time: H: 16.0; M: 0.125; Tick: 962.0
Time: H: 16.0; M: 0.1875; Tick: 963.0
Time: H: 16.0; M: 0.25; Tick: 964.0


I'm trying to calculate current minutes in the hour based off of the ticks.

any help would be greatly appreciated!  Pointing

Offline UprightPath
« Reply #1 - Posted 2014-05-07 23:53:47 »

Please provide the definitions of all of the variables (Types mainly) because it seems like this might be some sort of arithmetic issue involving types.

The issue is a simple arithmetic one: minute = (tick / hour) - 60 will always return something weird. Since hour is a variable that changes based on the number of times tick % 60 == 0 is true (So at hour 1, it will be tick - 60; hour 2, it will be tick / 2 - 60). You probably want something more like: minute = tick % ticksInMinute, hour = tick % ticksInHour.


A somewhat better/easier way to keep track of this is through an accumulator type system instead. Basically, instead of what you have right now you'd do something like:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
private int currentTicks;
private int hour;
private int minute;
private int minuteAccum;
private int ticksPerMinute;

public void tick() {
   currentTick++;
   minuteAccum++;
   while(minuteAccum > ticksPerMinute) {
      minuteAccum -= ticksPerMinute;
      minute++;
      while(minute > 60) {
         hour++;
         minute -= 60;
      }
   }
   
   /*
   * Do stuff based on time here.
   */

}

Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (16 views)
2014-09-12 09:08:26

Norakomi (50 views)
2014-09-10 13:57:51

TehJavaDev (58 views)
2014-09-10 06:39:09

Tekkerue (27 views)
2014-09-09 02:24:56

mitcheeb (50 views)
2014-09-08 06:06:29

BurntPizza (34 views)
2014-09-07 01:13:42

Longarmx (20 views)
2014-09-07 01:12:14

Longarmx (23 views)
2014-09-07 01:11:22

Longarmx (22 views)
2014-09-07 01:10:19

mitcheeb (31 views)
2014-09-04 23:08:59
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!