Skip to content

Instantly share code, notes, and snippets.

@uwezi
Last active December 11, 2025 21:38
Show Gist options
  • Select an option

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

Select an option

Save uwezi/931016039f080c2ad9d23bf49db3f0b3 to your computer and use it in GitHub Desktop.
[planetary orbit] Simulating a stable planetary orbit in Manim. #manim #astro #physics #updater #newton
class orbit(Scene):
def construct(self):
ax = Axes(
x_range=[-200e9,200e9,50e9],
y_range=[-200e9,200e9,50e9],
x_length=8,
y_length=8,
tips=False
)
G = 6.67e-11
sun = Dot(point=ax.c2p(0,0),color=YELLOW)
sun.m = 2e30
p = Dot()
p.m = 6e24
p_blue = p.copy().set_color(BLUE).move_to(ax.c2p(150e9,0,0))
p_blue.v = np.array([0,30e3])
p_red = p.copy().set_color(RED).move_to(ax.c2p(-150e9,0,0))
p_red.v = np.array([0,-30e3])
speed_factor = 24*60*60*20# time steps
def blue_updater(obj,dt):
dvec = ax.p2c(sun.get_center())-ax.p2c(obj.get_center())
a = dvec*G*sun.m/(np.linalg.norm(dvec)**3)
obj.v = obj.v + a*dt*speed_factor
obj.shift(ax.c2p(*(obj.v*dt*speed_factor)))
def red_updater(obj,dt):
dvec = ax.p2c(sun.get_center()) - ax.p2c(obj.get_center())
a = dvec*G*sun.m/(np.linalg.norm(dvec)**3)
obj.shift(ax.c2p(*(obj.v*dt*speed_factor)))
obj.v = obj.v + a*dt*speed_factor
p_blue.add_updater(blue_updater)
p_red.add_updater(red_updater)
bluetrace = TracedPath(p_blue.get_center, stroke_width=1, stroke_color=BLUE)
redtrace = TracedPath(p_red.get_center, stroke_width=1, stroke_color=RED)
self.add(sun,p_blue,p_red,bluetrace,redtrace)
self.wait(30)

Following a video released today by user braintruffle on youtube https://www.youtube.com/watch?v=nCg3aXn5F3M

Showing the difference the update order of motion equations makes in computer simulations for example of planetary orbits. The red planet is simulated by first updating the planet's position and then the velocity, the blue planet is simulated by first updating the speed and then the position.

The blue simulated orbit is much more stable than the red one - the initial values are taken from the Earth-Sun system.

image

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