matlab模拟飞机3D飞行

MATLAB 3D 飞机飞行模拟框架。
特点:
✓ 纯 MATLAB 代码,无 Simulink;
✓ 用键盘 W/S/↑/↓/←/→/A/D 实时控制俯仰、滚转、偏航、推力;
✓ 自带简单空气动力 + 四元数姿态更新;
✓ 三维视景随飞机移动,可切换 座舱视角 / 外部视角


1. 一键主程序 fly3D_demo.m

clc; clear; close all;
global plane state camMode
camMode = 1;          % 1=座舱 2=外部
initPlane();          % 初始化飞机
initScene();          % 初始化场景
initKey();            % 键盘钩子

% 主循环 50 Hz
tic; dt = 0.02;
while true
    if toc > dt
        tic;
        updateState(dt);   % 动力学+姿态
        updateScene();     % 更新画面
        drawnow limitrate;
    end
end

2. 飞机结构体 & 初始化

function initPlane()
global plane state
% 飞机物理参数
plane.m   = 1;            % kg
plane.Ixx = 0.1; plane.Iyy = 0.1; plane.Izz = 0.1;
plane.S   = 0.05;         % 机翼面积 m2
plane.rho = 1.225;        % 空气密度

% 状态:北东地坐标 + 速度 + 姿态四元数 + 角速度
state.pos   = [0 0 50];   % [x y z] m
state.vel   = [0 0 0];    % m/s
state.q     = [1 0 0 0];  % [w x y z]
state.omega = [0 0 0];    % [p q r] rad/s

state.thrust = 0;         % N
end

3. 场景与键盘钩子

function initScene()
global plane state camMode
figure('Color','w'); axis equal vis3d
hold on; grid on
% 地面网格
[gx,gy] = meshgrid(-200:50:200,-200:50:200);
surf(gx,gy,zeros(size(gx)),'FaceColor',[0.7 0.85 0.7],'EdgeColor','none');
% 飞机机身(3D patch)
plane.body = patch('Vertices',[0 0 0; 5 0 0; 2 0 1; 0 0 0], ...
                   'Faces',[1 2 3 4], ...
                   'FaceColor',[0.8 0.2 0.2]);
% 机翼
plane.wing = patch('Vertices',[2 0 0; 2 0 0; 2  3 0.2; 2 -3 0.2], ...
                   'Faces',[1 2 3 4], ...
                   'FaceColor',[0.2 0.2 0.8]);
light; camlight gouraud
axis([-100 100 -100 100 0 150])
end

function initKey()
set(gcf,'KeyPressFcn',@keyCB);
end

4. 实时动力学与姿态更新

function updateState(dt)
global state plane
% 键盘输入
[thrust,pitch,roll,yaw] = getInput();

% 推力
state.thrust = max(0, state.thrust + thrust*dt*20);

% 空气动力(极简化)
alpha = pitch; beta = roll;
CL = 2*pi*alpha; CD = 0.02 + 0.05*alpha^2;
qbar = 0.5*plane.rho*norm(state.vel)^2;
L = qbar*plane.S*CL; D = qbar*plane.S*CD;

% 机体坐标系力
F_body = [state.thrust-D; 0; -L];

% 机体→惯性坐标系旋转
R = quat2dcm(state.q);
F = (R.'*F_body).';          % 惯性系力
acc = F/plane.m - [0 0 9.81];  % 重力

% 速度/位置
state.vel = state.vel + acc*dt;
state.pos = state.pos + state.vel*dt;

% 角运动极简化
torq = [roll*5; pitch*5; yaw*5];
accOmega = [torq(1)/plane.Ixx; torq(2)/plane.Iyy; torq(3)/plane.Izz];
state.omega = state.omega + accOmega*dt;

% 四元数更新
delta = 0.5*quatmultiply(state.q,[0 state.omega]*dt);
state.q = state.q + delta;
state.q = state.q/norm(state.q);
end

5. 读取键盘输入

function [thrust,pitch,roll,yaw] = getInput()
global state
thrust = 0; pitch = 0; roll = 0; yaw = 0;
if isKeyPressed('w'), thrust =  1; end
if isKeyPressed('s'), thrust = -1; end
if isKeyPressed('uparrow'),  pitch =  1; end
if isKeyPressed('downarrow'), pitch = -1; end
if isKeyPressed('leftarrow'),  roll = -1; end
if isKeyPressed('rightarrow'), roll =  1; end
if isKeyPressed('a'), yaw = -1; end
if isKeyPressed('d'), yaw =  1; end
end

function tf = isKeyPressed(key)
persistent last
if isempty(last), last = []; end
now = get(gcf,'CurrentKey');
tf = strcmp(now,key);
end

6. 视角切换

function updateScene()
global state plane camMode
R = quat2dcm(state.q);
if camMode==1
    % 座舱视角:相机固定在机头,随飞机旋转
    pos = state.pos + R*[4 0 0]';
    look = state.pos + R*[10 0 0]';
    campos(pos); camtarget(look); camup(R*[0 0 1]');
else
    % 外部视角:相机追踪
    campos(state.pos + [0 -80 30]);
    camtarget(state.pos);
end

% 更新飞机顶点
bodyVerts = [0 0 0; 5 0 0; 2 0 1; 0 0 0];
wingVerts = [2 0 0; 2 0 0; 2  3 0.2; 2 -3 0.2];
bodyVerts = (R*bodyVerts.')'; wingVerts = (R*wingVerts.')';
set(plane.body,'Vertices',bodyVerts + state.pos);
set(plane.wing,'Vertices',wingVerts + state.pos);
end

7. 使用说明

键位 功能
W/S 增加/减小推力
↑/↓ 俯仰(pitch)
←/→ 滚转(roll)
A/D 偏航(yaw)
空格 在代码中自行添加 camMode = 3-camMode; 可切换视角

参考代码文献 matlab模拟飞机3D飞行程序 www.youwenfan.com/contentcsf/65609.html

8. 扩展方向

  1. 空气动力细化:使用 DATCOM 或 X-Plane 数据表替换简化公式
  2. PID 自动驾驶:加入高度/航向保持环路
  3. 多机协同:在 state 中加入 id 字段,绘制多个飞机
  4. VR 头显:MATLAB 2023 起支持 OpenXR,可直接接入 VR
posted @ 2025-09-04 15:49  bqyfa66984  阅读(44)  评论(0)    收藏  举报