0

I have a program designed to simulate a ball bouncing in C++ and save the position, and velocity of it. I have a ball class to simulate many of them at a time and for some reason when I run the update function to update the ball, none of the variables update.

Ball class:

    class Ball
    {
    public:
        float x;
        float y;
        float xVel;
        float yVel;
        float gravity;
        float elasticity;
        float radius;
        float friction;
        float width = 200;
        float height = 200;
        void update() { //Only simulate gravity for testing purposes
            this->yVel += this->gravity;
            std::cout << this->yVel << this->gravity << std::endl;
        }
        void init()
        {
            this->x = randomf(0, width);
            this->y = randomf(0, height);
            this->xVel = randomf(-10, 10);
            this->yVel = randomf(-0.5, 0.5);
            this->gravity = randomf(-1, 1);
            this->elasticity = randomf(0.25, 1);
            this->radius = randomf(2.5, 50);
            this->friction = randomf(0.25, 1);
        }
    };

For some reason when I call update() none of the variables update, only producing the output

@"0.0241846 0.0655345\r\n"
@"0.0241846 0.0655345\r\n"
@"0.0241846 0.0655345\r\n"
@"0.0241846 0.0655345\r\n"
@"0.0241846 0.0655345\r\n"
@"0.0241846 0.0655345\r\n"
@"0.0241846 0.0655345\r\n"

How I'm calling update is I have a for() loop that goes for as many iterations as I want, currently 500, and update is inside of it. Another thing that tends to happen with this program, is that my random function

The for() loop:

for (int i = 0; i < iter; i++)
    {
        balls.clear(); //Clear array
        for (int j = 0; j < batch; j++) //Create new balls
        {
            Ball ball;
            ball.init();
            balls.push_back(ball);
        }
        for (int j = 0; j < simLen; j++) //For simulation length update all balls
        {
            for (int k = 0; k < batch; k++)
            {
                Ball ball = balls.at(k);
                ball.update();
                std::vector<float> temp; //Store ball info to be saved later
                temp.push_back(ball.x);
                temp.push_back(ball.y);
                temp.push_back(ball.xVel);
                temp.push_back(ball.yVel);
                if (j % 2 == 0) {
                    y.push_back(temp);
                } else {
                    temp.push_back(ball.gravity);
                    temp.push_back(ball.elasticity);
                    temp.push_back(ball.radius);
                    temp.push_back(ball.friction);
                    x.push_back(temp);
                }
                temp.clear();
            }
        }
        std::cout << "Simulation " << i + 1 << " out of " << iter << " finished, batch size " << batch << std::endl;
    }

Random number function:

    float randomf(float LO, float HI)
    {
        return LO + static_cast<float>(rand()) / (static_cast<float>(RAND_MAX / (HI - LO)));
    }

Generates the same values most of the time. I have no clue what on earth would cause this, help.

6
  • How I'm calling update is I have a for() loop that goes for as many iterations as I want, currently 500, and update is inside of it. -- Post the code, do not just describe it. Commented Oct 25, 2020 at 2:08
  • just did that, thanks Commented Oct 25, 2020 at 2:09
  • Ball ball = balls.at(k); -- You do know that ball is a copy, right? Don't you really want a reference, i.e. Ball& ball = balls.at(k);? Commented Oct 25, 2020 at 2:11
  • i didnt know that lol, was a java dev, thank you Commented Oct 25, 2020 at 2:14
  • "These are not the variables you're looking for" :-) Commented Oct 25, 2020 at 2:17

1 Answer 1

2
Ball ball = balls.at(k);

This retrieves one of the Ball objects from the vector, and creates a new object called ball that's a copy of it.

ball.update();

This calls update() on this ball object. Since it's a copy of the original object from the vector this, of course, does nothing whatsoever to the object in the vector.

std::vector::at returns a reference, so you simply need to make ball a reference to the object in the array:

Ball &ball = balls.at(k);

See your C++ textbook for a more complete description of what references are and how they work.

Sign up to request clarification or add additional context in comments.

3 Comments

oh my god thank you, im a java programmer usually so i didnt know this
Java programmers should never be allowed to roam the hallowed halls of C++ land, that's like letting Notepad users edit Linux code, or allowing cats in a dog shelter :-) Note the smiley, I don't need any grief over what I think is, despite what my wife usually says, a humorous comment.
If you review all the previous copies of this question that you posted it should now be quite clear to you why stackoverflow requires a minimal reproducible example. You thought that problem was with your update() function so you only showed that. That's not where the problem was. P.S. C++ is not Java. Objects in C++ work in fundamentally different ways than they do in Java. You will do yourself a big favor if you completely forget everything you know about Java while you learn C++, else you'll kept getting tripped up like that. This is not the only fundamental difference between Java and C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.