Visual Python example

#!/usr/bin/python

from visual import *

def gravity(thing1, thing2): dist = thing2.pos – thing1.pos force = 6.7e-11 * thing1.mass * thing2.mass * dist / mag(dist)**3 return force

earth = sphere()
earth.pos = vector(0, 0, 0)
earth.radius = 6.371e6 # meters
earth.mass = 5.9736e24 # kilograms
earth.color = color.blue

moon = sphere()
moon.pos = vector(3.844e8, 0, 0) # meters
moon.radius = 1.737e6 # meters
moon.mass = 7.3477e22 # kilograms
moon.color = color.white

moon.momentum = vector(0, 7.509e25, 0)
earth.momentum = -moon.momentum

bodies = [earth, moon]

for body in bodies: body.orbit = curve(color = body.color, radius = body.radius / 10.0)

deltat = 1000 # seconds

while True: rate(100) # set animation speed for body in bodies: force = sum([gravity(body, other) for other in bodies if other != body]) body.momentum += force * deltat

for body in bodies: body.pos += (body.momentum / body.mass) * deltat body.orbit.append(pos = body.pos)