简体   繁体   中英

C++ SFML. How to create a diminishing(shrinking) circle

I have a class of circles that appear and disappear in the window for a while, there may be several, or maybe one. Currently drawn circles are stored in the vector_of_current_circles vector. I need to make them shrink to a certain size over time. How to do it?

window while loop:

while (window.isOpen()) {
    // check all the window's events that were triggered since the last iteration of the loop
    sf::Event event;
    while (window.pollEvent(event)) {
        // "close requested" event: we close the window
        if (event.type == sf::Event::Closed)
            window.close();
    }
    for (int i = 0; i < vector_of_circles.size(); i++) {
        if (std::fabs(vector_of_circles[i].getBeginOfLife() - clock.getElapsedTime().asSeconds()) < 1e-2) {
            if (!vector_of_circles[i].get_is_drawn()) {
                window.clear();
                window.draw(sprite);
                vector_of_current_circles.push_back(vector_of_circles[i]);
                for (const auto &item : vector_of_current_circles) {
                    item.print_circle(window);
                }
                window.display();
                vector_of_circles[i].set_is_drawn();
            }
        }
        if (std::fabs(vector_of_circles[i].getEndOfLife() - clock.getElapsedTime().asSeconds()) < 1e-2) {
            if (vector_of_circles[i].get_is_drawn()) {
                vector_of_current_circles.erase(vector_of_current_circles.begin());
                vector_of_circles[i].set_is_drawn();
            }
            window.clear();
            window.draw(sprite);
            for (const auto &item : vector_of_current_circles) {
                item.print_circle(window);
            }
            window.display();
        }
    }
}

Here is Circle code:

private:
    sf::CircleShape circle_;
    //sf::Clock clock;
    float begin_of_life_;
    bool is_drawn_ = false;
    float end_of_life_;
    //sf::RenderWindow& window_;
public:
    Circle();
    void print_circle(sf::RenderWindow&) const;
    float get_radius() const;
    void set_position(float, float);
    void set_texture(sf::Texture&);
    void setBeginOfLife(float);
    void setEndOfLife(float);
    double getBeginOfLife() const;
    double getEndOfLife() const;
    bool get_is_drawn() const;
    void set_is_drawn();

To reduce equaly a circl in a certain time with a certain speed, you need:

  • speed ( speed_ ) value: the speed of the reduice of radius by second,
  • radius ( radius_ ) value: the initial value of the radius. Your circle need to have setOrigin to the center.
// your function to reduce a certain circle (class member)
void reduce()
{
    float elapsed_time = ; // your time elapsed from the last call
    // Getting the position of your center
    // if you haven't set the origin to the center of the circle this code doesn't work 
    sf::Vector2f pos = circle_.getPosition();

    radius_ -= (speed_ * elapsed_time); // calculating the new radius
    circle_.setRadius(radius_); // set the new radius
    circle_.setOrigin(sf::Vector2f(radius_ / 2, radius_ / 2)); // update the origine to the center
    circle_.setPosition(pos); // not 100% sure that this line is used 
}

Now you have a circle that reduce at a certain speed and staying in the same place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM