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

C++ Struct Members not Updating in Funtion

Firstly, while not new to programming, I am very new to C++, so please bear with me.
I am using the Raylib library to attempt making a particle system for a game.

This consists of a struct with a few private members and public functions:

struct Particle {

Particle() {
    mPosVector = {(float)GetMouseX(), (float)GetMouseY()};
    mVelVector = {(float)GetRandomValue(15, 70)/100, (float)GetRandomValue(15, 70)/100};
    mSize = GetRandomValue(5, 15);
}

void update(double deltaTime) {
    mPosVector.x += mVelVector.x;
    mPosVector.y += mVelVector.y;
}

void draw() {
    DrawRectangleV(mPosVector, {(float)mSize, (float)mSize}, WHITE);
}

private:
    Vector2 mPosVector;
    Vector2 mVelVector;
    int mSize;
};

The Vector2 type is defined by Raylib:

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

struct Vector2 {
    float x;
    float y;
};

In my main function I have an std::vector storing Particles. A particle gets added when the left mouse button is pressed. I loop through the Particles vector twice, once for updating position based on velocity and once for drawing. I was originally doing these both in one loop, but was still getting the problem that I will get onto, so tried it this way.
This is the current code:

std::vector<Particle> particles = {Particle()};

while (!WindowShouldClose()) {

    deltaTime = GetFrameTime();

    if (IsMouseButtonDown(0)) {
        particles.push_back(Particle());
    }

    for (Particle part : particles) {
        part.update(deltaTime);
    }

    BeginDrawing();

        ClearBackground(BLACK);

        DrawFPS(10, 10);

        DrawText((numToString<double>(deltaTime*1000).substr(0, 5) + "ms").c_str(), 10, 40, 20, WHITE);

        for (Particle part : particles) {
            part.draw();
        }

    EndDrawing();

So, my problem: While particles are being instantiated as expected while pressing the left mouse button and being drawn, for some reason their positions are not being updated by their velocity. I have tried printing debug information to the console, such as the velocity, and it is as expected, but for some unknown reason to me (probably just me being stupid) their positions aren’t being updated.

Any help would be greatly appreciated.

>Solution :

   for (Particle part : particles) {
        part.update(deltaTime);
    }

this is making a copy of each entry , you need

   for (Particle &part : particles) {
        part.update(deltaTime);
    }

to get a reference to the object in the vector to update it in place

To understand, think that the ranged for is just short hand for this

for(int i = 0; i < particles.size(); i++)
{
     // this line copies the value
     particle p = particles[i];
}

whereas the one with & in it does

for(int i = 0; i < particles.size9); i++)
{
     // this line gets a reference to the ith entry
     particle &p = particles[i];
}

Its nothing special to do with the ranged for loop.

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