Below is a complete executable Python script that uses the VPython library to create a 3D animation of the solar system, with 9 planets rotating around the sun in their real-time paths. Each cosmic object is labeled with its name and distance from the Sun. Before running the script, ensure you have installed vpython using the following command:
bash
pip install vpython
Here is the code:
python
from vpython import sphere, vector, rate, label, color
import numpy as np
Constants
AU = 1.496e11 # Astronomical Unit in meters
Define planets with radius (km), colors and distance from the sun (AU)
planets = [
{"name": "Mercury", "color": color.gray(0.5), "distance": 0.39, "radius": 2439.7},
{"name": "Venus", "color": color.yellow, "distance": 0.72, "radius": 6051.8},
{"name": "Earth", "color": color.blue, "distance": 1.0, "radius": 6371},
{"name": "Mars", "color": color.red, "distance": 1.52, "radius": 3389.5},
{"name": "Jupiter", "color": color.orange, "distance": 5.2, "radius": 69911},
{"name": "Saturn", "color": color.orange(0.7), "distance": 9.58, "radius": 58232},
{"name": "Uranus", "color": color.cyan, "distance": 19.18, "radius": 25362},
{"name": "Neptune", "color": color.blue(0.5), "distance": 30.07, "radius": 24622},
]
Create the Sun
sun = sphere(pos=vector(0, 0, 0), radius=696340 / 500000, color=color.yellow)
Create planets
planet_bodies = []
for planet in planets:
body = sphere(pos=vector(planet["distance"] * AU, 0, 0),
radius=planet["radius"] / 500000,
color=planet["color"])
planet_bodies.append(body)
Create labels for planets
for planet in planets:
label(pos=vector(planet["distance"] * AU, 0, 0),
text=planet["name"],
xoffset=20,
yoffset=20,
height=10,
border=False)
Animation loop
while True:
rate(100) # Set the frame rate
for i, planet in enumerate(planets):
angle = (planet["distance"] / 12) * np.pi / 180 # Simplified elliptical motion
planet_bodies[i].pos = vector(planet["distance"] * AU * np.cos(angle),
planet["distance"] * AU * np.sin(angle),
0)
Explanation:
VPython: This library allows for easy 3D visualization. The solar system is represented using spheres for the sun and planets.Planet Data: Each planet is defined with its name, color, distance from the sun in Astronomical Units (AU), and radius.Animation Loop: The planets rotate around the sun in a simplistic circular manner. The angles are calculated to provide motion.Labels: Each planet is labeled with its name at its respective position.
Note:
For running this script, it will create an interactive 3D model that you can manipulate in your browser. Make sure to run this in an environment that supports VPython, such as an IPython Jupyter Notebook or Python scripts running in environments that support graphics.