How to make this truck move across the bridge in an animation?

15 views (last 30 days)
function lorry=Lorry_plot
figure
view(3)
grid on
axis ('equal');
% lorry
vertices_lorry = [0 1 0.5 ; 4 1 0.5; 6.5 1 0.5; 6.5 1 1.2; 6 1 1.3;...
5 1 1.8; 4 1 1.8; 4 1 2; 0 1 2; ... % postive y of lorry
0 -1 0.5; 4 -1 0.5; 6.5 -1 0.5; 6.5 -1 1.2; 6 -1 1.3;...
5 -1 1.8; 4 -1 1.8; 4 -1 2; 0 -1 2]; % negative y of lorry
faces_lorry_cargo = [10 1 9 18; 1 2 8 9; 8 9 18 17; 10 11 17 18;...
7 8 17 16; 1 2 11 10];
faces_lorry_driver = [7 6 15 16 16 16; 5 6 15 14 14 14; 5 4 13 14 14 14;...
3 4 13 12 12 12; 2 3 4 5 6 7; 11 12 13 14 15 16; 2 3 12 11 11 11];
lorry(1)=patch('Faces',faces_lorry_cargo,'Vertices',vertices_lorry,'FaceColor',...
'#86DA7D','EdgeColor','k');
lorry(2)=patch('Faces',faces_lorry_driver,'Vertices',vertices_lorry,'FaceColor',...
'#868686','EdgeColor','k');
% wheels
t = linspace(pi,2*pi,8)';
[a,b] = pol2cart(t,0.5); % create data for circle
v0 = a*0;
X = [v0 a a v0];
Z = [v0 b b v0];
Y = [v0-1 v0-1 v0+1 v0+1]*0.15;
hold on
lorry(3)=surf(X+1,Y+.85,Z+.5,'facecolor','#555555'); % draw wheel rear left
lorry(4)=surf(X+1,Y-.85,Z+.5,'facecolor','#555555'); % draw wheel rear right
lorry(5)=surf(X+4.95,Y+.85,Z+.5,'facecolor','#555555'); % draw wheel front left
lorry(6)=surf(X+4.95,Y-.85,Z+.5,'facecolor','#555555'); % draw wheel front right
hold off
% truss
vertices_truss = [0 2 0; 4 2 0; 8 2 0; 12 2 0; 16 2 0; 20 2 0; 24 2 0;...
20 2 4; 16 2 4; 12 2 4; 8 2 4; 4 2 4;... % pink truss positive y
0 -2 0; 4 -2 0; 8 -2 0; 12 -2 0; 16 -2 0; 20 -2 0; 24 -2 0;...
20 -2 4; 16 -2 4; 12 -2 4; 8 -2 4; 4 -2 4]; % blue truss negative y
faces_truss = [1 2;2 3;3 4;4 5;5 6;6 7;7 8;8 9;9 10;10 11;11 12;12 1;12 2;12 3;...
11 3;11 4;10 4;9 4;9 5;8 5;8 6;... %pink side
13 14;14 15;15 16;16 17;17 18;18 19;19 20;20 21;21 22;22 23;23 24;...
24 13;24 14;24 15;23 15;23 16;22 16;21 16;21 17;20 17;20 18;... %blue side
1 13;2 14;3 15;4 16;5 17;6 18;7 19;8 20;9 21;10 22;11 23;12 24]; %transverse
patch('Faces',faces_truss,'Vertices',vertices_truss,'FaceAlpha',0);
end
This file plots a truck and a truss. How can I make this into an animation so the truck(lorry) moves across the bridge in increments of 0.01?

Accepted Answer

Adam Danz
Adam Danz on 31 Mar 2020
Edited: Adam Danz on 31 Mar 2020
First of all, nice lorry (slow-clap).
Here's how to make it drive forward along the x axis. You could add a Y-component of motion very easily.
% What is the final position of the front
% of the lorry along the x-axis?
maxX = 35;
% How far should the lorry travel on each frame/loop?
% For faster velocity, use higher value.
stepSize = 0.2;
% Advance the lorry in steps of "stepSize" until
% the front of the lorry reaches "maxX".
arrived = false;
while ~arrived
lorry(1).Vertices(:,1) = lorry(1).Vertices(:,1) + stepSize;
lorry(2).Vertices(:,1) = lorry(2).Vertices(:,1) + stepSize;
lorry(3).XData = lorry(3).XData + stepSize;
lorry(4).XData = lorry(4).XData + stepSize;
lorry(5).XData = lorry(5).XData + stepSize;
lorry(6).XData = lorry(6).XData + stepSize;
drawnow()
% use pause() to slow the velocity
%pause(0.2)
% Determine if the next step would overshoot target
arrived = max(lorry(2).Vertices(:,1)) + stepSize > maxX;
end

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!