Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (593)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (646)
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 5449 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.

Riven (11 views)
2015-07-03 11:22:16

deepthought (41 views)
2015-06-30 15:39:44

deepthought (45 views)
2015-06-30 15:39:09

deepthought (55 views)
2015-06-30 15:36:52

Za\'Anzabar (24 views)
2015-06-29 05:44:54

TritonDreyja (41 views)
2015-06-24 17:10:40

CopyableCougar4 (40 views)
2015-06-23 00:34:45

BurntPizza (44 views)
2015-06-21 20:36:46

cookiecompiler (86 views)
2015-06-11 15:42:53

cookiecompiler (49 views)
2015-06-11 15:41:14
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 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‑
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!