如何使 sf::Vector2f transform(float t) 速度更快?
How to make sf::Vector2f transform(float t) speed faster?
#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 降低,以使循环更快,但是当满足条件时这样做,会使形状的位置重置。
您可以添加时间因素变量。这需要你改变一些关于时间管理的东西:
- 每帧重新启动时钟并将时间累积到一个单独的变量中。
- 有一个带有时间因子的新变量,它乘以每帧的增量时间。
float timeFactor = 1.0f;
float accTime = 0.0f;
例如,您可以在按 space 时复制它:
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 的时间段的值。
#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 降低,以使循环更快,但是当满足条件时这样做,会使形状的位置重置。
您可以添加时间因素变量。这需要你改变一些关于时间管理的东西:
- 每帧重新启动时钟并将时间累积到一个单独的变量中。
- 有一个带有时间因子的新变量,它乘以每帧的增量时间。
float timeFactor = 1.0f;
float accTime = 0.0f;
例如,您可以在按 space 时复制它:
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 的时间段的值。