[SOLVED] X and Y Coordinate change rate - THX

Hello everyone,

Im developing a simple 2D game. Iv added movement options for left and right, up is jump. My problem is that the movement refresh rate is 5ms, and it moves 1px, sometimes (random times) the movement slows down as if the refresh rate is 10ms or something. Im not sure how i can debug this to find out why its doing this.

Could someone tell me why this might happen and/or how i can fix this or how i can debug to find why.

If youd like to see for yourself you can download the JAR file HERE.

Right arrow = Move Right
Left arrow = Move Left
Up arrow = Jump
I Key = Inventory open/close
Z Key = Pickup item (Green Boxes)

Thanks very much :slight_smile:

You could first try adding System.nanoTime() calls during each update loop and calculate the time they take. This is so you can establish whether or not the update frequency is what is causing the problem. If you are using sleep(), the sleep time is the minimum amount of time slept, but it could sleep longer so that may be the problem.

Delta timing. As IronClawBt said: Calculate the amount of time taken in each loop, and after that check if 5 ms has passed yet. That way you’re independant of the drawing loop.

Iv done so and iv got this:


TB: 171677500409656. TA: 171677500411014. Time Diffrence: 1358
TB: 171677504907948. TA: 171677504908854. Time Diffrence: 906
TB: 171677510269938. TA: 171677510270844. Time Diffrence: 906

TB = Time Before
TA = Time After
Time Difference = (TA - TB)
The time is determined with System.nanoTime();

NOTE: The time diffrence changes alot between 300 - 1900

What happens is the loop get’s called every 5ms, im using new Timer(5, this) to call the actionPreformed method every 5ms…

So the time diffrence does change, how can i make it constant?

Also: Im using a laptop when i remove my power it slow’s down the computer a little. if this happens the whole game get’s slower too and i dono why, any idea’s?

That’s bad, don’t do it .
Use a flag, for exemple “movingRight”, and set it to true if your key is pressed, and to false if your key is released . Every loop interaction check this flag and change coordinates accordingly .
I’ve run your demo and it seems you’re using ints to the coordinates/speed . If your are, don’t do it. Use floats, you’ll get really smoother movement control with that .

[quote=“Mikelmao,post:4,topic:36462”]
Most likely because your laptop underclocks (or w.e. you call it) the cpu in order to save battery

The Swing Timer interval you are using is rather fast. Yes, every 5ms it places another call to your game logic routine onto the EDT (event dispatch thread). Java will run them as fast as they can, but it will complete each call before going on to the next call. So they are possibly backing up on the EDT, and anything else that goes through the EDT has the potential to add to the bottleneck. In some situations, the EDT will “merge” items rather than execute each one separately, so this could be a problem as well.

Continuous 5ms calls come out to a refresh rate of 200 fps (IF they are actually executed at that speed), but I think most folks are thrilled to get an “honest” 60 fps (=16msec).

I’m guessing you should probably convert it to a true game loop, and calculate the sleep amount as suggested by others here. This means getting rid of the Timer. And aim for a lower fps (higher number of msec per loop).

It’s possible to keep the Timer, but a more practical rate might be something like 25msec (=40 fps, which is still faster than television’s 30fps or film’s 24fps rates). I prefer the util Timer to the Swing timer, but the util Timer means you have to anticipate and handle potential concurrency problems.

I recommend not using Swing Timer at all since it uses the EDT, which in this scenario at 200FPS, will collapse multiple calls into one. If your CPU cannot handle all those calls, the EDT will collapse the calls and your FPS will drop sharply. I recommend either using the util Timer or making your own loop.

;D

Thanks all of you for your help and explenation, people like you are helping out the next generation of programmers :slight_smile:

I will try to make a loop and make it sleep for 25ms to get me a frame rate of 40… Ill post later on if this fixed my problem…

One again thx to all of u :slight_smile:

http://pastebin.com/UJufghk8
Here is the Timer class I use to sync my games and other applications with. It provides a much smoother framerate than just a regular Thread.sleep() and it can also calculate your fps.

Feel free to share any improvements to this class :slight_smile:

I got it working properly :).

I made a new Thread with the gameLoop and the thread sleeps after it executes the code. The MS isnt 100% steady, but it only differs 1MS at most unlike the method i used before wich could differ up to 20MS wich is very noticable while your moving

If you want even more precision, use System.nanoTime() :wink:

Glad to help ;D