我使用SFML和Box2d创建了一个简单的游戏。 但我遇到了问题。物理时间取决于玩家的帧率。
我已经获得了该播放器的FPS,但我不确定如何获得相同的时间。我也没有发现任何公式......有人可以帮助我吗?
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.display();
sf::Time time = clock.getElapsedTime();
float fps = 1.0f / time.asSeconds();
std::cout << 1.0f / time.asSeconds() << std::endl;
world.getBWorld()->Step(60/1, 8, 3);
clock.restart();
}
答案 0 :(得分:0)
You need to use a delta time.
In graphics programming, the term delta time is usually used for variably updating scenery based on the elapsed time since the game last updated. Wikipedia
sf::Clock deltaClock;
while (window.isOpen()) {
{
// ...
sf::Time dt = deltaClock.Restart(); // Restart returns the elapsed time
}
Then you can move your player with:
mySprite.Move(velocity * dt.AsSeconds());