Simple 2D collisions

Hello all! I’m trying to write a java program that, as simply as possible, simulates Brownian motion. In essence, there will be a bunch of circles with different velocity and mass, and I need to know what happens when they hit each other. Detecting the collision is simple, since they are all circles and you can just use the distance formula to see if they’re touching, but as for exactly what they do once they touch, I’m somewhat lost.

I know this has been done before, but I can’t find any code for it. Also, I’d like to avoid using a clunky physics library, as there could be hundreds or circles at once and I’d like to keep the program running smoothly.

Thank you!

I think mostly pc can handle hundreds physics. I think.

You can calc momentum. When a circle collides to other, call the difference of both’s momentum and apply it to circle with less value by divide momentum’s delta and its mass. Correct me, my physics books already dusty.

P = mass * velocity

Sure, but you have to keep in mind directional vectors and stuffs, to see where are they going to go one they collide… Another thing to keep in mind it´s the window border, since it´s plane, the bounce will be different from circles… :clue:

Right, it’s like Al3x said, I need to get the direction aproximantly right. It’s for a game, so as long as the player thinks it’s realistic it’s good enough :oP

just follow the vector of circle that has bigger mass on hit.

Hmmm… that might actually work. I would put in an auxiliary method to mess up the vector of the larger particle a little bit and make it look more believable, but I think that could work… I’ll give it a try after school! Thanks! ;D

Would you be so kind to share your progress AND your code?:slight_smile: I’m very interested in how you solve this problem, since it always bugged me how to calculate the movement direction of a particle when it bounce.

Thanks! :smiley:

Hey! I’m glad I decided to look at this post again, i wasn’t expecting more comments! xD This site never emails me :stuck_out_tongue:

Alright! So finally what I ended up doing is bugging a family friend who also happens to be a mathematics professor at a local university to help me. He did the equations while I translated it into code, and it turned out to be relatively simple once you understand it.

If ball B has a speedX and a speedY, and ball B also has these, then when a collision occurs (they’re circles, use the distance formula) you simple set A’s speed variables equal to B’s speeds, and then set B’s speeds to what A’s were before you changed them. This is assuming identical mass, if one is twice as heavy you just have to use multiplication.
A.mass = 1;
B.mass = 2;
A.speed = B.speed*2;
B.speed = A.PreviousSpeed/2;

This is a mathematically perfect algorithm assuming no friction, spin, damping, speed averaging, or invading aliens. It’s more accurate for atoms than superBalls cause more stuff gets in the way when things are bigger. Hope that comes in handy!

Glad that you already understand. Now finish the game and post it! ;D

Lol, I’m working on it! :smiley: stuff keeps coming up, but I’m making steady progress :o)

Thanks for sharing :)!