Timer question

Hello all,

I wish there was a "noobie" section to post, as I'm sure this falls in it, but here it goes.

I'm working on a simple pong game to begin learning JME.  I am looking for how to control the speed of the ball, so it doesn't move

faster on faster computers, and vice versa.

Is there something within JME that I should call to lock the movement?

Do I need to write my own little test to check time in between frames and only move after a certain amount of time?

Do I need to incorporate the physics engine in which may have some of this in it?

Something else obvious I'm missing?

Again, this is for a simple game, so simple is good :slight_smile:

Sorry if this has been asked before, looked at guides and forums, and didn't find answer, or maybe I did but didn't recognize it was the answer.

Thanks for all the great support on this forum!!!

Most common way (also the way SimpleGame does it if you are already using that).


float tpf = timer.getTimePerFrame();

tpf can now be used to regulate your speed. So if you have something like:

//pseudo code

ball.setPosition(ball.oldPosition() * ball.velocity()); //old

you can have

//pseduo code

ball.setPosition(ball.oldPosition() * ball.speed() * tpf); //new

so, older machines with a higher tpf will move the ball further per round, while faster machines will move is shorter, this insures that in the same amount of real-time the ball is in the same position.

we’re all newbs at some point, and seen as how im new to jme too ,Ill try to help out a fellow newb as best i can.

I think mojo handled your question, but i thought id throw this in..what the heck.<br /> <br /> I have been working with things sortof along these lines, well more about fps rates then logic rates but anyway, and i think i might have a quick solution for you so that you can focus on other things for the time being. Here is the thread Im refering to: http://www.jmonkeyengine.com/jmeforum/index.php?topic=1935.0

fixedRateGame and FixedLogicrateGame both provide the facility to limit the frame/logic rate in order not get into the “fast as possible” loops.

here is the javadoc description from FixedLogicrateGame to give you an idea

 * <code>FixedLogicrateGame</code> implements a main-loop designed to acheive
 * frame rate independence. The goal is to keep a consistent gameplay speed
 * regardless of the framerate acheived by the visuals. The game renders as fast
 * as the hardware permits, while running the game logic at a fixed rate. The
 * concept behind this is forcing every game logic tick to represent a fixed
 * amount of real-time. For example, if the logic is updated at a rate of 15
 * times per second, and we have a person moving at 30 pixels per second, each
 * update the person should move 2 pixels. <br>
 * To compensate for the non-constant frame rate, we smooth the visuals using
 * interpolation. So, if the scene is rendered twice without the game logic
 * being updated, we do not render the same thing twice. <br>
 * Using a fixed-timestep model has a number of benefits. Game logic is
 * simplified as there is no longer any need to add "*deltaTime" to acheive
 * frame rate independence. There is also a gain in efficiency: the logic can be
 * run at a lower frequency than the rendering, meaning that the logic may be
 * updated only once every second game - a net save in time. In addition,
 * because the exact same sequence of game logic code is executed every time,
 * the game becomes deterministic (that is to say, it will run the exact same
 * way every time). <br>
 * Further extension of this class could be used to integrate both a fixed logic
 * rate and a fixed frame rate.
 * @author Eric Woroshow
 * @version $Id: FixedLogicrateGame.java,v 1.6 2004/04/26 18:56:01 mojomonkey

Ah.  Thank you both for your suggestions.

I am using the SimpleGame already, so I just tried adding the *tpf into the equation, and walah. 

Seems to be just right!  I need to look closer at that guide.pdf, see what other goodies are in that SimpleGame I missed.

And looking through the other thread about FixedGameRate, I can see the benefits of this described.  I'm going to take a

further peak at it too.

Anyways, thanks both of you for your responses!!!


Is there a way to apply a fixed frame rate to a SimpleJMEApplet like this???

I've tried mojomonk's trick but my games gets extremelly slow. Using my machine it runs as fast as I want it to run, but I fear it will become crazy if I run it on a faster machine (or a slower one). Will this happen??

Thanks in advance, I will appreciate any help or advice!!

float tpf = timer.getTimePerFrame();

tpf can now be used to regulate your speed. So if you have something like:

//pseudo code
ball.setPosition(ball.oldPosition() * ball.velocity()); //old

If your code above runs at the speed you want without the * tpf, then sounds like your velocity is not per second, but per some fraction of a second that your machine happens to run at.  What you want is to increase the speed to be "per second" based.  Then multiplying it by tpf will make sense across all machines.

for example, (thinking linearly here, I'll leave it up to you to move this to 3d as needed) if your velocity is say 100 currently and your machine is running at 75 fps.  Then your per second velocity is 75*100 or 7,500.  Change the velocity to 7500 and use the * tpf.

Btw, the pseduo code you have for setting ball position does not look right.  I think you'd really want to do: position = oldPosition + (velocity * time);  Where velocity is a vector.

Thanks, great info, I'll try it!.

BTW: The code I quoted is Mojomonk's, not mine  :wink: