You are browsing as a guest. Sign up (or log in) to start making projects!

2h 29m 24s logged

I’m building Starshell, a 2D astronomical physics simulator in pure Python turtle. No game engine, no physics library—just the standard library, a turtle canvas, and physics and math.

The idea in my head is quite simple: I want to simulate how gravity actually works. Bodies with mass attract each other, their paths bend, and orbits merge. Eventually, I want collisions, multi-body chaos, and a UI where you click to place stars and planets.

Today’s milestone was the first orbit. To get here, I didn’t just plug in some formulas; I had to build the physics chains from a blank turtle canvas:

  1. Distance between two bodies
  2. Gravitational force: F = G × (m1 × m2) / r²
  3. Direction of that force: atan2(y, x)
  4. Force components: splitting into x and y with cos and sin
  5. Acceleration: a = F / m
  6. Velocity update: adding acceleration each frame
  7. Position update: moving the turtle by velocity each frame

The orbit isn’t actually hard to code. The planet doesn’t know it’s supposed to go in a circle; I don’t tell it to trace a circle. It just responds to the pull of the star every single frame. The circle is what falls out of the math when the initial velocity is balanced just right.

One of the trickiest moments was realizing my gravitational constant (G = 1) made the force so small—around 0.0004—that compared to my velocity of two units per frame, the planet just flew away in a straight line. I couldn’t see any curve at all, let alone an orbit. Cranking the gravity up to something like 1,000 to 1,600 (found by experimentation) made the orbits visible.

That moment when the blue dot actually curved and came back around in a full orbit felt very satisfying. Next up, I want multiple bodies that interact with each other, not just a fixed point (aka a star).

Follow Along!

0
3

Comments 1

@Mehmet

Follow along to where, hopefully, this becomes a fully fledged 2D physics sim!