Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | WindowsGott |
I am trying to make a simple planetary simulation in 2D space where every RigidBody2D is acting as a “celestial body”. For some reason, when I start the simulation with 2 bodies with the orbiting one being 10x lighter, but giving it a predefined velocity it starts to orbit. However, this orbit only lasts for half (in this case only a quarter of) an orbital period, after which it just accelerates away from the other body. The weird thing is, it doesn’t matter which side I am starting on, it always behaves this way.
Visualization:
After the planet completes half an orbit and crosses the x-axis it starts to accelerate away.
Code handling the gravity:
for child1 in get_children():
for child2 in get_children():
if child1 != child2:
distance = child2.position - child1.position;
force = ( (G * child1.mass * child2.mass) / (pow(distance.length(), 2)) );
child1.add_force(Vector2(0, 0), force * distance.normalized());
I know I don’t have to implement “radial gravity” myself, though when working with Area2D’s and Gravity Points I also ran into some troubles:
- If I have big constelations, how big should I make my Area2D? After a while I just got insecure about the size…
- I’d like to later add a preview of which orbit an object will take. Furthermore I’d like to automatically place an object at an circular orbit to its parent. To calculate these things I need values like the G constant, however, I found no documentation for how Godot does handles gravity online and attempts at calculating it myself were fruitless.
I guess Godot uses some other way I am not aware of to simulate “point gravity”. If somebody could point me towards documentation for this or explain it to me I would be delighted.
Hope anyone can help me or even just point me to some tutorial or other resource which could help me wrap my head around these subjects, I just had troubles finding the information I was looking for online.