Jump to content
  • Advertisement
Sign in to follow this  
leif_dragon

[java] Java Lag Issues

This topic is 3625 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a game that is fairly graphically intense. It's a space shooter type with lasers and explosions. Everything runs well, the only problem is that when a large number of lasers/effects, etc, are on the screen, the game tends to lag. This is understandable and in fact expected. Yet the weird thing is, is that I have timed the main game loop when the gameplay is fairly calm (no lasers or anything) the game runs smoothly at this time, and I have also timed the game loop when the game is running some pretty heavy graphics and lagging badly. The result of the 2 different timings is obviously different but not nearly enough to make the game lag so profoundly (roughly 1 ms difference). When the game is lagging severely, the fps counter is still showing a good frame rate (about 63 - 65 fps). Does anyone have any ideas?

Share this post


Link to post
Share on other sites
Advertisement
I think it might help if you clarify what you mean by 'lag'. This is a term that is usually used in network programming to describe high latency. A lot of MMOG players have (unfortunately) hijacked the term to mean 'low frame rates'. But in your case, I can't quite see what you mean by it. You certainly aren't describing high latency. And you don't appear to be talking about low frame rates, since you say you are getting 60 - 65 fps when 'lagging severely'.

So what are you describing here? A sudden decrease in frame rate from some upper N down to 60? Pauses? Stuttering? You need to be more specific.

Share this post


Link to post
Share on other sites
How are you handling input? It may be that even though your framerate is quite high your actual key presses are being delayed and making the controls feel sluggish.

As always, posting relevant code is always helpful.

Share this post


Link to post
Share on other sites
Are you prebuffering the image? using update instead of the paint method for drawing the image?

Are you making sure that the main game thread sleeps long enough for other stuff? On WinXP JVM you can easily get a thread that executes itself immediatly after running if it's told not to sleep. Which means that threads like your mouse listener, key listener, and other stuff won't get their turn in the queue.

On WinXP this could get a little depressing because the system timer resoulution is 15 MilliSeconds so if you tell the loop to sleep for even 3 MilliSeconds it could sleep for 15 or 16 Milliseconds(and often enough 30/33 or 40/45/46) and on a blue moon a bit longer.

Share this post


Link to post
Share on other sites
Sorry for not being more clear. Ill explain better, what I mean by 'lag' is a low frame rate. The weird part is that my FPS counter will be telling me the games frame rate is around 62-65 FPS when it is clearing not that fast (More like 2-5 FPS). This happened whenever I was shooting a large amount of lasers on the screen.

However I believe I have fixed the problem, I was using a highly inefficient method for drawing each laser (Ex. if a laser had length : 15 and size : 5, my method would paint using Graphics.fillOval() 15 times, with ovals of radius 5). I am now using the Graphics.drawLine() method which is infinitely faster.

However, the question still remains. Why does my FPS counter show such a high frame rate when it is clearly not that high.

//FPS method
// ** endTime and startTime are initialized to 0 in class constructor ** //
public void getFrameRate()
{
endTime = System.nanoTime(); // frameRate endtime
if((endTime - startTime) >= 1000000000)
{
startTime = System.nanoTime();
framesPerSecond = frameCount;
frameCount = 0;
}
else
frameCount++;
}

Share this post


Link to post
Share on other sites
PS - The reason I say that 'the frame rate is clearly not as high as the FPS counter claims' is because I can tell by looking at the screen. When the frame counter shows 62 FPS the screen is only updating at about twice a second.

Share this post


Link to post
Share on other sites
System.nanotime has a bug with multicore processors. Basically nanotime gets 'a' processor time and will grab one or the other instead of just one of them.

________

It seems logically sound. I'll show you what I've been using for my game loop logic though. It helps to look at other peoples code I guess.
//VAR DECL
long currentMilli = System.currentTimeMillis();
long prevMilli = currentMilli-50;
long diffMilli, sleepMilli;
//Fraction of second used for all physics
float fracSecond;
//FPS counters
double totSpeed=0, trueFPS=30;
double totTime=0;

__________

//millisecond control
currentMilli = System.currentTimeMillis();
diffMilli=currentMilli-prevMilli;
fracSecond = (float)diffMilli/1000;

//FPS counter
totSpeed+=1;
totTime += fracSecond;

//FPS display
if(totTime>1.0)
{
trueFPS=totSpeed;
//trueFPS -= totTime-1;
totSpeed=0;
totTime=0;
}


//GAME LOOP
//GAME LOOP


//END GAME LOOP
//END GAME LOOP


//prep next loop
prevMilli=currentMilli;

//sleep setting
sleepMilli= 50 - diffMilli;

if(sleepMilli<5)
{

sleepMilli=5; //on XP becomes 15 on other OS it tends to be 1. The JVM will decide all scheduling so for no reason it may wait extra system 'ticks'. May need to be upped if it runs bad on other OS
if(trueFPS<20)
{
System.out.println("Game Systems Running Slow " + trueFPS);
}
}


try
{
Thread.sleep(sleepMilli);
}
catch(Exception e)
{

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!