(VPython) Can someone help me with my orbit simulator?

In summary: Create the gravity wellEM.set_ gravitational_force(G, MM)#Set the initial positions of Earth and moonER.set_position( MM*G*dt )#Update the positionsEM.update_position( ER,dt )#Draw the systemgraphics.draw_system( EM, ER, create_ ellipical_orbit(ER,MM))#Display the systemprint("In summary, the Earth-Moon system has a net momentum in the y direction.")In summary, the Earth-Moon system has a net momentum in the y
  • #1
CrosisBH
27
4
Code: https://pastebin.com/5LajNBpj

So I was messing with VPython, trying to create an Earth-Moon System. I got the actual gravity to work, and can create some nice ellipical orbits. However I run into trouble trying to make the actual (almost) circular orbit of the moon.

I know that the circular orbit velocity is given by v = √(GM/r). I put the moon at <3.8e8,0,0> and Earth in the center, so natural I assigned the initial velocity to the y of the moon since the velocity is tangent to the source of the gravity. (line 27)

However when I do this, I get the right velocity (~10000 m/s), but instead of orbiting circular it shoots off in the y direction leaving the gravity well of Earth. I've been looking through this code and can't find where it went wrong. Can someone have a look at it?
 
Technology news on Phys.org
  • #2
CrosisBH said:
However when I do this, I get the right velocity (~10000 m/s), but instead of orbiting circular it shoots off in the y direction leaving the gravity well of Earth. I've been looking through this code and can't find where it went wrong. Can someone have a look at it?
When you update the position of the moon and earth, you need to use dt as well. position = position + velocity * dt.
dt = 0.01 is really slow. dt = 10 seems to work ok. t is supposed to be seconds of real time?

Vec = Earth.pos + Moon.pos
This is wrong too, altough it doesn't make a lot of difference at the start.
 
  • #3
willem2 said:
When you update the position of the moon and earth, you need to use dt as well. position = position + velocity * dt.
dt = 0.01 is really slow. dt = 10 seems to work ok. t is supposed to be seconds of real time?This is wrong too, altough it doesn't make a lot of difference at the start.

Well, adding the dt term to the position update did stop the moon from shooting off, so that's one problem solved, but the orbit is still highly ellipical. I also changed the vector from Earth.pos + Moon.pos to Moon.pos - Earth.pos.
 
  • #4
I put in the following:
dt = 10 instead of dt = 0.01
Vec = Moon.pos - Earth.pos instead of Vec = Moon.pos + Earth.pos
Earth.pos = Earth.pos+Earth.v * dt instead of Earth.pos = Earth.pos+Earth.v
Moon.pos = Moon.pos+Moon.v * dt instead of Moon.pos = Moon.pos+Moon.v
And I get a circle. There's one problem still. The canvas is often not updated for a while right after the start, so the moon will skip a couple of days and the trail of th e moon will have a section of straight line. There must be some vpython reason for this.
This Earth moon system also has net momentum in the y direction, so you see the moon spiraling upwards, and if you enabled trails for the Earth you'd see it creeping upwards along a cycloid path. You might want to give an initial velocity to the Earth to counter this.
 
  • #5
willem2 said:
I put in the following:
dt = 10 instead of dt = 0.01
Vec = Moon.pos - Earth.pos instead of Vec = Moon.pos + Earth.pos
Earth.pos = Earth.pos+Earth.v * dt instead of Earth.pos = Earth.pos+Earth.v
Moon.pos = Moon.pos+Moon.v * dt instead of Moon.pos = Moon.pos+Moon.v
And I get a circle. There's one problem still. The canvas is often not updated for a while right after the start, so the moon will skip a couple of days and the trail of th e moon will have a section of straight line. There must be some vpython reason for this.
This Earth moon system also has net momentum in the y direction, so you see the moon spiraling upwards, and if you enabled trails for the Earth you'd see it creeping upwards along a cycloid path. You might want to give an initial velocity to the Earth to counter this.

It turns out my error was forgetting to set the intial velocity. I copied it correctly but didn't save in my ide, and now it's circular. Thanks for that. As for the other problem, would any velocity be good, or do you have in mind a specific velocity?
 
  • #6
CrosisBH said:
Thanks for that. As for the other problem, would any velocity be good, or do you have in mind a specific velocity?
The total momentum of the Earth and the moon should end up 0.
 
  • #7
You can post code clearly and more effectively, sustainably etc. using the tags [code=python] ... <source> ... [/code]
example:
Python:
from vpython import *
import numpy as np

#Earth-Moon gravity EMmulation

#IMPORTANT CONSTANTS

G = 6.67e-11    #Newton's Constant

EM = 6e24        #Earth's Mass
MM = 7e22        #Moon's Mass

ER = 6.3e6        #Earth's Radius
MR = 1.7e6        #Moon's Radius

Distance = 3.8e8Earth = sphere(pos = vector(0,0,0), radius = ER, color = color.blue)
Earth.m = EM
Earth.v = vector(0,0,0) #Velocity
Earth.p = Earth.m*Earth.v #Momentum

Moon = sphere(pos = vector(Distance,0,0), radius = MR, color = vector(0.5,0.5,0.5), make_trail=True)

Moon.m = MM
Moon.v = vector(0,np.sqrt(G*Earth.m/Distance),0)
Moon.p = Moon.m*Moon.v

dt = 0.01 

while True:
    Vec = Earth.pos + Moon.pos
    Fg = -G*Earth.m*Moon.m/mag(Vec)**3*Vec #Vector form of the Gravity Equation

    # 1. We will update the momentum according to Pf = FΔt + Pi

    Earth.p = -Fg*dt + Earth.p
    Moon.p = Fg*dt+Moon.p #-Fg becDistancese forces are equal and opposite

    #2. Update the velocites of each object, p = mv, so v = m/p

    Earth.v = Earth.p/Earth.m
    Moon.v = Moon.p/Moon.m

    #3. Update the positions
    Earth.pos = Earth.pos+Earth.v
    Moon.pos = Moon.pos+Moon.v
    print("Vector Velocity: ",Moon.v,"Magnitutde: ",mag(Moon.v))
    rate(10000)
 
  • #8
CrosisBH said:
Code: https://pastebin.com/5LajNBpj

So I was messing with VPython, trying to create an Earth-Moon System. I got the actual gravity to work, and can create some nice ellipical orbits. However I run into trouble trying to make the actual (almost) circular orbit of the moon.

I know that the circular orbit velocity is given by v = √(GM/r). I put the moon at <3.8e8,0,0> and Earth in the center, so natural I assigned the initial velocity to the y of the moon since the velocity is tangent to the source of the gravity. (line 27)

However when I do this, I get the right velocity (~10000 m/s), but instead of orbiting circular it shoots off in the y direction leaving the gravity well of Earth. I've been looking through this code and can't find where it went wrong. Can someone have a look at it?
(emphasis added)

The center of gravity of the Earth is not the center of gravity of the Earth-Moon system.

From the Wikipedia barycenter article:
In cases where one of the two objects is considerably more massive than the other (and relatively close), the barycenter will typically be located within the more massive object. Rather than appearing to orbit a common center of mass with the smaller body, the larger will simply be seen to wobble slightly. This is the case for the Earth–Moon system, where the barycenter is located on average 4,671 km (2,902 mi) from the Earth's center, well within the planet's radius of 6,378 km (3,963 mi).​
(emphasis added)

In your Earth-Moon instance of the two body problem, your treating the Earth as the barycenter, instead of deriving the barycenter from the masses and distances and densities and momenta of the Earth and Moon (between and within) , produces an incorrect orbital result.
 
  • #9
sysprog said:
(emphasis added)

In your Earth-Moon instance of the two body problem, your treating the Earth as the barycenter, instead of deriving the barycenter from the masses and distances and densities and momenta of the Earth and Moon (between and within) , produces an incorrect orbital result.

Note that while the Earth starts off at 0,0 with a apeed of 0, it is not fixed. With the initial conditions here the Earth will both wobble and move slowly upwards.
I made the suggestion to give the Earth a momentum in the opposite direction of the moon, so the het momentum would be 0. The barycenter would then be at the the point (4.67*10^6, 0)
This was not the problem that made the moon fly off.
 
Last edited:
  • #10
@CrosisBH : how are things ? Any favourable developments ?
 
  • #11
BvU said:
@CrosisBH : how are things ? Any favourable developments ?

I managed to get everything working. I got the moon to orbit in a nice circular fashion, and did what willem suggested and changed the Earth's momentum and got a nice Moon-Earth System. I'm going to try it soon with a system with multiple moons to see if I can incoorperate gravity with more than two objects, maybe like Mars
Orbit.gif
 

Attachments

  • Orbit.gif
    Orbit.gif
    54.4 KB · Views: 1,660
  • Like
Likes BvU

1. How can I create an orbit simulator using VPython?

To create an orbit simulator using VPython, you will need to have a basic understanding of VPython coding and the laws of gravity. There are also many online tutorials and resources available to help you get started.

2. What are the required components for an orbit simulator in VPython?

The required components for an orbit simulator in VPython include a central object (such as a planet or star), a smaller object (such as a satellite or moon), and the necessary variables and equations to simulate the gravitational forces between them.

3. How can I add more objects to my VPython orbit simulator?

To add more objects to your VPython orbit simulator, you will need to define their initial position, mass, and velocity, and then incorporate them into your code using the appropriate equations and parameters. You can also use loops and arrays to make the process more efficient.

4. Can I customize the appearance of objects in my VPython orbit simulator?

Yes, you can customize the appearance of objects in your VPython orbit simulator by using the various visual attributes available in VPython. These include color, size, texture, and opacity, among others.

5. How can I troubleshoot errors in my VPython orbit simulator?

If you encounter errors in your VPython orbit simulator, you can use the debugging tools available in VPython or refer to online forums and resources for assistance. It is also helpful to double-check your code and equations to ensure they are accurate and properly formatted.

Similar threads

  • Programming and Computer Science
Replies
2
Views
2K
Replies
4
Views
735
  • Classical Physics
Replies
7
Views
825
Replies
13
Views
2K
  • Programming and Computer Science
Replies
4
Views
4K
  • Introductory Physics Homework Help
Replies
18
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Replies
10
Views
1K
  • Classical Physics
Replies
2
Views
1K
  • Sci-Fi Writing and World Building
Replies
21
Views
1K
Back
Top