【问题标题】:How to make sf::Vector2f transform(float t) speed faster?如何使 sf::Vector2f transform(float t) 速度更快?
【发布时间】:2021-06-02 06:20:01
【问题描述】:
#include <iostream>
#include <SFML/Graphics.hpp>


float  period_ms = 5000.f;
using namespace std;

sf::Vector2f transform(float t)
{
    float const ellipse_width = 500.f;
    float const ellipse_height = 500.f;
    float const a = ellipse_width / 2.f;
    float const b = ellipse_height / 2.f;
    float const pi = 3.141592653589f;
    float const tau = 2.f * pi;

    float const x = (std::fmodf(t, period_ms) / period_ms) * tau;
    return sf::Vector2f(a * std::cos(x), b * std::sin(x));
}
int main()
{
    sf::ContextSettings settings;
    settings.antialiasingLevel = 8;
    sf::RenderWindow window(sf::VideoMode(1000, 1000), "SFML shapes", sf::Style::Default, settings);
    window.setFramerateLimit(144);

    //white orbitting circle
    sf::CircleShape shape(28.f);
    shape.setFillColor(sf::Color::Black);
    shape.setOutlineColor(sf::Color::Red);
    shape.setOutlineThickness(5);
    //white orbitting circle

    //center red circle
    sf::CircleShape shapeTwo(208.f);
    shapeTwo.setFillColor(sf::Color::Red);
    shapeTwo.setOutlineThickness(5);
    shapeTwo.setOutlineColor(sf::Color(250, 150, 100));
    shapeTwo.setPosition(325.00, 320.00);
    //base red
    sf::Clock clock;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        float const t = static_cast<float>(clock.getElapsedTime().asMilliseconds());
        shape.setPosition(sf::Vector2f(500.f, 500.f) + transform(t));

        window.clear();
        window.draw(shape);
        window.draw(shapeTwo);
        window.display();
        cout << "Orbitting X:  " << shape.getPosition().x << ", Y: " << shape.getPosition().y << endl;

    }

    return 0;
}

如何在不重置形状位置的情况下使用 sf::Vector2f 更改圆的旋转速度? 就像,我将它设置在 if 条件下,period_ms 降低,以使循环更快,但是在满足条件时这样做,会使形状的位置重置。

【问题讨论】:

  • 在 Vector2F 上使用时提高转换速度
  • 我在这里没有看到圆圈?你能提供一个 MCVE 吗?
  • 我添加了源代码。

标签: c++ sfml


【解决方案1】:

您可以添加时间因子变量。这需要你改变一些关于时间管理的事情:

  • 每帧重新启动时钟并将时间累积到一个单独的变量中。
  • 有一个带有时间因子的新变量,它乘以每帧的增量时间。
float timeFactor = 1.0f;
float accTime = 0.0f;

例如,您可以在按空格键时复制它:

while (window.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
            window.close();
        else if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Space)
            timeFactor = 2.0f;
        else if(event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space)
            timeFactor = 1.0f;
        
    }

使用它来乘以您在转换中使用的时间并重新启动时钟

float const dt = static_cast<float>(clock.getElapsedTime().asMilliseconds()); // delta time
accTime += dt * timeFactor;
shape.setPosition(sf::Vector2f(500.f, 500.f) + transform(accTime));
clock.restart();

请注意,即使这样可行,period 变量也可能存在语义歧义,因为现在它仅表示时间因子为 1 的时段的值。

【讨论】:

  • 什么意思?
  • 不是SFML语言的吗?如何在我的代码中使用它?
  • SFML 不是一种语言,它是一个库。它甚至在名称中:简单的快速媒体库。你的问题中的代码(我假设你写的)是 C++,我的也是。
  • 听起来您对编程很陌生...如果是这种情况,我建议您在使用 SFML 之前正确学习 C++,否则您可能会遇到不必要的困难。
  • 谢谢,效果很好。 event.type == sf::KeyPressed 应该是 event.type == sf::Event::KeyPressed 但我必须包含 Event.Hpp
猜你喜欢
  • 2018-06-17
  • 2010-09-09
  • 2013-10-21
  • 2019-04-20
  • 1970-01-01
  • 1970-01-01
  • 2019-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多