0
\$\begingroup\$

(This game is written in C++ with GDI+ It's a simple side-scroller.)

I am having a problem deciding on how I should handle player movement. Currently it runs at 20 frames per second (I just chose that number, it could no doubt run at higher fps.)

Now I have one function that moves the character 1 pixel. It checks collisions with anything around.

There is two ways I can do this (because moving 20px/ second is too slow.)

  • I can use a for loop and then I can move any amount of space per frame. One minor problem is that the player will be moving several pixels between screen updates, so the player moves 5px (or any amount) increments every second.

  • I can turn up the fps, and then, consistently move the character 1px per frame, and it would be smooth movement.

I am having trouble deciding which way of doing this is better and need a bit of input, ideas, advice, etc...

Thanks.

\$\endgroup\$
  • \$\begingroup\$ Why not move the player by more than one pixel ? I mean without a loop, just moving it x pixels and then check the collisions. And what is the problem with moving 5 pixels per frame ? \$\endgroup\$ – Heckel Jul 14 '14 at 22:02
2
\$\begingroup\$

There is nothing wrong with moving multiple pixels per frame. In fact, that's typical of most games. Even 2D games often store the character's position using floats, typically in a different space (world space), and then translate that to view space and then to pixels before rendering.

The main problem you might run into is that is the character moves really fast, and you only check collisions once per frame, you might actually miss a collision. In practice this only happens for objects that move REALLY fast, like bullets. In this case you can do multiple collision checks, like you suggest in your first option, or do collision using a different test that takes into account the movement (Google Continuous Collision Detection).

I would strongly advice to decouple the movement from the framerate, so you can tune the velocity of the character without affecting the smoothness. Also, bear in mind that some systems may have inconsistent framerate, which will make the game play badly.

Finally, 20 fps is low for most games, especially action games like a side-scroller. Aim at least for 30 fps, try for 60 if your game requires quick reflexes.

\$\endgroup\$
  • \$\begingroup\$ Thanks. I moved the key down and movement into the windows message. Then I use the timer to only draw frames. \$\endgroup\$ – Evan Carslake Jul 15 '14 at 5:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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