View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Blessed  Members

Posted 10 June 2008 - 01:17 PM

### #20BZEN  Members

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).

Vector delta = (pos1 - pos2);   // delta vectorfloat d = delta.length();       // delta vector lengthif(d > (r1 + r2)) return false; // no intersectionmtd = 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 collisionVector v  = (vel1 - vel2);     // relative velocityfloat vn = v.dotProduct(n);   // impact speedif(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 quantityVector impulse = i * n;               // impulse vectorvel1 += impulse; // apply impulse to change ball 1 monentumvel2 -= 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]

### #3Blessed  Members

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]

### #40BZEN  Members

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.