Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (601)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (649)
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 1610 times)
0 Members and 1 Guest are viewing this topic.
Offline wjp

Senior Newbie

Java games rock!

« Posted 2005-04-01 06: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 17: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 07: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 10: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 16:41:17 »

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

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

Senior Newbie


« Reply #5 - Posted 2005-04-14 21: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 11: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.

Jesse (11 views)
2015-07-29 04:35:27

Riven (33 views)
2015-07-27 16:38:00

Riven (17 views)
2015-07-27 15:35:20

Riven (20 views)
2015-07-27 12:26:13

Riven (10 views)
2015-07-27 12:23:39

BurntPizza (29 views)
2015-07-25 00:14:37

BurntPizza (40 views)
2015-07-24 22:06:39

BurntPizza (24 views)
2015-07-24 06:06:53

NoxInc (27 views)
2015-07-22 22:16:53

NoxInc (18 views)
2015-07-22 22:13:39
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 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‑
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!