当前位置: 首页 > news >正文

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

在这里插入图片描述


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()

相关文章:

  • 数据结构:哈希表(二)
  • 如何在Odoo 18中创建记录规则Rule
  • Deepseek官网接口文档
  • 【Python项目】文件销毁工具文档
  • 用户管理中心---前端页面设计测试登录功能
  • 服务器装机可用的基本操作
  • LSTM细胞状态门控设计详解:数学原理、代码实现与工业级优化方案
  • 超全Deepseek资料包,deepseek下载安装部署提示词及本地部署指南介绍
  • 《机器学习数学基础》补充资料:求解线性方程组的克拉默法则
  • JAVA EE初阶 - 预备知识(三)
  • 用自定义注解实现Excel数据导入中的枚举值校验
  • 第25周JavaSpringboot实战-电商项目 2.数据库准备和项目初始化
  • Dify本地部署过程问题
  • openGauss 6.0.0 RC1数据库日常运维
  • AllData数据中台核心菜单十三:数据湖平台
  • 【大模型】硅基流动对接DeepSeek使用详解
  • Jenkins+maven+gitlab+shell 实现项目自动化部署
  • 聚铭网络入围2025年度江苏省政府采购信息安全设备协议供货名单
  • 汽车通信未来新趋势:Eclipse uProtocol
  • 如何使用DeepSeek + PlantUML/Mermaid 生成专业图表
  • 四川资阳市原市长王善平被双开,“笃信风水,大搞迷信活动”
  • 鸿蒙电脑正式亮相,五年布局积累超2700项核心专利
  • 中国以“大幅开放市场”回应贸易保护主义
  • 经济日报:降准降息,提前还房贷划算吗?
  • 美联储宣布维持基准利率不变
  • 湖南张家界警方公告宣布一名外国人居留许可作废