Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (856)
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  
  Framerate-independent movement  (Read 6753 times)
0 Members and 1 Guest are viewing this topic.
Offline sharpshooter

Senior Newbie

Java games rock!

« Posted 2003-02-11 17:24:56 »

I'm writing an Arkanoid/Breakout clone in Java (fullscreen exclusive mode). What's the best way to make the movement/speed of the ball independent of framerate? Currently I do step() (which moves the ball a certain number of pixels in x and y) once every frame. Obviously, this isn't what one would like, I just did this for testing purposes. So, is there an optimal way of handling the movement? Suggestions?  Smiley
Offline Stuart Still

Senior Newbie

« Reply #1 - Posted 2003-02-11 17:56:13 »

The way to do it goes something like this (pseudo-code)

1.  When game is loaded grab and store the time, and render the screen.
2. At the start of the next frame grab the time again and work out the time since the last grab of the time. Store the new time.
3.  Distance moved = the speed (a constant of your choice for a nice speed) * the time.

For example, you load the game and call System.currentTimeMillis().  You store this in a long variable called lastTime.  You render the scene.  Get the time again and calculate the difference since the first time, multiplying this time by the speed, say for example the speed is 10 and 0.1secs have passed, you move the paddle 10*0.1 = 1 pixel.  You then store the new time into the lastTime variable and repeat?

Hope that is a start, try coding it now Smiley

Offline sharpshooter

Senior Newbie

Java games rock!

« Reply #2 - Posted 2003-02-11 18:29:06 »

Yup, thought about doing it like that. I hope the accuracy/resolution of System.currentTimeMillis() is good enough for this. If not, I suppose movement may become less smooth than desired. Well I'll just have to try it and find out. Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Stuart Still

Senior Newbie

« Reply #3 - Posted 2003-02-11 18:33:05 »

Well, I am using this in a game I am writing, and it is not far off from being accurate enough, check the shared code section for information on more accurate timers!

8: Undefined index: online
File: /home/jgo/public_html/Themes/default/Display.template.php (main sub template - eval?)
Line: 161