Home > リソース > MATLABでアニメーション作成 > 11. 運動する直方体のアニメーション〜回転運動
MATLABでアニメーション作成

11. 運動する直方体のアニメーション〜回転運動

x軸周りに回転運動する直方体のアニメーションを作成するMATLABスクリプトをコード1に示します。また、このスクリプトを実行して得られるアニメーションを動画1に示します。11〜13行目の運動データ「Motion data」の部分以外は前項のスクリプトと全く同じです。

anim_block_rotate.m
% anim_block_rotate.m

clear; close all;

% Block spec
Lx = 0.15;
Ly = 0.05;
Lz = 0.30;

% Motion data
t = (0:0.001:1)';            % Time data
position = [0*t, 0*t, 0*t];  % Position data
angles = [2*pi*t, 0*t, 0*t]; % Orientation data (XYZ Euler angles)

% Initial vertices and faces
r_ini = position(1,:)';
R_ini = eulerXYZ(angles(1,1), angles(1,2), angles(1,3));
[vertices_ini, faces_ini] = calcBlock(r_ini, R_ini, Lx, Ly, Lz);

% Draw initial figure
figure(1);
h = patch('Faces', faces_ini, 'Vertices', vertices_ini, 'FaceColor', 'y');

% Axes settings
xlabel('x'); ylabel('y'); zlabel('z');
axis vis3d equal;
view([-37.5,30]);
camlight;
grid on;
xlim([-0.7,0.7]);
ylim([-0.7,0.7]);
zlim([-0.7,0.7]);

% Compute vertices at each time
vertices = zeros(8,3,length(t));
for i = 1:length(t)
    r = position(i,:)';
    R = eulerXYZ(angles(i,1), angles(i,2), angles(i,3));
    [vertices(:,:,i), ~] = calcBlock(r, R, Lx, Ly, Lz);
end

% Animation Loop
for i = 1:length(t)
    set(h, 'Vertices', vertices(:,:,i));
    drawnow;
end
コード1: 回転運動する直方体のアニメーションを作成するMATLABスクリプト



動画1: 回転運動する直方体のアニメーション
スポンサーリンク