Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Simple 2D collisions  (Read 5145 times)
0 Members and 1 Guest are viewing this topic.
Offline 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!
Offline 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

Offline _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...  Clueless

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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
Offline ReBirth
« Reply #4 - Posted 2012-03-07 12:49:39 »

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

Offline 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!  Grin
Offline _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?Smiley 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!  Cheesy

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

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

Offline doby162

Senior Newbie





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

Lol, I'm working on it! Cheesy stuff keeps coming up, but I'm making steady progress Shocked)
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline _Al3x

Senior Devvie


Medals: 7


Indie Games FTW!


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

Thanks for sharing Smiley!

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (32 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (58 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!