[討論]如何將中間那根圓柱繞軸做旋轉

作者: q6q5q4q3q2q1 (小正妹 )   2015-09-30 16:10:23
如題~
我現在畫的是一個動畫
題目大概就是
下方有一個障礙物(為兩個立方體構成)
上方有一個立方體(可移動)
在立方體的正下方中間有一個圓柱(可用方柱代替)和上方立方體的邊是貼齊的
然後當上方物件移動時,圓柱也會跟著移動
且圓柱會繞y軸或x軸旋轉,直到碰到下方障礙物體就停止。
現在已經建構出以上所說的模型了,然後也可以讓物體移動
但是現在就是不知道要怎麼樣讓圓柱(或方柱)繞軸做旋轉TT
還有碰到障礙物停止
有大大可以和我一起討論嗎!
這是我的code,有三個部分,有些也是我上網慢慢拼湊而來的。
1.title moving box
%% 邊界
axis=[-10,10,-10,10,-10,10];
boxplot3(0,0,0,20,20,30);
%% 視角改變
rotate3d ;
%% 畫圖
%起使位置作標
x1=2; y1=10; z1=25; lax=2; lay=2; laz=2;
r=0.1; h=5; hit=0;
%宣告碰撞物
boxplot3(9.5,9.5,4,1,1,1); %下面(上)
boxplot3(8,8,0,4,4,4); %下面(底)
%宣告探測物
boxplot3(x1-(lax/2),y1-(lay/2),z1,lax,lay,laz); %上面
circle_stick3(x1,y1,z1-h,r,h); %上面圓柱
%% 迴圈for moving
m=0.5; %精細度、誤差(cm)
delx=m*1; delz=0; mot=1; xlim=10; zlim=11;
while hit == 0,
if x1>=xlim && z1>=zlim , mot=2;
end
if z1<zlim , mot=3;
end
switch mot
case 1
delx=m*1; delz=0;
case 2
delx=0; delz=m*-1;
case 3
hit=1;
end
x1=x1+delx;
z1=z1+delz;
clf;
axis=[-10,10,-10,10,-10,10];
boxplot3(0,0,0,20,20,30);
boxplot3(9.5,9.5,4,1,1,1); %下面(上)
boxplot3(8,8,0,4,4,4); %下面(底)
boxplot3(x1-(lax/2),y1-(lay/2),z1,lax,lay,laz); %上面
circle_stick3(x1,y1,z1-h,r,h); %上面圓柱
pause(0.01); %暫停0.005秒(如不暫停無法看到影像)
hold on;
grid on;
end
2.title box _plot3
function boxplot3(x0,y0,z0,Lx,Ly,Lz)
%(x0,y0,z0)是原點的位置; (Lx,Ly,Lz)是立方體的三高.
x=[x0 x0 x0 x0 x0+Lx x0+Lx x0+Lx x0+Lx];
y=[y0 y0 y0+Ly y0+Ly y0 y0 y0+Ly y0+Ly];
z=[z0 z0+Lz z0+Lz z0 z0 z0+Lz z0+Lz z0];
index=zeros(6,5);
index(1,:)=[1 2 3 4 1];
index(2,:)=[5 6 7 8 5];
index(3,:)=[1 2 6 5 1];
index(4,:)=[4 3 7 8 4];
index(5,:)=[2 6 7 3 2];
index(6,:)=[1 5 8 4 1];
for k=1:6
plot3(x(index(k,:)),y(index(k,:)),z(index(k,:)),'r','linewidth',0.5)
hold on;
grid on;
axis square;
end
xlabel('X','FontSize',12,'FontWeight','bold','Color','b')
ylabel('Y','FontSize',12,'FontWeight','bold','Color','b')
zlabel('Z','FontSize',12,'FontWeight','bold','Color','b')
將兩個程式各建立一個script之後 就可以執行了

Links booklink

Contact Us: admin [ a t ] ucptt.com