Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
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  
  [SOLVED] X and Y Coordinate change rate - THX  (Read 1209 times)
0 Members and 1 Guest are viewing this topic.
Offline Mikelmao

Senior Newbie





« Posted 2011-03-21 11:05:56 »

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 Smiley
Offline IronclawsBt

Junior Member


Medals: 1



« Reply #1 - Posted 2011-03-21 12:12:06 »

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.

It's not what you know, it's what other people think you know.
Just hope you don't get quizzed on it.
Game engine design tutorials
Offline Mads

JGO Ninja


Medals: 24
Projects: 3


One for all!


« Reply #2 - Posted 2011-03-21 13:01:14 »

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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mikelmao

Senior Newbie





« Reply #3 - Posted 2011-03-21 13:19:14 »

Iv done so and iv got this:
1  
2  
3  
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?
Offline teletubo
« League of Dukes »

JGO Ninja


Medals: 48
Projects: 6
Exp: 8 years



« Reply #4 - Posted 2011-03-21 13:48:53 »

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

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 .

Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #5 - Posted 2011-03-21 15:31:22 »

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?

Most likely because your laptop underclocks (or w.e. you call it) the cpu in order to save battery
Offline philfrei
« Reply #6 - Posted 2011-03-22 02:06:09 »

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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #7 - Posted 2011-03-22 03:08:40 »

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.

Grin

Offline Mikelmao

Senior Newbie





« Reply #8 - Posted 2011-03-22 07:49:58 »

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

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 Smiley
Offline Captain Awesome

Junior Member


Medals: 2


Hi


« Reply #9 - Posted 2011-03-22 09:07:11 »

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

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 Smiley
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 Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mikelmao

Senior Newbie





« Reply #10 - Posted 2011-03-22 09:26:23 »

I got it working properly Smiley.

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
Offline ra4king

JGO Kernel


Medals: 322
Projects: 2
Exp: 4 years


I'm the King!


« Reply #11 - Posted 2011-03-22 18:48:03 »

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

Glad to help Grin

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.

xsi3rr4x (48 views)
2014-04-15 18:08:23

BurntPizza (44 views)
2014-04-15 03:46:01

UprightPath (60 views)
2014-04-14 17:39:50

UprightPath (42 views)
2014-04-14 17:35:47

Porlus (58 views)
2014-04-14 15:48:38

tom_mai78101 (82 views)
2014-04-10 04:04:31

BurntPizza (140 views)
2014-04-08 23:06:04

tom_mai78101 (240 views)
2014-04-05 13:34:39

trollwarrior1 (200 views)
2014-04-04 12:06:45

CJLetsGame (207 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!