Java-Gaming.org Hi !
 Featured games (91) games approved by the League of Dukes Games in Showcase (757) Games in Android Showcase (229) games submitted by our members Games in WIP (844) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Simple 2D collisions  (Read 7874 times) 0 Members and 1 Guest are viewing this topic.
doby162

Senior Newbie

 « Posted 2012-03-05 21:42:07 »

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!
ReBirth
 « Reply #1 - Posted 2012-03-06 14:41:28 »

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

_Al3x

Senior Devvie

Medals: 7

Indie Games FTW!

 « Reply #2 - Posted 2012-03-06 20:00:22 »

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

doby162

Senior Newbie

 « Reply #3 - Posted 2012-03-07 05:11:24 »

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
ReBirth
 « Reply #4 - Posted 2012-03-07 12:49:39 »

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

doby162

Senior Newbie

 « Reply #5 - Posted 2012-03-07 13:38:53 »

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!
_Al3x

Senior Devvie

Medals: 7

Indie Games FTW!

 « Reply #6 - Posted 2012-03-11 19:46:48 »

Would you be so kind to share your progress AND your code? 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!

doby162

Senior Newbie

 « Reply #7 - Posted 2012-03-17 21:42:48 »

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

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!
ReBirth
 « Reply #8 - Posted 2012-03-18 03:13:13 »

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

doby162

Senior Newbie

 « Reply #9 - Posted 2012-03-18 04:03:48 »

Lol, I'm working on it! stuff keeps coming up, but I'm making steady progress )
_Al3x

Senior Devvie

Medals: 7

Indie Games FTW!

 « Reply #10 - Posted 2012-04-06 19:54:19 »

Thanks for sharing !

Pages: [1]
 ignore  |  Print

 EgonOlsen (79 views) 2018-06-10 19:43:48 EgonOlsen (59 views) 2018-06-10 19:43:44 EgonOlsen (78 views) 2018-06-10 19:43:20 DesertCoockie (261 views) 2018-05-13 18:23:11 nelsongames (160 views) 2018-04-24 18:15:36 nelsongames (158 views) 2018-04-24 18:14:32 ivj94 (901 views) 2018-03-24 14:47:39 ivj94 (162 views) 2018-03-24 14:46:31 ivj94 (813 views) 2018-03-24 14:43:53 Solater (177 views) 2018-03-17 05:04:08
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org