How can I let my object repeat over time when animating it?

조회 수: 2 (최근 30일)
María Paulina Pantoja Gavidia
María Paulina Pantoja Gavidia 2022년 6월 7일
답변: Simon Chan 2022년 6월 7일
Hello, I am trying to animate a 3d object with the information from the arduino serial port, but the object only appears in another position and the past is not removed, just like this:
Any idea how to make only the figure move without it reappearing? here is the code
clc
for i = 1:20
delete(instrfind({"Port"},{"COM6"}));
micro=serial("COM6");
micro.BaudRate=9600;
warning("off","MATLAB:serial:fscanf:unsuccesfulRead");
fopen(micro)
savedData = fscanf(micro,"%s");
v = strsplit(savedData, ',');
ra = str2double(v(7));
pa= str2double(v(6));
ya= str2double(v(1));
offset_3d_model=[0, 0, 0];
sb= "F22jet.stl";
[Model3D. rb.stl_data.vertices, Model3D.rb.stl_data.faces,~,~]= stlRead(sb);
Model3D.rb.stl_data.vertices= Model3D.rb.stl_data.vertices-offset_3d_model;
AC_DIMENSION = max(max(sqrt(sum(Model3D.rb.stl_data.vertices.^2,2)))) ;
AX=axes("position",[0.0 0.0 1 1]);
axis off
scrsz = get(0,"ScreenSize");
set(gcf,"Position",[scrsz(3)/40 scrsz(4)/12 scrsz(3)/2*1.0 scrsz(3)/2.2*1.0], "Visible","on");
set(AX,"color","none");
axis("equal")
hold on;
cameratoolbar("Show")
AV_hg = hgtransform("Parent",AX,"tag","ACRigidBody");
for j=1:length(Model3D.rb)
AV = patch(Model3D.rb(j).stl_data, "FaceColor", [0 0 1], ...
"EdgeColor", "none", ...
"FaceLighting", "gouraud", ...
"AmbientStrength", 0.15, ...
"Parent", AV_hg);
end
axis("equal");
axis([-1 1 -1 1 -1 1] * 1.0 * AC_DIMENSION)
set(gcf,"Color",[1 1 1])
axis off
view([30 10])
camlight("left");
material("dull");
M=makehgtform("xrotate",ra);
M2=makehgtform("yrotate",pa);
set (AV_hg, 'Matrix', M);
set (AV_hg, 'Matrix', M);
drawnow
delete(micro);
end

답변 (1개)

Simon Chan
Simon Chan 2022년 6월 7일
Try to preallocate 'AV' before the for loop and change the data each time inside the for loop:
AV = patch('Faces',1,'Vertices',[NaN NaN], "FaceColor", [0 0 1], ...
"EdgeColor", "none", ...
"FaceLighting", "gouraud", ...
"AmbientStrength", 0.15, ...
"Parent", AV_hg);
%
for j=1:length(Model3D.rb)
set(AV,'Faces', Model3D.rb.stl_data.faces,'Vertices',Model3D.rb.stl_data.vertices)
end

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by