0 votes

First post. Be gentle.

  • I have 2 RigidBodies (Star is Static and Planet is Character)
  • Both bodies have Area2D with Combined Gravity points.
  • Global gravity is disabled.
  • More planets could be added and the idea is for each to Combine their gravity with the Star to give a n-body-like effect.

But for now I have 1 Planet to orbit around the Star.

During startup I want to set each Planet's velocity so that it is in a perfect orbit around the sun (ignoring for now the fact that each planet will interfere during n-body calcs).

However when I attempt to calculate the velocity required for circle orbit I find that the velocity is waaay to low to keep the planet moving in circle. This results in the planet plunging into the star.

I think I'm missing something obvious - here is current code in my ready function:

    func _ready():

        # set initial estimated veloc for each planet based on distance to star
        for member in get_tree().get_nodes_in_group("planet"):

            print("\nmem  ",member.name)
            print("sm  ",$Star.mass)        
            print("pm  ",member.mass)       

            # http://orbitsimulator.com/formulas/vcirc.html

            var direction = member.position.direction_to($Star.position)
            print(direction)

            var distance = member.position.distance_to($Star.position)
            print("dis  ",distance)     

            var dir_tangent=direction.tangent() 
            print("tan  ",dir_tangent)

            var bigG = 6.6743 * pow(10, -11)  # does this need tweaked?
            print("G   ", bigG)     

            var mag= sqrt((bigG*($Star.mass+member.mass))/distance)  # m/s
            print("mag  ", mag)     

            var accf=(bigG*($Star.mass+member.mass))/(distance*distance)  # not used currently
            print("acc  ", accf)        

            var velo=dir_tangent * mag  #  * 4000000    # seems that multiplying by about 4mil gives me almost a circle orbit
            print("velo ", velo)        

            member.linear_velocity=velo 

Fumbling with this for a few hours I noticed that if I multiply the MAG by about 4,000,000 then I get the (almost) circular orbit... something tells me that isn't the right way to handle it and that probably won't work for just any distance.

Please let me know if I can add more details.

  • What am I missing?
  • Am I misunderstanding unit conversions (pixels to meter?)
  • Is the gravity Acceleration Force even needed in this calculation?
  • What role does the Gravitational constant play here and if that needs tweaked how would I do that so that it works for planets at varying distances?
Godot version v3.3.3.stable.official [b973f997f]
in Engine by (12 points)

If you're only going to Simulate the solar system, use "Path / Pathfollow"

One thing I'd recommend is to use global_position instead of position, since the latter is relative to the node's parent. If your star and planet are both siblings (i.e. children of the same node) then position ought to work just fine though.

Another thing, which may be minor or irrelevant, is to set the gravity point of each star / planet to be Vector2(0,0) by calling Area2D.set_gravity_vec(Vector2(0,0)), since the default point of gravity is Vector2(0,1) relative to the Area2D origin.

The units of G are probably okay, since I think Godot's default units are metres, kilograms, and of course seconds.

The thing I suspect might be wrong is that the gravity field might be uniform by default. If I'm understanding the docs correctly (which are a bit ambiguous on this matter) then the default value of the Area2D's gravity_distance_scale being 0.0 means that the gravity field is uniform. My guess is that a value of 1.0 would correspond to the ordinary Newton's law of universal gravitation. Or maybe a value of G corresponds to the ordinary Newton's law of universal gravitation. To find out I'd try 1.0 and then G and see if either of those make it work as expected. https://docs.godotengine.org/en/3.3/classes/class_area2d.html#class-area2d-property-gravity-distance-scale

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.