Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

How to make an Smooth Movement in C++, SFML

I have a problem doing an smooth movement in c++ w/ sfml. Someone can make an algorithm to smooth move a shape / sprite by 15 px (the sprite / shape is 15 px) by pressing a button? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because many other programmers might want to know that, here it is, but next time ask a more precise question
Here's my way to make a smooth movement:

Take your initial movement, lets say 25, 15 per second
Make a loop like this:

void GameEngine::GameLoop()
{
    sf::Clock timer;
    sf::Time tickRate;

    sf::Vector2f movement(25.0,15.0); // Your movement vector

    while(/* wahtever */){
        tickRate = timer.restart();
        yourShape.move(movement * ((float)tickRate.asMilliseconds() / 1000)); //Move your shape depending on the time elapsed between two frame

        yourWindow.clear();
        yourWindow.draw(yourShape);
        yourWindow.display();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...