Home > Resources > Making Animation with MATLAB > 9. Animation of a moving block - Preparation
Making Animation with MATLAB

9. Animation of a moving block - Preparation

The basic idea of animation of a rigid body motion is the same as that of animation of a moving point discussed in Section 2 and 3. The only difference is that in the case of a rigid body motion the object to draw and erase is a three-dimensional shape.

Let us make an animation of a moving block as an example. This can be done based on the script developed in Section 6 for drawing a block shape in a general configuration.

First, in order to make the script concise let us define a function that returns vertices and faces of a block shape as in Code 1.

calcBlock.m
function [vertices, faces] = calcBlock(r, R, Lx, Ly, Lz)
% Return vertices and faces of a block geometry

vertices_0 = [
0,   0,  0;  % #1
Lx,  0,  0;  % #2
0,  Ly,  0;  % #3
0,   0, Lz;  % #4
Lx, Ly,  0;  % #5
0,  Ly, Lz;  % #6
Lx,  0, Lz;  % #7
Lx, Ly, Lz]; % #8

vertices = r' + vertices_0*R';

faces = [
1, 2, 5, 3;  % #1
1, 3, 6, 4;  % #2
1, 4, 7, 2;  % #3
4, 7, 8, 6;  % #4
2, 5, 8, 7;  % #5
3, 6, 8, 5]; % #6

end
Code 1: MATLAB function that returns the vertices and the faces of a block shape

Using the calcBlock() function in Code 1, we can rewrite the script developed in Section 6 for drawing a block shape in a general configuration concisely as in Code 2 below.

make_block.m
% make_block.m

clear; close all;

% Reference position
r = [1; 1; 1];

% Reference orientation
R = eulerXYZ(-pi/3, 0, pi/6);

% Side lengths
Lx = 0.15;
Ly = 0.05;
Lz = 0.30;

% Vertices and faces
[vertices, faces] = calcBlock(r, R, Lx, Ly, Lz);

% Draw patch
figure(1);
h = patch('Faces', faces, 'Vertices', vertices, 'FaceColor', 'y');

% Axes settings
xlabel('x'); ylabel('y'); zlabel('z');
axis vis3d equal;
view([-37.5,30]);
camlight;
grid on;
xlim([0.8,1.3]);
ylim([0.9,1.4]);
zlim([0.8,1.3]);
Code 2: MATLAB script to draw a block shape

Sponsor Link