Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [GDK] O(n^2) n-body gravitational simulation

Author
Message
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 21st Mar 2010 04:15 Edited at: 21st Mar 2010 04:46
Basically, I'm working on a large-scale capable n-body gravitational simulation. What I've worked up is more a cool bit of collision code along with a demonstration of verlet integration, wrapped up with gravitational forces.

What is inefficient about this is that I have a for loop that is basically:

for a = 1 to n
for b = 1 to n
- dostuff
next
next

I'm going to streamline this process and make it faster by using a barnes-hutt algorithm (which implements a quadtree). Here's the source code:

(this is my second project in c++, and my first time using a lot of features of c++. There might be errors in something-or-other that I did, but it's only ~350 lines.)

(also note it uses cloggy's d3d dll, because built in circle-and-stuff commands SUCK and don't work on vista)

left click to add a regular object
right click to add a nonmovable object with 0 mass (acts as a wall)
middle click to create a draggable collision object (0 mass)
spacebar to pause the simulation
control to zero all velocities
hhhhhh hhhhhh
hhhhh hhhhh
hhhh hhhh

Watch out while holding the space bar!!!!! since it's just a quick once-over I don't check for divide by zero! If you create two objects in the exact same spot while paused, then their positions will be undefined! If their positions are undefined, and you check the distance against that point, then it will return undefined, and every single object will have a position of undefined!

Screenie:


[edit]
and I'm posting this here because it's too small to be announced
[edit2]
right, forgot to upload the exe, here it is:
[href]http://www.filefactory.com/file/b0c214a/n/GDKtest.exe [/href]


Attachments

Login to view attachments
Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 21st Mar 2010 04:35
Looks cool. Maybe i'll try to read it someday. I'm not really fluent in C++ yet.

When I'm doing forces simulations, I bin the objects into a grid, and only do interactions within some cutoff range. This is far from perfect though. Maybe what you could do is, for stuff in grid squares beyond the cutoff range, treat the mass inside grid squares as all concentrated at the centre, or calculate the centre of mass for it, or something similar to that. I imagine if you're quoting names of authors then they'll have thought about all this stuff. Will be interesting to see what kind of speed increases you can get.

I don't have GDK, so maybe you could upload an exe for people to try.

Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 21st Mar 2010 04:48
uploaded it, I'm also posting a video on youtube, might take a while if my computer is retarded.


Dr Tank
15
Years of Service
User Offline
Joined: 1st Apr 2009
Location: Southampton, UK
Posted: 24th Mar 2010 18:55 Edited at: 24th Mar 2010 18:56
Works well. I made a collision system for round objects, but when there were loads pushing up against eachother, it didn't work so well. Your system doesn't seem to suffer from this nearly as much. Hopefully i can learn something from your code. Thanks.

For about 200 objects, i get about 20fps. You can do better than order n^2, dude. Put in a grid system or something!

Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 26th Mar 2010 03:57
I really want to work on this but... I'll be in japan for a week, and ma leaving 12 hours from now. Unless I program a glitch-free quadtree (what I'm planning to do) after I wake up before I leave... I won't be able to work on this for a week.
Also, this is the relevant code for the collision system:


I think after I finish with this, I'll work on a 3d version. Should be awesome, and still really speedy


Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 12th Jul 2010 11:42
took a look at this again and... Have you noticed how you can't really get a cluster to orbit a cluster? That's cuz clusters slow down after a while, and they don't obey conservation of momentum X_X

Fix'n right now. I think I gots me a good equation to determine velocity but... it's the AMs and I have to be up in 5 hrs.

If anyone wants to know, where P1,P2 are positions, V1,V2 are velocities, M1,M2 are masses, u is coefficient of restitution, and X is the normalized vector of P2-P1, or X=||P2-P1||, also . is vector dot product and * is scalar multiplication
V1'=V1-(V1.X)*X+((u*M1*(V1.X-V2.X)+M1*V1.X+M2*V2.X)/(M1+M2))*X
That should determine the new velocity of the verlet circle while conserving momentum.


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 13th Jul 2010 23:38 Edited at: 14th Jul 2010 05:54
Well... that vector method looks too computationally intense, and with further thought, I think the whole idea of having rigid circles colliding is bad.

Instead, I'm going to treat the particles not as circles, but as point "molecules" - attracting each other, until they get close, and then sharply repelling each other.

Normally, the equation for force over distance is newton's law of gravitation:

whose graph looks like this:

lets define F1(d) to be that function, in other words
F1(d)=G*m1*m2/d^2

Now... I'm not trying to make a true to life simulation, so I don't really need true to life equations. IMHO as long as my simulation obeys newtons laws, it's accurate enough. So, now we want an equation for a repelling force (in this case, a negative force repels, a positive force attracts), with an equally sharp drop off as F1(d) as the distance approaches 0. Basically:
F2(d)=C-M1*M2/d^2
where C is a constant. Now, this equation should also have the property that when d=r1+r2 (IE the circles are just touching), they are at an equilibrium, and the force between them is 0, IE
F2(r1+r2)=0
solving for C gives
C=M1*M2/(r1+r2)^2
so
F1(d)=G*m1*m2/d^2
F2(d)=M1*M2(1/(r1+r2)^2-1/d^2)
the graph of those two equations looks like this:

The lowest of the two values at any point seems to be a good graph, so: F(d)=min(F1(d),F2(d))=min(G*m1*m2/d^2,M1*M2(1/(r1+r2)^2-1/d^2))
graphed:


With two large radius/low density particles, they would probably bounce off each other repeatedly. With two extremely small radius/very high density particles, it would probably be vibrating or haywire collisions. Both of these situations are visually unappealing (circles slowly bouncing or going crazy, intruding into each others' boundaries), but I think with hundreds or thousands of such particles, better structures will form. There is also no need for collision detection, because everything works itself out. If structures don't form, and everything just repels everything else too much, velocity dampening could be added in the direction of the collision.

In vector notation, where P1 and P2 are vectors of the position of one such particle and another:
Force on particle 1 += (P1-P2)/( |P1-P2|)*F( |P1-P2|)
Force on particle 2 -= (P1-P2)/( |P1-P2|)*F( |P1-P2|)

I hafta go right now, but hopefully I'll have all this implemented soon.

[edit]
o rite, all graphs are force over separating distance of two particles.


Is't life, I ask, is't even prudence, to bore thyself and bore thy students?
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 14th Jul 2010 06:59 Edited at: 14th Jul 2010 10:39
Even better graph! Its pretty much as easy to make that function a continuous curve:
F(d)=G*m1*m2/d^2*(1-(r1+r2)^2/d^2)
Try it out! As the radii approach zero, or the larger x gets, the closer the function is to G*m1*m2/d^2


[edit]
well neither of those worked at all, and everything goes haywire when I try to implement that collision conservation of momentum stuff.




Is't life, I ask, is't even prudence, to bore thyself and bore thy students?

Login to post a reply

Server time is: 2024-11-21 21:13:44
Your offset time is: 2024-11-21 21:13:44