Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Object is erasing consecutive variable change

I am creating a project where for now I just want it to move a particle on a graphic window(SFML lib).
I look into creatinng multiple objects to interact with each other. So, i made a tick function that triggers all object’s tick function to handle collision, movement ect…
My problem is that each time I trigger my tick function, the position is adding to itself from the initial value I put in the constructor and not the already updated one causing the particle to not move at all.
I would apreciate any piece of advice or solutions !

#include <iostream>
#include <SFML/Graphics.hpp>

class Particle
{
public:
    float size{};
    sf::CircleShape shape;
    sf::Vector2f pos;


    Particle(float s) {
        size = s;
        shape = sf::CircleShape(size);
        shape.setFillColor(sf::Color::White);

    }
    void begin() {
        
        pos = sf::Vector2f(100, 100);

    }

    void tick(sf::RenderWindow& window) {
        
        pos.x += 10;
        shape.setPosition(pos);
        window.draw(shape);
        std::cout << pos.x;
    }

};

void tick(sf::RenderWindow& window, Particle particle) {
    window.clear();

    particle.render(window);

    window.display();
}



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

    sf::RenderWindow window(sf::VideoMode(1500, 900), "MiniPhySim", sf::Style::Default, settings);

    sf::Clock clock;

    Particle particle(20);


    while (window.isOpen())
    {

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


        tick(window , particle);

        sf::sleep(sf::seconds(0.5));


    }

    return 0;
}



What I thought happened is that the position value I’m incrementing is not the position of the instance object but I do not know how to access it. It works fine when I update the value from the the main function but I don’t want to because It’s obviously not a good idea.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your problem is here:

void tick(sf::RenderWindow& window, Particle particle) {

You’re passing the Particle object by-value, which means that the particle inside the tick() method-call is a copy of the original Particle object from main(), and any modifications made to the copy get discarded when tick() returns; they don’t affect the original object in main() at all.

You can get the behavior you want by passing particle by-reference instead:

void tick(sf::RenderWindow& window, Particle & particle) {
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading