Home > Resources > Making Animation with MATLAB > 11. Animation of a moving block - Rotational motion
Making Animation with MATLAB

11. Animation of a moving block - Rotational motion

A MATLAB script to make an animation of a block in a rotational motion about the x-axis is shown in Code 1. The animation produced by this script is shown in Movie 1. This script is the same as the one in the last section except the part "Motion data" in the rows 11-13.

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
Code 1: MATLAB script to make an animation of a block in a rotational motion



Movie 1: Animation of a block in a rotatinal motion
Sponsor Link