太空飞船任务,生成一个地球发射、火星着陆以及下一次发射窗口返回地球的动画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()
相关推荐
kyle~2 小时前
机器人时间链路---工程流程示例
c++·3d·机器人·ros2
传说故事5 小时前
【论文阅读】VGGT-Ω
论文阅读·人工智能·3d·具身智能
熊猫_豆豆6 小时前
一个模拟四轴飞行器在随机气流扰动下悬停飞行的交互式3D仿真网页,包含飞行器建模与PID控制算法
javascript·3d·html·四轴无人机模拟飞行
3DVisionary6 小时前
磁性轴承尺寸如何精准检测?蓝光扫描仪全尺寸3D检测解析
人工智能·3d·质量控制·蓝光三维扫描仪·非接触三维测量·磁性轴承·全尺寸形位公差
MhZhou04126 小时前
1.11M参数小模型实现脑瘤分割 CVPR 2026 Findings 开源
算法·计算机视觉·3d·空间计算
迁移科技7 小时前
案例丨AI+3D视觉,赋能制药行业拆垛及破包更精准高效
人工智能·科技·3d·自动化·视觉检测
今日综合7 小时前
杭州博型科技:用3D扫描建模与打印复刻时光,定制你的专属记忆
科技·3d
threelab9 小时前
Three.js 3D 地图可视化 | 三维可视化 / AI 提示词
前端·javascript·人工智能·3d·着色器
CG_MAGIC10 小时前
PBR 材质通用套用技巧
3d·材质·效果图·建模教程·渲云渲染
yeflx10 小时前
HLOC (Hierarchical-Localization) 技术文档
3d