Witam!
Chciałbym uzyskać taki efekt obracających się sześciokątów. Z początku myślałem, że wystarczy nadać im obrót w zależności od odległości od pierwszego zaczynającego obrót. Było to mylne. Możecie mi doradzić jak mógłbym to zrobić? Może powinienem zacząć od początku?
To co mam:
#include <iostream>
#include <vector>
#include <cmath>
#include <SFML/Graphics.hpp>
class Hexagon : public sf::CircleShape
{
public:
Hexagon()
{
Hexagon(0.f);
}
Hexagon(float radius)
{
setRadius(radius);
setPointCount(6);
setPosition(0, 0);
rotate(30);
setFillColor(sf::Color::White);
setOrigin(radius, radius);
}
};
int main()
{
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(1024, 720), "Sernik", sf::Style::Default, settings);
sf::Clock clock;
std::vector<Hexagon> hexagons;
size_t line = 0;
const float hex_width = 70.f;
const float hex_height = hex_width * std::sqrt(3.f) / 2.f;
for (float y = 0; y < 800; y += hex_height)
{
line++;
for (float x = 0; x < 1100; x += hex_width)
{
Hexagon hex(hex_width / 2.f);
if (line % 2 == 0)
hex.setPosition(sf::Vector2f(x, y));
else
hex.setPosition(sf::Vector2f(x + hex_width / 2.f, y));
hexagons.push_back(hex);
}
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
if (clock.getElapsedTime() > sf::seconds(1.f / 60.f))
{
clock.restart();
const static float delta_angle = 2.0;
if (int(hexagons.front().getRotation()) % 30 == 0)
{
sf::sleep(sf::milliseconds(400));
}
window.clear();
for (auto& hex : hexagons)
{
hex.rotate(delta_angle);
window.draw(hex);
}
window.display();
}
}
}