MVH 发表于 2006-11-15 15:47

一个用matlab编的可以动的飞机动画制作小程序

一个用matlab编的可以动的飞机动画制作小程序,注意六个状态值是需要外部输入的,比较适合做飞行器仿真试验用。

该程序可以运行,但是尚存在一定的问题,希望大家讨论解决


总共有8个m文件

PLANE_.M

% plane_ same as List_B4.
% Plots a wire frame airplane.
% Copyright S. Nakamura
set(gcf, 'NumberTitle','off','Name', 'Figure B.3; plane_ ')


clc
clear, clf


mov1 = avifile('airplane.avi');
mov2 = avifile('airplane_xz.avi');
mov3 = avifile('airplane_xy,avi');
mov4 = avifile('airplane_yz,avi');

for(hhh = 0:-1:-15)
    clf
   
   
dth=pi/16;
fuselen=6;
thf=pi:-dth:pi/2;
xa = 0:0.5:fuselen;

hyq_x = hhh;
hyq_y = hhh;
hyq_z = -hhh;

xt=fuselen+0.25:0.25:fuselen+2;
dxt = 1.4/(length(xt)-0) ;
yt = -1+dxt:dxt:0.4;
length(yt);


xft= ;

yft= ;


xfb= ;

yfb=[-sin(thf),-ones(size(xa)),yt ] ;
k=length();
yfb(k)=( yfb(k-1)+yfb(k+1))/2;
xc =(xfb+xft)/2 ;
yc = (yfb+yft)/2;
L=length(xc);
for i=1:L
if xc(i)<0
   yc(i)=0;
end
end




% plot(xfb,yfb,'y', xft,yft,'y', xc, yc,':')
%
% axis([-2 8 -6 6])
% pause


a=0.5;
b=0.5;

dth=pi/8;

th=0:dth:2*pi;
jmax=length(th);
xr=cos(th);

yr=sin(th);
L=length(xc);

for i=1:L
%   xr=cos(th);
%   yr=sin(th);
a = (yft(i)    -yc(i))/(-yfb(i) + yc(i));
b = (-yfb(i)    +yc(i));
for j=1:jmax
       y(i,j)=yr(j)*b+yc(i);
       if th(j)<pi    y(i,j)=yr(j)*b*a + yc(i); end
    x(i,j)=xr(j)*b;
    z(i,j)=xc(i);
end
end
opengl neverselect
surface(z+hyq_x,x+hyq_y,y+hyq_z)
axis([-8 8 -16 16 -16 16])
%mesh(z(1:30,1:11),x(1:30,1:11),y(1:30,1:11))
   hold on
   surfc(z+hyq_x,x+hyq_y,y+hyq_z)
= wing_;
F = 1.7;
xw=F*xw; yw=F*yw; zw=F*zw;
= rotz_(xw,yw,zw,-90);
= rotx_(xw,yw,zw,180);
= rotz_(x2,y2,z2,-270);
surface(x1+2+hyq_x,y1-0.5+hyq_y,z1+ 0.7+hyq_z);
surface(x2+2+hyq_x,y2+0.5+hyq_y,z2+ 0.7+hyq_z);
surface(0.8*x1+6.6+hyq_x,0.5*z1-0+hyq_y,-0.3*y1+1.2+hyq_z)
surface(0.7*x1+6.6+hyq_x,0.3*y1-0.7+hyq_y,0.9*z1+ 0.7+hyq_z);
surface(0.7*x2+6.6+hyq_x,0.3*y2+0.7+hyq_y,0.9*z2+ 0.7+hyq_z);
caxis([-3,1])
grid
% = rotx_(x1,y1,z1,+80)

%axis([-2 8 -6 6 -6 6])
    for(jjjj = 1:10)
    view(3)
    F = getframe(gca);
    mov1 = addframe(mov1,F);
    view()
    F = getframe(gca);
    mov2 = addframe(mov2,F);
    view()
    F = getframe(gca);
    mov3 = addframe(mov3,F);
    view()
    mov4 = addframe(mov4,F);
    end

title('Commuter Airplane')
caxis([-2, 2])
colormap(hsv)
end

mov1 = close(mov1);
mov2 = close(mov2);
mov3 = close(mov3);
mov4 = close(mov4);

[ 本帖最后由 lxq 于 2007-1-1 17:08 编辑 ]

MVH 发表于 2006-11-15 15:48

PLANE1_.m

% plane_ same as List_B4.
% Plots a wire frame airplane.
% Copyright S. Nakamura
set(gcf, 'NumberTitle','off','Name', 'Figure B.3; plane_ ')
%scrsz = get(0,'ScreenSize');
%set(gcf,'Position',)

clc
clear all, clf

mov1 = avifile('airplanh.avi');
for hhh = 0:0.2:50
dth=pi/16;
fuselen=6;
thf=pi:-dth:pi/2;
xa = 0:0.5:fuselen;

hyq_x = 0;
hyq_y = 0;
hyq_z = 0;

xt=fuselen+0.25:0.25:fuselen+2;
dxt = 1.4/(length(xt)-0) ;
yt = -1+dxt:dxt:0.4;
length(yt);


xft= ;

yft= ;


xfb= ;

yfb=[-sin(thf),-ones(size(xa)),yt ] ;
k=length();
yfb(k)=( yfb(k-1)+yfb(k+1))/2;
xc =(xfb+xft)/2 ;
yc = (yfb+yft)/2;
L=length(xc);
for i=1:L
if xc(i)<0
   yc(i)=0;
end
end




% plot(xfb,yfb,'y', xft,yft,'y', xc, yc,':')
%
% axis([-2 8 -6 6])
% pause


a=0.5;
b=0.5;

dth=pi/8;

th=0:dth:2*pi;
jmax=length(th);
xr=cos(th);

yr=sin(th);
L=length(xc);

for i=1:L

a = (yft(i)    -yc(i))/(-yfb(i) + yc(i));
b = (-yfb(i)    +yc(i));
for j=1:jmax
       y(i,j)=yr(j)*b+yc(i);
       if th(j)<pi    y(i,j)=yr(j)*b*a + yc(i); end
    x(i,j)=xr(j)*b;
    z(i,j)=xc(i);
end
end
opengl neverselect



= wing_;
F = 1.7;
xw=F*xw; yw=F*yw; zw=F*zw;
= rotz_(xw,yw,zw,-90);
= rotx_(xw,yw,zw,180);
= rotz_(x2,y2,z2,-270);

aa = -2.8;
z = z+aa;
x3 = x1+2+aa;
y3 = y1-0.5;
z3 = z1+0.7;
x4 = x2+2+aa;
y4 = y2+0.5;
z4 = z2+0.7;
x5 = 0.8*x1+6.6+aa;
z5 = 0.5*z1;
y5 = -0.3*y1+1.2;
x6 = 0.7*x1+6.6+aa;
y6 = 0.3*y1-0.7;
z6 = 0.9*z1+0.7;
x7 = 0.7*x2+6.6+aa;
y7 = 0.3*y2+0.7;
z7 = 0.9*z2+0.7;

% 侧倾角变换
=rotx_(x3,y3,z3,hhh);
=rotx_(x4,y4,z4,hhh);
=rotx_(x5,z5,y5,hhh);
=rotx_(x6,y6,z6,hhh);
=rotx_(x7,y7,z7,hhh);
= rotx_(z,x,y,hhh);

% 俯仰角变换
=roty_(x3,y3,z3,hhh);
=roty_(x4,y4,z4,hhh);
=roty_(x5,z5,y5,hhh);
=roty_(x6,y6,z6,hhh);
=roty_(x7,y7,z7,hhh);
= roty_(z,x,y,hhh);

% 航向角变换
=rotz_(x3,y3,z3,hhh);
=rotz_(x4,y4,z4,hhh);
=rotz_(x5,z5,y5,hhh);
=rotz_(x6,y6,z6,hhh);
=rotz_(x7,y7,z7,hhh);
= rotz_(z,x,y,hhh);

clf
subplot(221)
surface(z+hyq_x,x+hyq_y,y+hyq_z)
axis([-16 16 -16 16 -16 16])

hold on
surfc(z+hyq_x,x+hyq_y,y+hyq_z)


surface(x3+hyq_x,y3+hyq_y,z3+hyq_z);
surface(x4+hyq_x,y4+hyq_y,z4+hyq_z);
surface(x5+hyq_x,z5+hyq_y,y5+hyq_z)
surface(x6+hyq_x,y6+hyq_y,z6+hyq_z);
surface(x7+hyq_x,y7+hyq_y,z7+hyq_z);
grid


subplot(222)
surface(z+hyq_x,x+hyq_y,y+hyq_z)
axis([-16 16 -16 16 -16 16])

hold on
surfc(z+hyq_x,x+hyq_y,y+hyq_z)


surface(x3+hyq_x,y3+hyq_y,z3+hyq_z);
surface(x4+hyq_x,y4+hyq_y,z4+hyq_z);
surface(x5+hyq_x,z5+hyq_y,y5+hyq_z)
surface(x6+hyq_x,y6+hyq_y,z6+hyq_z);
surface(x7+hyq_x,y7+hyq_y,z7+hyq_z);
view()
grid


subplot(223)
surface(z+hyq_x,x+hyq_y,y+hyq_z)
axis([-16 16 -16 16 -16 16])

hold on
surfc(z+hyq_x,x+hyq_y,y+hyq_z)


surface(x3+hyq_x,y3+hyq_y,z3+hyq_z);
surface(x4+hyq_x,y4+hyq_y,z4+hyq_z);
surface(x5+hyq_x,z5+hyq_y,y5+hyq_z)
surface(x6+hyq_x,y6+hyq_y,z6+hyq_z);
surface(x7+hyq_x,y7+hyq_y,z7+hyq_z);
view()
grid


subplot(224)
surface(z+hyq_x,x+hyq_y,y+hyq_z)
axis([-16 16 -16 16 -16 16])

hold on
surfc(z+hyq_x,x+hyq_y,y+hyq_z)


surface(x3+hyq_x,y3+hyq_y,z3+hyq_z);
surface(x4+hyq_x,y4+hyq_y,z4+hyq_z);
surface(x5+hyq_x,z5+hyq_y,y5+hyq_z)
surface(x6+hyq_x,y6+hyq_y,z6+hyq_z);
surface(x7+hyq_x,y7+hyq_y,z7+hyq_z);
view()
grid


caxis([-3,1])

title('Commuter Airplane')
caxis([-2, 2])
colormap(hsv)
F = getframe(gcf);
mov1 = addframe(mov1,F);
end

mov1 = close(mov1);

MVH 发表于 2006-11-15 15:48

PLANE2_.m

function PLANT2_(north, east, height, yaw, pitch, roll)

% plane_ same as List_B4.
% Plots a wire frame airplane.
% Copyright S. Nakamura
% Updated By: Yuing He

set(gcf, 'NumberTitle','off','Name', 'Figure B.3; plane_ ','Position',)

clc

dth=pi/16;
fuselen=6;
thf=pi:-dth:pi/2;
xa = 0:0.5:fuselen;

hyq_x = north;
hyq_y = east;
hyq_z = height;

xt=fuselen+0.25:0.25:fuselen+2;
dxt = 1.4/(length(xt)-0) ;
yt = -1+dxt:dxt:0.4;
length(yt);


xft= ;
yft= ;

xfb= ;
yfb=[-sin(thf),-ones(size(xa)),yt ] ;
k=length();
yfb(k)=( yfb(k-1)+yfb(k+1))/2;

xc =(xfb+xft)/2 ;
yc = (yfb+yft)/2;

L=length(xc);
for i=1:L
if xc(i)<0
   yc(i)=0;
end
end

dth=pi/8;

th=0:dth:2*pi;
jmax=length(th);
xr=cos(th);

yr=sin(th);
L=length(xc);

for i=1:L
a = (yft(i) - yc(i))/(-yfb(i) + yc(i));
b = (-yfb(i) + yc(i));
for j=1:jmax
    y(i,j)=yr(j)*b+yc(i);
    if th(j)<pi    y(i,j)=yr(j)*b*a + yc(i); end
    x(i,j)=xr(j)*b;
    z(i,j)=xc(i);
end
end

= wing_;
F = 1.7;
xw=F*xw; yw=F*yw; zw=F*zw;
= rotz_(xw,yw,zw,-90);
= rotx_(xw,yw,zw,180);
= rotz_(x2,y2,z2,-270);

aa = -2.8;
z = z+aa;
x3 = x1+2+aa;
y3 = y1-0.5;
z3 = z1+0.7;
x4 = x2+2+aa;
y4 = y2+0.5;
z4 = z2+0.7;
x5 = 0.8*x1+6.6+aa;
z5 = 0.5*z1;
y5 = -0.3*y1+1.2;
x6 = 0.7*x1+6.6+aa;
y6 = 0.3*y1-0.7;
z6 = 0.9*z1+0.7;
x7 = 0.7*x2+6.6+aa;
y7 = 0.3*y2+0.7;
z7 = 0.9*z2+0.7;

% 横滚角变换
=rotx_(x3,y3,z3,roll);
=rotx_(x4,y4,z4,roll);
=rotx_(x5,z5,y5,roll);
=rotx_(x6,y6,z6,roll);
=rotx_(x7,y7,z7,roll);
= rotx_(z,x,y,roll);

% 俯仰角变换
=roty_(x3,y3,z3,pitch);
=roty_(x4,y4,z4,pitch);
=roty_(x5,z5,y5,pitch);
=roty_(x6,y6,z6,pitch);
=roty_(x7,y7,z7,pitch);
= roty_(z,x,y,pitch);

% 航向角变换
=rotz_(x3,y3,z3,yaw);
=rotz_(x4,y4,z4,yaw);
=rotz_(x5,z5,y5,yaw);
=rotz_(x6,y6,z6,yaw);
=rotz_(x7,y7,z7,yaw);
= rotz_(z,x,y,yaw);

opengl neverselect
clf
subplot(221)
surface(z+hyq_x,x+hyq_y,y+hyq_z)
axis()

hold on
surfc(z+hyq_x,x+hyq_y,y+hyq_z)
surface(x3+hyq_x,y3+hyq_y,z3+hyq_z);
surface(x4+hyq_x,y4+hyq_y,z4+hyq_z);
surface(x5+hyq_x,z5+hyq_y,y5+hyq_z)
surface(x6+hyq_x,y6+hyq_y,z6+hyq_z);
surface(x7+hyq_x,y7+hyq_y,z7+hyq_z);
grid
title('3 dimention view');

subplot(222)
surface(z+hyq_x,x+hyq_y,y+hyq_z)
axis()

hold on
surfc(z+hyq_x,x+hyq_y,y+hyq_z)
surface(x3+hyq_x,y3+hyq_y,z3+hyq_z);
surface(x4+hyq_x,y4+hyq_y,z4+hyq_z);
surface(x5+hyq_x,z5+hyq_y,y5+hyq_z)
surface(x6+hyq_x,y6+hyq_y,z6+hyq_z);
surface(x7+hyq_x,y7+hyq_y,z7+hyq_z);
view()
grid
title('looking throgh negative x');


subplot(223)
surface(z+hyq_x,x+hyq_y,y+hyq_z)
axis()

hold on
surfc(z+hyq_x,x+hyq_y,y+hyq_z)
surface(x3+hyq_x,y3+hyq_y,z3+hyq_z);
surface(x4+hyq_x,y4+hyq_y,z4+hyq_z);
surface(x5+hyq_x,z5+hyq_y,y5+hyq_z)
surface(x6+hyq_x,y6+hyq_y,z6+hyq_z);
surface(x7+hyq_x,y7+hyq_y,z7+hyq_z);
view()
grid
title('looking throgh negative y');


subplot(224)
surface(z+hyq_x,x+hyq_y,y+hyq_z)
axis()

hold on
surfc(z+hyq_x,x+hyq_y,y+hyq_z)
surface(x3+hyq_x,y3+hyq_y,z3+hyq_z);
surface(x4+hyq_x,y4+hyq_y,z4+hyq_z);
surface(x5+hyq_x,z5+hyq_y,y5+hyq_z)
surface(x6+hyq_x,y6+hyq_y,z6+hyq_z);
surface(x7+hyq_x,y7+hyq_y,z7+hyq_z);
view()
grid
title('looking throgh negative z');


caxis([-3,1])
caxis([-2,2])
colormap(hsv)

end

MVH 发表于 2006-11-15 15:49

move.m

mov1 = avifile('airplan2.avi');

length1=length(height);

for hh = 1:1:length1/10
    hhh = hh*10;
    north1 =- north(hhh) ;
    east1 = east(hhh);
    height1 = height(hhh);
    yaw = psid(hhh);
    pitch = thetad(hhh);
    roll = phid(hhh);
    plane2_(north1,east1,height1,yaw,pitch,roll);
    F = getframe(gcf);
    mov1 = addframe(mov1,F);
    mov1 = addframe(mov1,F);
    mov1 = addframe(mov1,F);
end

mov1 = close(mov1);

MVH 发表于 2006-11-15 15:49

wing_.M

% wing_ generates a wing data used in plane_
% (originally named wing_2d)
% Example:   =wing_; mesh(x,y,z)
% See Figure B.3
function = wing_
x=0:0.1:1;
n=length(x);
for k=1:30

for i=2:n-1
x(i)=0.5*x(i-1)+0.4*x(i+1);
end
end
for i=2:n
x(n+i-1)=x(n-i+1);
end
y=0.2969*sqrt(x) - 0.126*x - 0.3516*x.^2+ ...
      0.2843 * x.^3 - 0.1015*x.^4;
for i=n+1:length(y)
y(i)=-y(i);
end
%axis([-0.5, 1.5 ,-1 1])
%plot(x,y)
jmax=15;
for j=1:jmax
for i=1:2*n-1

xw(i,j)=x(i);
yw(i,j)=y(i);
zw(i,j)=0.3*(j-1);
end

end
yw(:,jmax)=zeros(size(yw(:,jmax)));
zw(:,jmax)=zw(:,jmax-1);

%mesh(zw,xw,yw)
%axis()

MVH 发表于 2006-11-15 15:50

rotx_.M

% rotx_(x,y,z,th) rotates a vector
% th degrees counter-clockwise about
% the x-axis.See Appendix B; List B.1.
function =rotx_(x,y,z,th)
cosf=cos(th*pi/180);sinf=sin(th*pi/180);
xd =x;
yd =cosf.*y - sinf.*z;
zd =sinf.*y + cosf.*z;

MVH 发表于 2006-11-15 15:50

roty_.M

% roty_(x,y,z,th) rotates a vector
% th degrees counter-clockwise about
% the y-axis.See Appendix B; List B.1.
function =roty_(x,y,z,th)
cosf=cos(th*pi/180);sinf=sin(th*pi/180);
yd =y;
xd =cosf.*x + sinf.*z;
zd = - sinf.*x + cosf.*z;

MVH 发表于 2006-11-15 15:50

rotz_.M

% rotz_(x,y,z,th) rotates a vector
% th degrees counter-clockwise about
% the z-axis.See Appendix B; List B.1.
function =rotz_(x,y,z,phi)
cosf=cos(phi*pi/180);sinf=sin(phi*pi/180);
xd =cosf *x - sinf *y;
yd = sinf *x + cosf *y;
zd =z;

xjtu211 发表于 2006-11-15 20:39

哦,挺不错啊,学习研究一下

suffer 发表于 2006-11-20 08:32

原帖由 xjtu211 于 2006-11-15 20:39 发表



该程序还有很多缺陷,希望大家讨论改进

y-y8641 发表于 2006-11-20 15:12

根本就没有办法运行啊

bjydly 发表于 2006-11-21 14:59

晕,论坛的程序为啥都没法运行呢?

suffer 发表于 2006-11-22 10:03

原帖由 y-y8641 于 2006-11-20 15:12 发表



这个我试过,可以运行,不要没搞清楚就随便下判断

suffer 发表于 2006-11-22 10:04

原帖由 bjydly 于 2006-11-21 14:59 发表
晕,论坛的程序为啥都没法运行呢?

可以运行的,只是有一点点小问题

hqhuang 发表于 2006-11-23 15:51

是有点问题,运行后不会自动跳出。
要强制才能跳出,不过我是菜鸟一个不能解决。
页: [1] 2
查看完整版本: 一个用matlab编的可以动的飞机动画制作小程序