太空飞船任务,生成一个地球发射、火星着陆以及下一次发射窗口返回地球的动画3D代码


python 复制代码
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from mpl_toolkits.mplot3d import Axes3D

# 天体参数设置(简化模型)
AU = 1.5e8  # 天文单位(公里)
earth_orbital_radius = 1.0 * AU
mars_orbital_radius = 1.5 * AU
orbital_speed = 2e4  # 简化轨道速度(km/s)

# 时间参数
earth_period = 365  # 天
mars_period = 687  # 天
transfer_time = 258  # 霍曼转移时间(天)
time_step = 2  # 动画时间步长(天)
total_duration = 800  # 总任务时间(天)

# 初始化图形
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
ax.set_facecolor('black')


# 天体初始位置
def celestial_pos(t, period, radius):
    angle = 2 * np.pi * t / period
    return radius * np.array([np.cos(angle), np.sin(angle), 0])


# 霍曼转移轨道计算
def transfer_orbit(t, t_start, radius_from, radius_to, transfer_time):
    angle = np.pi * (t - t_start) / transfer_time
    r = radius_from + (radius_to - radius_from) * (t - t_start) / transfer_time
    return r * np.array([np.cos(angle), np.sin(angle), 0])


# 初始化绘图元素
earth, = ax.plot([], [], [], 'o', color='blue', markersize=8)
mars, = ax.plot([], [], [], 'o', color='red', markersize=6)
ship, = ax.plot([], [], [], 'o', color='white', markersize=4)
trajectory, = ax.plot([], [], [], '-', color='gray', alpha=0.5)
sun = ax.plot([0], [0], [0], 'o', color='yellow', markersize=12)[0]

# 坐标轴设置
max_orbit = mars_orbital_radius * 1.2
ax.set_xlim(-max_orbit, max_orbit)
ax.set_ylim(-max_orbit, max_orbit)
ax.set_zlim(-max_orbit / 10, max_orbit / 10)
ax.axis('off')


# 动画更新函数
def update(frame):
    t = frame * time_step

    # 更新天体位置
    earth_pos = celestial_pos(t, earth_period, earth_orbital_radius)
    earth.set_data(earth_pos[0], earth_pos[1])
    earth.set_3d_properties(0)

    mars_pos = celestial_pos(t, mars_period, mars_orbital_radius)
    mars.set_data(mars_pos[0], mars_pos[1])
    mars.set_3d_properties(0)

    # 飞船状态机
    if t < transfer_time:  # 前往火星阶段
        ship_pos = transfer_orbit(t, 0, earth_orbital_radius, mars_orbital_radius, transfer_time)
    elif t < transfer_time + 30:  # 火星停留
        ship_pos = mars_pos
    else:  # 返回地球阶段
        ship_pos = transfer_orbit(t - transfer_time - 30, 0, mars_orbital_radius, earth_orbital_radius, transfer_time)

    ship.set_data(ship_pos[0], ship_pos[1])
    ship.set_3d_properties(0)

    # 更新轨迹
    x, y, z = trajectory.get_data_3d()
    x = np.append(x, ship_pos[0])
    y = np.append(y, ship_pos[1])
    z = np.append(z, 0)
    trajectory.set_data(x, y)
    trajectory.set_3d_properties(z)

    return earth, mars, ship, trajectory, sun


# 创建动画
ani = FuncAnimation(fig, update, frames=int(total_duration / time_step),
                    interval=50, blit=True)

# 添加图例和标注
ax.text(0, 0, 0, "SUN", color='yellow', ha='center')
ax.text(earth_orbital_radius, 0, 0, "Earth", color='blue')
ax.text(mars_orbital_radius, 0, 0, "Mars", color='red')

plt.show()
相关推荐
子燕若水19 小时前
daz3d + PBRSkin (MDL)+ SSS
3d
灏瀚星空1 天前
Python数学可视化:3D参数曲面与隐式曲面绘制技术
开发语言·python·3d
高德技术1 天前
全流程开源!高德3D贴图生成系统,白模一键生成真实感纹理贴图
3d·贴图
daifgFuture3 天前
Android 3D球形水平圆形旋转,旋转动态更换图片
android·3d
牧子川3 天前
【论文解读】CVPR2023 PoseFormerV2:3D人体姿态估计(附论文地址)
3d·cvpr2023·poseformerv2
资深设备全生命周期管理3 天前
优化版本,增加3D 视觉 查看前面的记录
3d
m0_748250743 天前
GPUCUDA 发展编年史:从 3D 渲染到 AI 大模型时代(上)
人工智能·3d
少林6594 天前
谷歌地图高清卫星地图2026中文版下载|谷歌地图3D卫星高清版 V7.3.6.9796 最新免费版下载 - 前端工具导航
3d·谷歌地图
LeonDL1684 天前
HALCON 深度学习训练 3D 图像的几种方式优缺点
人工智能·python·深度学习·3d·halcon·halcon训练3d图像·深度学习训练3d图像
xhload3d5 天前
图扑软件 | 带你体验 Low Poly 卡通三维世界
物联网·3d·智慧城市·html5·webgl·数字孪生·可视化·工业互联网·三维建模·工控·轻量化·中国风·卡通动画·写实风格·科技风·low poly