Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (565) Games in Android Showcase (151) games submitted by our members Games in WIP (606) 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 5258 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

You cannot reply to this message, because it is very, very old.

 theagentd (11 views) 2015-03-27 23:08:20 wxwsk8er (52 views) 2015-03-20 15:39:46 Fairy Tailz (46 views) 2015-03-15 21:52:20 Olo (28 views) 2015-03-13 17:51:59 Olo (30 views) 2015-03-13 17:50:51 Olo (38 views) 2015-03-13 17:50:16 Olo (44 views) 2015-03-13 17:47:07 ClaasJG (57 views) 2015-03-10 11:36:42 ClaasJG (41 views) 2015-03-10 11:33:01 Pippogeek (47 views) 2015-03-05 14:36:23
 BurntPizza 21x LiquidNitrogen 21x basil_ 20x KevinWorkman 18x EgonOlsen 17x theagentd 16x Roquen 16x 65K 11x princec 10x Varkas 10x Riven 10x wessles 10x SHC 8x Ashedragon 8x phu004 8x lcass 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 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