0
votes

As a student, I've always wanted to become a game developer. So far I've managed to learn the basics of C++ and SFML 2.1

For my first project, upon deciding to make a 2D RPG-ish game, I encountered a problem that the game loop refreshes so quickly that if I move them by 1 pixel per frame, the sprite will move more quick than I intended to.

I implemented a frame-determined movement system(not sure what it's called) that whatever the fps is, the character moves at same speed.

However, I couldn't solve the problem(which is what I'm asking) that the sprite won't move smoothly. What's happening in my build is that the player will move about 1px per frame, and can be interrupted by other inputs. How can I do this?

TL;DR : I want to move my sprite like old-school RPG games, moving predetermined pixels(or tiles) smoothly.

1
You may have better luck at gamedev.stackexchange.com. - Chris Hayes
You generally want movement to be based on time, not frames. - Retired Ninja
Think of how velocity is measured in real life: Distance divided by time. Displacement is then velocity times time. Well, you can model movement similarly in your game. When you set an object's velocity, set it in units like "pixels per second", and when you compute how far an object has moved, multiply the velocity by the time elapsed to get the displacement since the last update. - Joe Z
I think I wasn't clear enough. Sorry. What I want the program to do is when I press 'Right' key once, sprite moves towards the positive side of x-axis, for 32px. What's currently functioning in my program is the sprite will move only once, 1px. - user3128244
Take a look at delta timing. In otherwords, you use the time that has passed since the objects last update (or equivalent) member function is called, multiply your speed by the time that has passed. - OMGtechy

1 Answers

0
votes

the number of times your loop can be processed per second is going to be statistically random. There's a lot more going on in a computer, and the time that your program will be given is not guaranteed to be constant. You need to base your movement on time.

In SFML I like to

window.setFramerateLimit(60);

This will give me a very good attempt at 60 frames per second. It's close enough to reality that it's indistinguishable to the eye.

From there I like to assume that each frame will receive a

float timeStep = 1.0f/60.0f.

I use this timeStep to control motions. So if I want my car to drive 10 pixels per second, then in each iteration of the big game loop, we use the timestep.

currentPosition += carSpeed * timeStep