当前位置: 首页 > 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 / periodreturn 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_timer = radius_from + (radius_to - radius_from) * (t - t_start) / transfer_timereturn 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_poselse:  # 返回地球阶段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()
http://www.lryc.cn/news/539034.html

相关文章:

  • IDEA——Mac版快捷键
  • 智能体系统(AI Agent System)是什么?——从概念解析到企业数字化转型的全景落地及投资视角
  • Vue 前端开发中的路由知识:从入门到精通
  • 前端VUE+后端uwsgi 环境搭建
  • I2C实践开发 ---【STM32-I2C-HDC1080温湿度采集系统】
  • 【个人开发】deepspeed+Llama-factory 本地数据多卡Lora微调【完整教程】
  • 浏览器报错:无法访问此网站 无法找到xxx.xxx.net的DNS地址。正在诊断该问题。尝试运行Windows网络诊断。DNS_PROBE_STARTED
  • 【设计模式】 代理模式(静态代理、动态代理{JDK动态代理、JDK动态代理与CGLIB动态代理的区别})
  • 网络安全-攻击流程-用户层
  • 网络安全等级保护测评(等保测评):全面指南与准备要点
  • 具身导航赋能智能物流!OpenBench:智能物流最后一公里语义导航新基准
  • 详解 本机安装多个MySQL服务【为后续大数据量分库分表奠定基础,以mysql8.0为例,附有图文】
  • 2025年新趋势:如何利用AI技术优化你的在线帮助中心
  • 同花顺Java开发面试题及参考答案 (上)
  • CommonLang3-使用介绍
  • Java常用设计模式及其应用场景
  • Ubuntu 下 nginx-1.24.0 源码分析 - ngx_pnalloc函数
  • 【ISO 14229-1:2023 UDS诊断(会话控制0x10服务)测试用例CAPL代码全解析①】
  • A与B组件自动对齐与组装,无映射直接补偿。
  • QT 读写锁
  • C++ 的时间库之二:Ratio
  • 使用小雅xiaoya/Emby正确的观看电影电视剧的姿势
  • Java状态机
  • 【Pandas】pandas Series isin
  • 通过VSCode直接连接使用 GPT的编程助手
  • 一种最常见的js加密解密
  • 【Python爬虫(4)】揭开Python爬虫的神秘面纱:基础概念全解析
  • SMOJ 一笔画/洛谷 P7171 COCI 2020/2021 #3 Selotejp 题解
  • 【Java学习】继承
  • 计时器任务实现(保存视频和图像)