Hello Everyone!
I know this topic can get boring, but there's just another guy, who can't handle these ball to ball collisions and he's pretty desperate in finding help. I've written an app in Java which was suposed to simulate ellastic collision, but I've stumped upon a problem. I tried to read a lot about collisions and so on and I know, that I need to separate two balls during collision, but that's where I get lost. I think I somehow understand what's needed to make this simulation look realistic, but I just can't find a good way to implement it. I have problems mostly with my separate method (I'll just add my source code, so everyone who's willing to help me, can see it from inside). In this method I check if two balls are colliding, I check if distance between them is smaller than their radius sum. If it is, than there is a collision and in addition they are overlapping (do I get it right?). After this collision check I try to separate them in such way:
pos1 -= vel1 * dt
pos2 -= vel2 * dt.
And I don't know why, but it doesn't seem to behave as it should. The rest of you can see it running, I've uploaded it here:
http://www.turboupload.com/download/t7LUPWf7Kj84/Bounclets.jar
The source code is included in this jar file.
And for people who have seen my app running - yes I know there is this ball, which is beeing omitted in calculations, but I'm not doing anything with it until I'm done with my separation.
All your help is so much welcome! Big thanks in advance!
PS. If This post is a dumb one, violates the rules, or somethin, then just please delete it and accept my apology.
Regards!
[Edited by - Blessed on June 11, 2008 11:02:31 AM]

**0**

# [SOLVED]Another Ball to Ball collision written in Java

Started by Blessed, Jun 10 2008 01:17 PM

3 replies to this topic

###
#2
Crossbones+ - Reputation: **2193**

Posted 10 June 2008 - 09:30 PM

for the balls to stop intersecting, you need to place their centres at distance 'r1 + r2' from each other.

this can be done by pushing them apart using the 'mtd' vector (minimum translation distance vector).

then you can push the spheres by the mtd to separate them.

after that, the two centres pos1 and pos2 will be exactly at disatnce r1+r2 from each other. The balls stop intersecting.

then to make them bounce, you need some collision response.

more code here. This is using the ratio of masses to distribute the collision impulse.

Note that the impulse equation is based on the conservation of momentum, it is not some random quantity.

[Edited by - oliii on August 24, 2008 7:30:42 AM]

this can be done by pushing them apart using the 'mtd' vector (minimum translation distance vector).

Vector delta = (pos1 - pos2); // delta vector

float d = delta.length(); // delta vector length

if(d > (r1 + r2)) return false; // no intersection

mtd = delta * ((r1 + r2)-d)/d; // the minimum translation distance vector

then you can push the spheres by the mtd to separate them.

pos1 += mtd * 0.5f;

pos2 -= mtd * 0.5f;

after that, the two centres pos1 and pos2 will be exactly at disatnce r1+r2 from each other. The balls stop intersecting.

then to make them bounce, you need some collision response.

Vector n = mtd / mtd.length(); // normal of collision

Vector v = (vel1 - vel2); // relative velocity

float vn = v.dotProduct(n); // impact speed

if(vn > 0.0f) return false; // ball already moving away from each other. stop response.

float cor = 0.8f; // coefficient of restitution (range [0, 1].

float i = -((1.0 * cor) * vn) / 2.0f; // impulse quantity

Vector impulse = i * n; // impulse vector

vel1 += impulse; // apply impulse to change ball 1 monentum

vel2 -= impulse; // apply impulse to change ball 2 monentum

more code here. This is using the ratio of masses to distribute the collision impulse.

Note that the impulse equation is based on the conservation of momentum, it is not some random quantity.

[Edited by - oliii on August 24, 2008 7:30:42 AM]

###
#3
Members - Reputation: **131**

Posted 10 June 2008 - 10:12 PM

Hey!

Thanks for your really good help and answer. Now the balls dont overlapp each other and this problem is gone. But the method you gave me for collision response acts like it didn't conserve momentum. And that's not what I'm trying to achieve. After some time the balls just slow down, and it looks like kinetic energy would get lost somewhere. Is it because of the coefficient of restitution? If i set it to zero then the balls don't respond to the collision. But the bigger it is the faster the balls are loosing on speed. Oh and in addition i was rather trying to make the answer being mass dependent. Once again thank you, coz you were a great help for me! If you would just give me an answer to the second question then i would be grateful.

This is my method which i was using before you answered me http://rafb.net/p/Em6Czb97.html

EDIT: I have worked it out! Now my problem is solved. Than you for your precious help.

[Edited by - Blessed on June 11, 2008 11:12:11 AM]

Thanks for your really good help and answer. Now the balls dont overlapp each other and this problem is gone. But the method you gave me for collision response acts like it didn't conserve momentum. And that's not what I'm trying to achieve. After some time the balls just slow down, and it looks like kinetic energy would get lost somewhere. Is it because of the coefficient of restitution? If i set it to zero then the balls don't respond to the collision. But the bigger it is the faster the balls are loosing on speed. Oh and in addition i was rather trying to make the answer being mass dependent. Once again thank you, coz you were a great help for me! If you would just give me an answer to the second question then i would be grateful.

This is my method which i was using before you answered me http://rafb.net/p/Em6Czb97.html

EDIT: I have worked it out! Now my problem is solved. Than you for your precious help.

[Edited by - Blessed on June 11, 2008 11:12:11 AM]

###
#4
Crossbones+ - Reputation: **2193**

Posted 11 June 2008 - 07:17 AM

you have to be careful with the conservation of momentum. The integration is a basic euler integration, and it works on solving intersections. So there will be a loss of energy even with cor = 1.0f.

If you like, to me it looks like the potential energy is lost when you separate the balls from each other. The kinetic energy is conserved via the collision impulse calculations, but euler is a bit innacurate and this will also loose a bit of kinetic energy over a long period of time, as the motion is discontinuous when you take into account collisions.

This is just a 20-line-of-code example. To be truely accurate, you'd have to delve deeper into physics, but in an ideal world, a cor = 1.0f should conserve energy.

A first step would be to use a swept collision test (use ball velocities to detect the time of collision). And a better integration (RK4) would improve the energy conservation.

If you like, to me it looks like the potential energy is lost when you separate the balls from each other. The kinetic energy is conserved via the collision impulse calculations, but euler is a bit innacurate and this will also loose a bit of kinetic energy over a long period of time, as the motion is discontinuous when you take into account collisions.

This is just a 20-line-of-code example. To be truely accurate, you'd have to delve deeper into physics, but in an ideal world, a cor = 1.0f should conserve energy.

A first step would be to use a swept collision test (use ball velocities to detect the time of collision). And a better integration (RK4) would improve the energy conservation.