I'm fairly certian that the problem of a rotation body about antoher body can be solved analitically. since the sun is so massive, to a close approximation, we can ignore gravity from other planets. I'm fairly certian for the problem of one planet orbiting abround the sun, you can derive a paramaterized version of the motion, after all, we know from kepler law that it travels in an eliptical path, and we can certianally prarmaterize an elipse. I think you could build an elipse representing the positions of the planet, and use energy consideration to find the velocity of the planet. this would be certianlly faster then integrated force approach, furthermore, since it is traveling in a paramaterizxed curve, it eliminates the problem you have with to large of a time step, naimly divergance from the eliptical path.
to use energy consideration you could do something like this
you know that the sum
potentialEnergyDueToGravity + kineticEnergyOfPlanet = Constant; the potential energy due to gravity can be easily calculated, and the kinetic energy can be solved for using the above equation, which would give us the velocity of the object. also, this technique is sutable for real time.
tim
Planetary Gravity.
Still nobody posted the gravitational constant:
gravitational constant = 6.67300 × 10-11 m3 kg-1 s-2
gravitational constant = 6.67300 × 10-11 m3 kg-1 s-2
Dang... I think I did what you said... but it's not working to well. All the bodies just start moving down-right instead of toward each other. Here is the code I have now:
I bet I'm doing something really stupid... but I'm not sure.
;Cycle through and update all existing bodies For cur1.Body = Each Body For cur2.Body = Each Body If cur1\ID <> cur2\ID ;Calculate gravitational force RXsquared# = (cur1\x-cur2\x)*(cur1\x-cur2\x) RYsquared# = (cur1\y-cur2\y)*(cur1\y-cur2\y) Rsquared# = RXsquared + RYsquared Fgrav# = (cur1\mass*cur2\mass)/Rsquared ;Calculate the ratio Ratio# = Sqr(RYsquared#)/Sqr(RXsquared# + RYsquared#) ;Calculate acceleration based on (Force = Mass * Acceleration) A1# = (Fgrav/cur1\mass) ;Adjust the velocities of this body based on the force calculated cur1\xvel = cur1\xvel + A1# * Ratio# cur1\yvel = cur1\yvel + A1# * Ratio# EndIf Next ;Adjust the position of the bodie based on it's velocity cur1\x = cur1\x + cur1\xvel cur1\y = cur1\y + cur1\yvel ;Draw the body Color 255,255,255 Oval cur1\x-cur1\radius,cur1\y-cur1\radius,cur1\radius*2,cur1\radius*2,1 Next
I bet I'm doing something really stupid... but I'm not sure.
Quote:Original post by JS Lemming
Dang... I think I did what you said... but it's not working to well. All the bodies just start moving down-right instead of toward each other. Here is the code I have now:
*** Source Snippet Removed ***
I bet I'm doing something really stupid... but I'm not sure.
The formula calculates the intensity of the force, and you're applying it as a direction too.
What you should do is to calculate the vector between the centers of your two bodies, normalize it, and then multiply it by the gravitation intensity. This is your acceleration force for one of the two bodies, the other one is reversed. You should then take into account the mass of each body to calculate the change of velocity.
Y.
That ratio you have is the same as the sine. Except you're taking the square root of y^2 and that basically just loses the sign information, which is not what you want to do, it creates the same problem as using atan. Also cosine is dx/sqrt(dx^2+dy^2), so use that for determining how much of the acceleration to apply in the x direction.
For anyone who is trying to use atan for this effect: don't :P
Read up on atan2, it's a wrapper around the atan function that automatically gives you the output you want. Just be careful not switching around the coordinates, that still happens to me sometimes ;)
Read up on atan2, it's a wrapper around the atan function that automatically gives you the output you want. Just be careful not switching around the coordinates, that still happens to me sometimes ;)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement