[讨论]如何将中间那根圆柱绕轴做旋转

楼主: 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