在 C++-SFML 中使对象平滑移动 [英] Make Object Move Smoothly In C++-SFML

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

问题描述

嘿,伙计们,我是 C++ 和 Sfml 游戏开发的初学者我写了这段代码来使那个紫色的对象移动,但问题是它移动不顺畅,就像文本输入一样,如何解决这个问题?

Hey Guys I'm a Beginner in Game Development with C++ and Sfml I Wrote this code to make that purple Object move,but the problem is that it doesn't move smoothly, it's like the text input, How to fix That ?

这是我的代码:

#include <SFML/Graphics.hpp>

int main()
{
    sf::ContextSettings settings;
    settings.antialiasingLevel = 12;

    sf::RenderWindow window(sf::VideoMode(640, 480), "Ala Eddine", sf::Style::Default, settings);

    sf::CircleShape player(20, 5);
    player.setFillColor(sf::Color(150, 70, 250));
    player.setOutlineThickness(4);
    player.setOutlineColor(sf::Color(100, 50, 250));
    player.setPosition(200, 200);

    while(window.isOpen())
    {
        sf::Event event;

        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            {
                window.close();
            }
            //MOOVING PLAYER////////////////////////////////////////
            // moving our player to the right                     //
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)){      //
                                                                  //
                                                                  //
                player.move(3, 0);
            }
            // moving our player to the left
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Q)){
                player.move(-3, 0);
            }
            // moving our player to the UP
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z)){
                player.move(0, -3);
            }
            // moving our player to DOWN
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
                player.move(0, 3);
            }
        }

        window.clear();
        window.draw(player);
        window.display();
    }

    return 0;
}

推荐答案

我假设您的 player.move() 方法只是将偏移量添加到玩家位置.这意味着您的对象将始终以相同的恒定速度移动(假设帧速率是恒定的).你想要的是有一个加速度来更新每一帧的速度.

I assume your player.move() method simply adds the offset to the player position. This means that your object will always move with the same constant velocity (assuming frame rate is constant). What you want instead is to have an acceleration which updates the velocity in every frame.

这是基本思想(对于一个方向;y 方向将相应地起作用,尽管使用矢量会更好):

Here's the basic idea (for one direction; the y direction will work accordingly, although using vectors would be better):

  • 给你的物体一个速度和一个加速度.
  • 如果按住某个键,请将加速度设置为常数项,否则将其设置为零.
  • 在每一帧中,将timestep *加速度添加到速度上.
  • 在每一帧中,将timestep * velocity添加到对象位置.
  • 在每一帧中,将速度乘以某个衰减因子,例如 0.99.
  • Give your object both a velocity and an acceleration.
  • If a key is held down, set the acceleration to a constant term, otherwise set it to zero.
  • In every frame, add timestep * acceleration to the velocity.
  • In every frame, add timestep * velocity to the object position.
  • In every frame, multiply the velocity with some decay factor, say 0.99.

这是假设您有一个固定的时间步长(例如,60 fps 为 1/60 秒).Timestepping 稍微高级一些,我会向您推荐这篇关于该主题的文章.

This is assuming you have a fixed timestep (say, 1/60 s for 60 fps). Timestepping is slightly more advanced, and I'll refer you to this article on the topic.

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

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