Jump to content
  • Advertisement
Sign in to follow this  
danvx6

Gravity problems

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am having trouble with this program that I wrote yesterday It is supposed to "simulate" the motion a masses acting on each other with gravity, but the particles move in seemingly random ways. i can't find out what is wrong with the code, can anyone see something wrong? sorry for the clutter and sloppy coding here ******************************************************************************* ******************************************************************************* REM $DYNAMIC RANDOMIZE TIMER KEY(1) ON ON KEY(1) GOSUB EndNow CLS SCREEN 13 LINE (0, 0)-(320, 210), 7, BF 'F = G*(q1*q2)/(d^2) 'G = 6.673 * 10^-11 Gravitational Constant NumMasses = 50 'Total number of masses - 1 (enter one less than the actual number desired) Msmall# = 5! * 10 ^ 35 G# = 6.673 * 10 ^ -11 t# = .000000000002# 'seconds per frame (Time Step) delay = 0 'Higher values = slower operation (so it doesnt run too fast) DIM q#(NumMasses, 7) '1 X position '2 Y position '3 Mass '4 X Force Total '5 Y Force Total '6 Old Velocity X '7 Old Velocity Y FOR SetMasses = 0 TO NumMasses q#(SetMasses, 3) = Msmall# q#(SetMasses, 1) = INT((RND * 320)) q#(SetMasses, 2) = INT((RND * 200)) NEXT SetMasses 'TEST MASSES ENTERED HERE 'q#(0, 1) = 190 '********************************** 'q#(0, 2) = 0 '********************************** 'q#(0, 3) = 7.34 * 10 ^ 22 'q#(1, 1) = 210 '********************************** 'q#(1, 2) = 0 '********************************** 'q#(1, 3) = 5.97 * 10 ^ 24 'Start Main Loop DO WHILE quit = 0 FOR set = 0 TO NumMasses 'Reset forces on all masses to 0 and draw masses q#(set, 4) = 0: q#(set, 5) = 0 PSET (q#(set, 1), q#(set, 2)), 4 NEXT set FOR freeze = 0 TO delay: NEXT freeze FOR i = 0 TO NumMasses 'Iterate through the masses j = 0 'Reset inner loop DO WHILE j < NumMasses + 1 'Iterate through all masses IF j = i THEN j = j + 1 'Skip one so mass doesn't affect itself IF NOT j = NumMasses + 1 THEN 'If the array element is there... m1x# = q#(i, 1): m1y# = q#(i, 2): m1# = q#(i, 3) m2x# = q#(j, 1): m2y# = q#(j, 2): m2# = q#(j, 3) Dx# = (m2x# - m1x#) Dy# = (m2y# - m1y#) Fx# = 0: Fy# = 0 IF NOT Dx# = 0 THEN Fx# = G# * (m1# * m2#) / (Dx# ^ 2) IF NOT Dy# = 0 THEN Fy# = G# * (m1# * m2#) / (Dy# ^ 2) IF m1x# > m2x# THEN Fx# = -Fx# IF m1x# < m2x# THEN Fx# = Fx# IF m1y# > m2y# THEN Fx# = -Fy# IF m1y# < m2y# THEN Fx# = Fy# q#(i, 4) = q#(i, 4) + Fx# 'Apply the Force X to mass 1 q#(i, 5) = q#(i, 5) + Fy# 'Apply the Force Y to mass 1 'q#(j, 4) = q#(j, 4) + Fx# 'Apply the Force X to mass 2 (not needed, it is done in a later or earlier loop) 'q#(j, 5) = q#(j, 5) + Fy# 'Apply the Force Y to mass 2 j = j + 1 'Increase to next mass END IF LOOP NEXT i FOR set = 0 TO NumMasses 'Erase mass from screen PSET (q#(set, 1), q#(set, 2)), 15 NEXT set 'Calculate movement and update positions FOR i = 0 TO NumMasses x# = q#(i, 1): y# = q#(i, 2) Fx# = q#(i, 4): Fy# = q#(i, 5): oldVx# = q#(i, 6): oldVy# = q#(i, 7) m# = q#(i, 3) Ax# = Fx# / m# Ay# = Fy# / m# x# = x# + ((oldVx# * t#) + (.5 * Ax#) * (t# ^ 2)) y# = y# + ((oldVy# * t#) + (.5 * Ay#) * (t# ^ 2)) q#(i, 1) = x# q#(i, 2) = y# 'CHECK FOR COLLISION**** ' FOR k = 0 TO NumMasses ' l = 0 'Reset inner loop ' DO WHILE l < NumMasses + 1 'Iterate through all masses ' IF l = k THEN l = l + 1 'Skip one so mass doesn't affect itself ' ' IF NOT l = NumMasses + 1 THEN 'If the array element is there... ' IF q#(k, 1) = q#(l, 1) AND q#(k, 2) = q#(l, 2) THEN ' 'q#(k, 1) = q#(k, 1) - ((q#(k, 6) * t#) + (.5 * (q#(k, 4) / q#(k, 3))) * (t# ^ 2)) 'move back to old position ' 'q#(k, 2) = q#(k, 2) - ((q#(k, 7) * t#) + (.5 * (q#(k, 5) / q#(k, 3))) * (t# ^ 2)) ' END IF ' l = l + 1 ' END IF ' LOOP ' NEXT k q#(i, 6) = Vx# q#(i, 7) = Vy# NEXT i LOOP END EndNow: END RETURN ****************************************************************** ****************************************************************** Also, is there a better way to calculate the forces without using a "time step"?

Share this post


Link to post
Share on other sites
Advertisement
<<<NumMasses = 50 'Total number of masses - 1>>>

this may be the problem, 50 masses are to much to see any unrandom movements
only three masses can produce an chaotic system, cant imagine how it may look with 50
are your masses standing on the starting or are they moving (if they are in proper movement every num of masses can produce an stable system, but to find such an movings (speed and direction) for every of the 50 masses you can buy a little EarthSimulator)
actually i saw on the cebit an math-physic simulator who did this, but with more than 2 masses it only switched to chaotic

cut it down to 2 masses for testing (there you should see an stable system if both masses are in proper movement, else they may collide or fly in far space)

Share this post


Link to post
Share on other sites
Oops, I changed that back last minute but I was testing with 2 and 3 masses and with 3 masses it was horrible.

With 2 masses one of them did not move for some reason even though it should be moving just as fast as the other mass and that is my biggest problem. Sorry about that.

Each mass starts motionless and moves due to the gravity of other masses.

Share this post


Link to post
Share on other sites
i cant figure out where the problem in your code is, that 1 mass is moving and 1 is not (maybe Fx# and Fy# cant have negative values - i dont know this language)

have you tryed to trace your calculating code with an debuger, if you see which values which variable at a time has it can help you to see what your programm actually does

hope it helps

Share this post


Link to post
Share on other sites
When I check the values of the forces they are correct, it is as if the forces are not being applied at all to one of the particles. I can't discover why this happens because I traced the program myself with 2 masses (NumMasses = 1)
and I found nothing wrong. I think I weeded out any typos too.

Well, thanks for your efforts.

Share this post


Link to post
Share on other sites
I will tell you what is wrong with your code. Nothing. Behold the N body problem. Wolfram's site tends to be thick so Ill also take a stab at it. Attempting to [wholly] solve for the way a set of N gravitationally interacting bodies behave or evolve over a period of time t is impossible for more than N = 2 bodies because as Poincare showed, to the great annoyance of Lagrange in the late 1800s, chaos is introduced into the dynamics of the system. Determinisim is no longer the norm.

Algorithims based on numeric approximations built on the set of differential equations governing the systems can be used to a fair bit of accuracy but a good deal of expense. I started this project too before but didnt get far with it. Maybe I will take it up again...Need to find my books on celestial mechanics...Anyway you are going to have to introduce hacks into the system to make it work at any reasonable speed and also look accurate. Introduce some constraints based on your needs. Good luck! [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!