### Balls collision simulation - problem

Hi ! :) I'm new to C++ and need to somehow manage balls collision simulation for
my classes. If there was similar topic then I'm sorry I didnt find it.

So here is my code and what I find problematic is the void Runrandom function
in Universe class. Im really struggling to somehow combine balls movement, class plotter and collision detection. Can someone help me? Please :)

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280`` `````` #include #include #include #include #include using namespace std; class Ball { double mass=1, radius=0.2; public: double Getmass(){ return mass;} double Getradius(){ return radius;} double vx, vy, vz; double posx, posy, posz; Ball(){} void Randomize_parameters(); void set_starting_parameters(double, double, double, double, double, double); void Print(); double Distance(const Ball &B); }; void Ball::Print() { cout<<" "< index; Movement(Ball A): a(A){} bool collision=false; //vector vector_x, vector_y, vector_z; void MoveBall(Ball &a) { a.posx+=a.vx*interval; a.posy+=a.vy*interval; a.posz+=a.vz*interval; //vector_x.push_back(a.posx); //vector_y.push_back(a.posy); //vector_z.push_back(a.posz); if(a.posx>=box && a.vx>0){a.vx*=-1.;} if(a.posy>=box && a.vy>0){a.vy*=-1.;} if(a.posz>=box && a.vz>0){a.vz*=-1.;} if(a.posx<=0 && a.vx<0){a.vx*=-1.;} if(a.posy<=0 && a.vy<0){a.vy*=-1.;} if(a.posz<=0 && a.vz<0){a.vz*=-1.;} } void Collision(const Movement &A); void check() { for(int i=0;i0) { index.push_back(i); collision=true; } else if(a.vx>0 && A.a.vx<0) { index.push_back(i); collision=true; } else { collision=false;} //cout<<"Collision"< Balls; int N; Universe(vector balls, int n): Balls(balls), N(n){} vector moves; vector now_x, now_y; void Runrandom(vector &Balls, int &N) { for(int i=0;i<=N-1;i++) { Balls.at(i).Randomize_parameters(); //Balls.at(i).Print(); Movement M(Balls.at(i)); moves.push_back(M); } for(int k=1;k<=10;k++) { for(int i=0;i<=N-1;i++) { for(int j=0;j<=N-1;j++) { if(j!=i) {moves.at(i).Collision(moves.at(j));} if(moves.at(i).collision==true) { cout<<"Collision"< now_x2, now_y2; for(int k=1;k<=10;k+=1) { now_x2.push_back(now_x.at(k)); now_y2.push_back(now_y.at(k)); } } void Runtest() { Ball A, B; A.set_starting_parameters(0,5,0,1,0,0); B.set_starting_parameters(10,5,0,-1,0,0); vector vec_xa, vec_ya, vec_xb, vec_yb; for(double i=1;i<=40;i+=0.5) { Movement MA(A); Movement MB(B); MA.Collision(MB); if(MA.collision==true) { cout<<"Collision"< balls; for(int i=1;i<=n;i++) { Ball A; balls.push_back(A); } Universe U(balls,n); //U.Runrandom(balls,n); U.Runtest(); return 0; }``````
Last edited on
Hello yumi518,

After all that code you left out the header file "matplotlibcpp.h". With out it the code is full of errors and I can not test it.

Please post that file so the program can compile and run.

Andy
Hello Handy Andy,

Thank you for your reply. Unfortunately the header file "matplotlibcpp.h" is already installed on my PC and I cant find it so I just removed all the elements that need it and I will add them later. Now it should run smoothly :) When you run U.Runtest(); in the main() the balls are clearly colliding and changing directions but I cant get the same result for U.Runrandom(balls,n) :(

The idea of the simulation is to make an animation of n balls in a box using python. The balls are supposed to bounce from the walls and from each other.
Last edited on
@yumi518,
your code doesn't compile. Click the button to the right and try it in cpp.sh.

Have a look at it in your post above. It is not very intelligible. Please go back and fix that. I strongly suggest that you use spaces rather than tabs, as the latter often appears different in different environments.

Please remove all unnecessary stuff and make sure that the indentation shows correctly.

"It doesn't give the same result" is not a helpful description of the problem as (a) it doesn't run as it stands and (b) you are dealing with random numbers.
Hi Yumi518!

Try to use one of two 2D collision detection.
The first is based on rectangle shape bounding boxes:
 ``123456`` ``````if (ball1.x < ball2.x + ball2.width && ball1.x + ball1.width > ball2.x && ball1.y < ball2.y + ball2.height && ball1.height + ball1.y > ball2.y) { // collision detected! }``````

and the second is the circle collision: this algorithm works by taking the centre points of the two circles and ensuring the distance between the centre points are less than the two radii added together.
Just take a ball (ball1) and test the collision with all other balls with a FOR loop (it gives the values of ball2).
Hi Tomi!

Yes, it worked perfectly! Thank you so much. I didn't think I will ever get it to work properly :)
ASIDE

If you are going on to extend your program, you should consider refactoring it by converting associated variables into classes. In particular:

`double vx, vy, vz;` --> `Velocity v;`

`double posx, posy, posz;` --> `Position pos;`

And get the classes Velocity and Position to do their own work!

Andy
^^ in 3-d programs you typically need an XYZHPR for objects (3-d coordinate and 3-d orientation angles), as well as as velocity. What you are seeing here is that a ball does not really have a front/back/left/right that is noteworthy so you can ignore the HPR, but if it were a car, it could be important that it was moving to the left but the front of the car is to the right (its going in reverse) or that its moving to the left and facing to the right (its sliding down an icy hill?) and so on.
Hi,

You could also add to the modelling by adding more realistic physics like momentum, friction and energy. But this adds a few extra functions to affect energy etc.
Registered users can post here. Sign in or register to post.