Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 [libGDX] Calculate seconds based on number of ticks  (Read 1173 times) 0 Members and 1 Guest are viewing this topic.
kingroka123
 « 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.0Time: H: 16.0; M: 4.0; Tick: 960.0Time: H: 16.0; M: 0.0625; Tick: 961.0Time: H: 16.0; M: 0.125; Tick: 962.0Time: H: 16.0; M: 0.1875; Tick: 963.0Time: 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!
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

 DesertCoockie (33 views) 2018-05-13 18:23:11 nelsongames (75 views) 2018-04-24 18:15:36 nelsongames (70 views) 2018-04-24 18:14:32 ivj94 (752 views) 2018-03-24 14:47:39 ivj94 (82 views) 2018-03-24 14:46:31 ivj94 (622 views) 2018-03-24 14:43:53 Solater (98 views) 2018-03-17 05:04:08 nelsongames (179 views) 2018-03-05 17:56:34 Gornova (405 views) 2018-03-02 22:15:33 buddyBro (1065 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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