0
\$\begingroup\$

hello again I have a basic platformer game and I want to make an fps counter and cap the fps to 30 but I don't know how. I want this because when I start up the game sometimes the character moves realy fast and other times very slow.

\$\endgroup\$
0
\$\begingroup\$

You should probably use framerate-independent movement, because then you wouldn't have to care about the FPS.

What you are probably doing right now is in every frame you say position += velocity;, where position is in units of distance (whatever they may be--pixels, meters, etc.), and velocity is in units of distance per frame.

However, what you should probably do is have velocity instead be in units of distance per time instead of distance per time. Then what you would do is position += velocity * frameTime;, where frameTime is 1/FPS, and is in units of time (seconds, to be specific).

Now, no matter what the framerate is, your character will move the same distance in the same amount of time.

\$\endgroup\$
0
\$\begingroup\$

I believe what you want to add is a fixed timestep. If you wish to make your game's logic independent of your framerate, you'll want to look into adding a delta - a timestep that calculates the time between update calls.

For a more in-depth explanation, check out DeWitter's article: http://www.koonsolo.com/news/dewitters-gameloop/

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.