RigidBody2D with Area2D gravity - Initial velocity for a circular orbit

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rknepp

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)
    		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)		

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?

If you’re only going to Simulate the solar system, use “Path / Pathfollow”

ramazan | 2022-07-12 07:30

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. Area2D — Godot Engine (3.3) documentation in English

haydenv | 2022-07-12 11:51