Skip to content

Instantly share code, notes, and snippets.

@uwezi
Last active April 7, 2026 19:54
Show Gist options
  • Select an option

  • Save uwezi/a7e9251cf3740a9a4efc6eb2b12f3c96 to your computer and use it in GitHub Desktop.

Select an option

Save uwezi/a7e9251cf3740a9a4efc6eb2b12f3c96 to your computer and use it in GitHub Desktop.
[Planetary orbits] Orbit simulation. #manim #animation #simulation #newton #planet #physics #orbit
class Planet(VMobject):
def __init__(self,
sun:VMobject=None,
ax:CoordinateSystem=None,
color = BLUE,
radius =0.1,
mass = 6e24,
pos = np.array([150e9,0,0]),
vel = np.array([0,30e3,0]),
G = 6.67e-11,
speed_factor = 24*60*60*20
):
super().__init__()
self.become(Dot(point=ax.c2p(*pos), color=color, radius=radius))
self.mass = mass
self.vel = vel
self.ax = ax
self.sun = sun
self.G = G
self.speed_factor = speed_factor
self.add_updater(self.updater)
def updater(self, obj, dt):
if self.sun is not None:
dvec = self.ax.p2c(self.sun.get_center())-self.ax.p2c(obj.get_center())
a = dvec*self.G*self.sun.mass/(np.linalg.norm(dvec)**3)
obj.vel = obj.vel + a*dt*self.speed_factor
obj.shift(self.ax.c2p(*(obj.vel*dt*self.speed_factor)))
class orbit(Scene):
def construct(self):
ax = ThreeDAxes(
x_range=[-200e9,200e9,50e9],
y_range=[-200e9,200e9,50e9],
z_range=[-200e9,200e9,50e9],
x_length=8,
y_length=8,
z_length=8,
tips=False
)
G = 6.67e-11
sun = Planet(
sun = None,
ax = ax,
color = YELLOW,
radius = 0.2,
mass = 2e30,
pos = np.array([0,0,0]),
vel = np.array([3000,0,0]),
)
earth = Planet(
sun = sun,
ax=ax,
radius=0.1,
color=BLUE,
mass=6e24,
pos = np.array([150e9,0,0]),
vel = np.array([3000,30000,0]),
)
bluetrace = TracedPath(earth.get_center, stroke_width=1, stroke_color=BLUE)
self.add(sun,earth,bluetrace)
self.wait(50)

Based on my previous gist and following a video by user braintruffle on youtube https://www.youtube.com/watch?v=nCg3aXn5F3M

The blue planet is simulated by first updating the speed and then the position.

orbit_ManimCE_v0 20 1

I have been simulating planetary systems for the first time using Turbo Pascal in the middle of the 1990s and observed how planets got sling-shotted out of the system under certain circumstances - if I only had my old code I could check in which order I had made the updates back then, but I remember how critical short time steps were in order to keep the simulations and orbits somewhat stable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment