如何在 C++、SFML 中进行平滑运动 [英] How to make an Smooth Movement in C++, SFML

查看:67
本文介绍了如何在 C++、SFML 中进行平滑运动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 sfml 的 C++ 中平滑移动时遇到问题.有人可以制作一种算法,通过按下按钮将形状/精灵平滑移动 15 像素(精灵/形状为 15 像素)?谢谢.

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.

推荐答案

因为很多其他程序员可能想知道,这里是,但下次问一个更精确的问题
这是我的平稳运动的方法:

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:

开始你的初始动作,比方说 25, 15 每秒
做一个这样的循环:

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

这篇关于如何在 C++、SFML 中进行平滑运动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆