Welcome to OStack Knowledge Sharing 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 Answer

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();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...