title: Solar System in Python
body: <p>I am trying to create a very simplified model of Solar System in Python, using the <a href="https://docs.python.org/3.3/library/turtle.html?highlight=turtle" rel="nofollow noreferrer">turtle</a> library. Earth, Earth's spacecraft, Mars and at the center of coordinate system - Sun. I've found a very helpful <a href="https://www.youtube.com/watch?v=v1GT0jGBR-I" rel="nofollow noreferrer">video</a>, that shows easiest way to draw an ellipse around the centre of mass, although, I see many disadvantages in it. For example, the math of the equations is illogical in here. </p>
  
  <p>Despite that, I don't have the problem with the unrealistic numbers in this video (like mysterious 1000). I just need to create simplified orbits, they don't have to be accurate to those in real world, so the code from video mentioned above should be enough. At the beggining I've created a simulation with just Mars and Earth orbiting the Sun: </p>
  
  <pre><code>import turtle
  import math
  
  mars = turtle.Turtle()
  earth = turtle.Turtle()
  
  def way_to_orbit(x,y, object, colors):
      object.dot(50, "yellow")
      object.color("white")
      object.fillcolor(colors)
      object.shape("circle")
      object.penup()
      object.setposition(x, y)
      object.pendown()
  
  
  def ellipse(object1, object2):
  
      loop = True
      object2_xvel = 0
      object2_yvel = 1
      object1_xvel = 0
      object1_yvel = 1
  
      while loop:
          object2_xvel += math.cos(math.radians(object2.towards(0, 0))) * (1000 / (object2.xcor() ** 2 + object2.ycor() ** 2))
          object2_yvel += math.sin(math.radians(object2.towards(0, 0))) * (1000 / (object2.xcor() ** 2 + object2.ycor() ** 2))
          object2.setposition(object2.xcor() + object2_xvel, object2.ycor() + object2_yvel)
  
          object1_xvel += math.cos(math.radians(object1.towards(0, 0))) * (1000 / (object1.xcor() ** 2 + object1.ycor() ** 2))
          object1_yvel += math.sin(math.radians(object1.towards(0, 0))) * (1000 / (object1.xcor() ** 2 + object1.ycor() ** 2))
          object1.setposition(object1.xcor() + object1_xvel, object1.ycor() + object1_yvel)
  
  
  
  way_to_orbit(620, 0, mars, "red")
  way_to_orbit(375, 0, earth, "blue")
  
  ellipse(mars, earth)
  
  
  turtle.done()
  </code></pre>
  
  <p>Output was a moving simulation with two planets: </p>
  
  <p>Output with planets only</p>
  
  <p><img src="https://i.stack.imgur.com/mqnJd.jpg" alt=""></p>
  
  <p>Everything was fine, so I've decided to add the spacecraft, orbiting the Earth. In this case, Earth (<code>object2</code>) was the center of the mass for the spacecraft (<code>object3</code>) in <code>ellipse</code> function:</p>
  
  <pre><code>import turtle
  import math
  
  mars = turtle.Turtle()
  earth = turtle.Turtle()
  spacecraft = turtle.Turtle()
  
  
  
  def way_to_orbit(x,y, object, colors):
      object.dot(50, "yellow")
      object.color("white")
      object.fillcolor(colors)
      object.shape("circle")
      object.penup()
      object.setposition(x, y)
      object.pendown()
  
  
  def ellipse(object1, object2, object3):
  
      loop = True
      object2_xvel = 0
      object2_yvel = 1
      object1_xvel = 0
      object1_yvel = 1
      object3_xvel = 0
      object3_yvel = 1
      a = 0
      while loop:
          object2_xvel += math.cos(math.radians(object2.towards(0, 0))) * (1000 / (object2.xcor() ** 2 + object2.ycor() ** 2))
          object2_yvel += math.sin(math.radians(object2.towards(0, 0))) * (1000 / (object2.xcor() ** 2 + object2.ycor() ** 2))
          object2.setposition(object2.xcor() + object2_xvel, object2.ycor() + object2_yvel)
  
          object1_xvel += math.cos(math.radians(object1.towards(0, 0))) * (1000 / (object1.xcor() ** 2 + object1.ycor() ** 2))
          object1_yvel += math.sin(math.radians(object1.towards(0, 0))) * (1000 / (object1.xcor() ** 2 + object1.ycor() ** 2))
          object1.setposition(object1.xcor() + object1_xvel, object1.ycor() + object1_yvel)
  
          # object3_xvel += math.cos(math.radians(object3.towards(object2.xcor(), object2.ycor()))) * (1000 / ((object3.xcor()**2)-(object2.xcor()**2)) + ((object3.ycor()**2)-(object2.ycor()**2)))
          # object3_yvel += math.sin(math.radians(object3.towards(object2.xcor(), object2.ycor()))) * (1000 / ((object3.xcor()**2)-(object2.xcor()**2)) + ((object3.ycor()**2)-(object2.ycor()**2)))
          # object3.setposition(object3.xcor() + object3_xvel, object3.ycor() + object3_yvel)
  
  
  way_to_orbit(620, 0, mars, "red")
  way_to_orbit(375, 0, earth, "blue")
  way_to_orbit(376, 0, spacecraft, "green")
  
  ellipse(mars, earth, spacecraft)
  
  
  turtle.done()
  
  </code></pre>
  
  <p>Output was insane: 
  Output with spacecraft</p>
  
  <p><img src="https://i.stack.imgur.com/UGVWj.jpg" alt=""></p>
  
  <p>I have no idea what happened in here. I just want the moving simulation, as simple as possible, of 2 objects orbiting around the Sun and the satellite for one of them. The laws of physics are secondary in this case, all of this is made for other purpose. </p>
  
  <ol>
  <li>So, how can I fix the path of the turtle simulating the spacecraft/satellite?</li>
  <li>Is there any easier or faster way to create such simulation in Python?</li>
  <li>Could somebody explain for me the methods used for ellipse drawing used in the video? </li>
  </ol>