Reputation: 43
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.
Upvotes: 0
Views: 720
Reputation: 118292
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.
Upvotes: 2