# NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

## Information about this web site => Request for physics Simulations => Topic started by: PL_kolek on June 13, 2010, 05:42:29 am

 Title: 2D collision with multiple balls at the same time Post by: PL_kolek on June 13, 2010, 05:42:29 am Hi!I come from Poland, but I found this website (I don't remember how), and I used the applet with explanation about 2D collision to understand this situation. The problem is, I cannot figure what happens and how to calculate the velocities of more colliding balls when they hit each other at the same time. I could predict, what would happen, but is not exact calculation, but my guess. Moreover, google didn't help me in finding information about that. I am trying to write my own simple billiard game, and the most difficult task to understand is the physics.So could you please explain me (or at least post some links to such explanation) what happens when many balls collide?Every ball has its mass (m), velocity (Vx, Vy), coordinates (x,y). The radius is constant, identical for every ball. Example for such situation:3 balls with radius 10:-x=0, y=100, Vx=20, Vy=0, m=1-x=100, y=90, Vx=0, Vy=0, m=1-x=100, y=110, Vx=0, Vy=0, m=1With regardsPL_kolek Title: Re: 2D collision with multiple balls at the same time Post by: Fu-Kwun Hwang on June 13, 2010, 09:34:38 am The way to solve it depends on what is the purpose of your simulation.For the case you provided above:Yes. in theory (mathematically), the first ball will collide with the other two ball at the same time.If you really need such precission: The above problem can be solve analytically.However, in real case, there is always a small error. The first ball wil hit one of them first, then collide with the second one. You can check out Collision 2D (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=120.0) to find out how to calculate collision between two balls.The same technique was used to calculate collision between many particles: Browian motion. (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=178.0) or particle model of liquid in a container (microscopic) (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=294.0)If you need a better time precission, EJS has build in EVENT GUI interface to help you.Select one of the experiment (other than the default: Random)Click Pause to hold the simulation, then drag one of the ball a very small amount and click play again.You can modify value of g to add gravity or change k for inelastic collision.You will find it really calculate all the collisions between particles. Title: Re: 2D collision with multiple balls at the same time Post by: PL_kolek on June 14, 2010, 05:46:57 am I have read on this site about 2D collision with 2 balls and I understand taht, thanks to you. I had the same thoughts about the problem - calculating every collision differently, one after another. So I must have made mistake in my code or my algorithm, because it doesn't work as it should.Let me analyze the situation mentioned before (2 sticked balls, and a third one hitting them in the middle, at the same time):-I detect collision with one of the balls (let's say the upper one)-I have to move the moving ball back not to overlap staying one-I calculate velocities-I move the time forward, putting these two balls in new positionsThe effect is - Upper ball moved, moving ball bounced back, but there is no collision now with the last ball, and there won't be, as balls go further away from it. Whole my simulation doesn't work, because it gives wrong results. Where is the mistake? If it is not clear enough, I can upload pictures which will show what I mean.Thank you for your reply, now I know I was on a good way! Title: Re: 2D collision with multiple balls at the same time Post by: Fu-Kwun Hwang on June 14, 2010, 08:58:08 am Code:The effect is - Upper ball moved, moving ball bounced backYes. There is something wrong with your code.If the incoming ball just collide with the upper one, it should move into the direction to collide with the second ball. (The incoming ball should not bounced back???).In real life,usually, the incoming ball will hit one of the ball first then collide with the second one.If you want to simulate the incomng ball collide with the other two balls at the same time:Assume the incoming ball with initial valocity $v_0$,Since the size of the balls are the same, and they all have the same mass.If the incoming ball hit the other two balls at the same time, the interactive force between incoming ball and any one of the ball are in the direction connect two center of the ball.So the direction is 45 degree or -45 degree. It means that the force is the same in x direction and in y direction. So the momentum change in x/y diretion are the same.So the velocity for the other two balls just after the collision are $\vec{v_a}=v \hat{x} + v\hat{y}$ and    $\vec{v_b}=v \hat{x} - v\hat{y}$ and assume the velocity for the incoming ball after the collision is $v'$Conservation of momentum: $v_0= 2 v+v'$Conservation of energy: $\tfrac{1}{2}mv_0^2= \tfrac{1}{2}m v_a^2+\tfrac{1}{2}m v_b^2+\tfrac{1}{2}m v'^2=2mv^2+\tfrac{1}{2}m v'^2$which imply $v_0^2=4 v^2+v'^2$So the result are: $v'=0, v_0=2v$which mean that the incoming ball with initial velocity $v_0$ will be stopped ($v'=0$) just after the collision, and the other two balls will move with velocity $\vec{v_a}=\tfrac{1}{2}v_0 \hat{x} + \tfrac{1}{2} v_0\hat{y}$ and     $\vec{v_b}=\tfrac{1}{2}v_0 \hat{x} - \tfrac{1}{2}v_0 \hat{y}$However, this kind of situation normally will not happened in real life. Title: Re: 2D collision with multiple balls at the same time Post by: PL_kolek on June 15, 2010, 05:12:02 am I adapted your 2D collision applet to check what happens, when just two balls collide like in the case I provided (one of the staying balls missing). Now I see, that my balls act differently (does it sound weird?). I must have made a mistake in my calculations (for better understanding I made my own calculations to get your result, and in one place I didn't get your way in calculating the velocities), or putting them into the code. So I will try to fix it (In the worst case I will just copy your code, am i allowed to?), and if it still give no result, I will come back with more questions.Thank you for your efforts to help me. Now I see I can't calculate it this way (too many possible situation without just one general algorithm), but simulating single collision should work. It is amazing, that I couldn't find help anywhere, and here I got answers quickly, with clear explanation to my problem. After learning a bit physics it gets more interesting than it looked. Thank you very much!PL_kolek Title: Re: 2D collision with multiple balls at the same time Post by: Some1 on July 25, 2012, 10:04:22 am Hello!My name is Alex and I am from Romania. Firstly I cannot thank you enough for providing these examples and explanations. It has helped me a lot.I am working on a 2D modable pool game and the 2D collision program was a lifesaver. It is better than any of my previous attempts and although I was never very good at mathematics and especially physics, I finally understand things in a bit more depth.I have managed to create a little test frame for multiple ball collisions using a 0.1 of a pixel iteration for each ball(int)Math.ceil((10000/UPDATE_RATE)/0.1); //number of tests per frame with an iteration that is ALWAYS below 0.1 of a pixeland dividing the speeds accordingly to match the iteration.This method actually works, but I am realizing that it is extremely inefficient and there is always a loss(which I may be able to accept though) since the balls have to always overlap(worst case possible 0.2 in between two balls) and then pull back r1+r2+0.01 across the segment that connects their centers so that they do not collide anymore when it happens. (0.01 is something to ignore the double variable imprecision). At more than 30-40 balls lag can inevitably be felt.I haven't used EJS before. I will try this program. Also is there any way you could kindly provide the .java code file for the .jar file provided? It would help me immensely and I would like to ask if it is ok to use fragments of code from your applets into personal projects. I will give credit if necessary once the application is done!Thank you again! Title: Re: 2D collision with multiple balls at the same time Post by: Fu-Kwun Hwang on July 25, 2012, 07:06:27 pm You are welcomed to check out Browian motion (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=178.0).You will find several hundreds particles collide with each other without any problem.It was created with ejs. And it is much easier to create simulation with EJS. You are welcomed to download the jar file, run it and click right mouse button to open it in EJS. (You need to install EJS)Then , you will find out all the code in EJS environment.Good luck to your work. You are welcomed to share your code when you finish your work. ;-) Title: Re: 2D collision with multiple balls at the same time Post by: Holu Callouse on November 26, 2012, 02:30:52 pm Nice job, itâ€™s a great post. The info is good to know! Title: Re: 2D collision with multiple balls at the same time Post by: ivwiwba on January 19, 2013, 10:10:03 am This is all very new to me and this article really opened my eyes.Thanks for sharing with us your wisdom.-*- Title: Re: 2D collision with multiple balls at the same time Post by: sajidtoor on February 08, 2013, 05:01:34 pm This method actually works, but I am realizing that it is extremely inefficient and there is always a loss(which I may be able to accept though) since the balls have to always overlap(worst case possible 0.2 in between two balls) and then pull back r1+r2+0.01 across the segment that connects their centers so that they do not collide anymore when it happens. (0.01 is something to ignore the double variable imprecision). At more than 30-40 balls lag can inevitably be felt.-*- Title: Re: 2D collision with multiple balls at the same time Post by: Fu-Kwun Hwang on February 11, 2013, 01:04:50 pm You might want to check out Browian motion (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=178.0) where several hundreds particles are collide with each other with any problem. Title: Re: 2D collision with multiple balls at the same time Post by: tomoisgood0444 on April 19, 2013, 03:27:17 pm very good-*-