Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (534)
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  
  Can't get consistent FPS in Applet  (Read 1455 times)
0 Members and 1 Guest are viewing this topic.
Offline wjp

Senior Newbie




Java games rock!


« Posted 2005-04-01 08:55:23 »

Hi everyone,

I have a problem thats really starting to do my head in!  Cry
I have a online game that is run via an applet in a browser. It works OK, except that I can't get consistent timing for the FPS. It has balls which move about, but sometimes they speed up/slow down non- linear manner. When I run the same game throught AppletViewer is nice and smooth. Its only when I run in an applet I have the issues. However I'm stuck using and applet as it needs to be easy and quick to use for the online users.

The code for the thread is :

long framerate = 1000 / 30; //  frames per sec
long oldtime;
while (true) {
     oldtime = System.currentTimeMillis();

     // update object movement                        
     // draw stuff
     
     long currenttime = System.currentTimeMillis();
     if (currenttime - oldtime < framerate) {
     try {
           Thread.sleep((oldtime + framerate) - currenttime);
     } catch (InterruptedException e) {
     }
}


Edited so say: I'm using a doubled BufferedImage to do the drawing. I'm not sure if its an issue with the consistency of the FPS or whether its the timing between calculating the movement of the objects. I'm not calling pain directly rather update() FYI.t
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #1 - Posted 2005-04-01 19:45:18 »

Don't lock the framerate.  That might be causing the problem.  Do all calculations based on time.

Offline wjp

Senior Newbie




Java games rock!


« Reply #2 - Posted 2005-04-04 09:41:52 »

But is is based on time no?
what do you mean? (sorry bit slow today)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #3 - Posted 2005-04-04 12:10:25 »

You are locking you framerate to 30 FPS.  So all your updates occur each frame.

If you use time based movement, you just start your loop, check how much time has elasped since the last update and update all game objects based on time.  This will give you more consistent results.

ie, if you have a game object that moves 50 pixels per second and it has been 20 milliseconds since the last update then you move it 1 pixel.  If the next update is 40 milliseconds, the 2 pixels.

20 milliseconds = 0.02 seconds.
50 pixels per seconds * 0.02 seconds = 1 pixel

It is also advisable to have all variables that are affected by time be floating point.

Offline jonask84

Senior Newbie




Can't make it? Simulate it!


« Reply #4 - Posted 2005-04-10 18:41:17 »

This is the framelimiter i came up with, don't know if its any good, but it seems to work. =)

           //framelimiter-------------------------
           while(System.currentTimeMillis() < nextTick){
               try {Thread.sleep(1);}
               catch (InterruptedException e) {}
           }
           nextTick = System.currentTimeMillis() + 30;
           //-------------------------------------
Offline allan_s

Senior Newbie




pwned


« Reply #5 - Posted 2005-04-14 23:35:50 »

Thread.sleep(1) is not a good idea.  It has been mentioned on many threads in these forums that the granularity of the timer for Java in Windows is 10ms, which means that your thread will not reliably sleep for any time period less than 10ms.  I have also read that sleeping for less than 10ms on a Windows machine can cause the system's clock to drift, which  is not cool.  All this is assuming you plan on running your software in Windows.  If you don't, ignore everything I just said :].

Offline jonask84

Senior Newbie




Can't make it? Simulate it!


« Reply #6 - Posted 2005-04-20 13:15:43 »

No, i am running it under windows. thanks for the heads up =)
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.

pw (35 views)
2014-07-24 01:59:36

Riven (33 views)
2014-07-23 21:16:32

Riven (21 views)
2014-07-23 21:07:15

Riven (24 views)
2014-07-23 20:56:16

ctomni231 (55 views)
2014-07-18 06:55:21

Zero Volt (47 views)
2014-07-17 23:47:54

danieldean (38 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!