Animation of multiple objects(with hgtransform)

1 view (last 30 days)
Fat Bos
Fat Bos on 20 Mar 2017
Answered: Karan Singh on 20 Feb 2025
Hello, I have a 3d plot with 2 spheres, a plane and a partial circle(arc). One of the spheres(sphere1) is supposed to "hold" the arc. Sphere1 pierces the arc through the plane from above and rotates it until the tip comes out again. Meanwhile sphere2 moves to the plane above the piercing point of the tip and "grabs" the upper end of the arc, sphere1 lets go and sphere 2 rotates/pulls the arc above the plane. I tried using hgtransform, but I get following error message :
Error using matlab.graphics.primitive.Data/set Invalid or deleted object.
Error in VerzweifelterVersuchvX (line 50) set(h,'Parent',t1);
And I am not entirely sure how to move two objects independently
Here is my code so far
clf;
figure(1)
rotate3d on
ax = axes('XLim',[0,50],'YLim',[-20,20],'ZLim',[-20,20]);
grid on
axis equal;
view(3);
hold on
xlabel('x')
ylabel('y')
zlabel('z')
%the plane
patch([15 -15 -15 15], [15 15 -15 -15], [0 0 0 0],'red');
hold on
% the arc
r=4;
teta=-pi:0.01:0;
xn=r*cos(teta);
yn=r*sin(teta);
h(1)=plot3(zeros(1,numel(xn)),xn-0.5,yn+5,'green');
hold on
% the spheres
[xs ,ys, zs]=sphere;
h(2)=surf(xs,ys+3,zs+5,'FaceColor','green');
hold on
h(3)=surf(xs,ys-3,zs+5,'FaceColor','blue');
hold on
t1 = hgtransform('Parent',ax);
t2 = hgtransform('Parent',ax);
set(h,'Parent',t1);
drawnow
% rotate sphere1 holding the arc
% Form z-axis rotation matrix
Rz = makehgtform('zrotate',r1);
% Set transforms for both transform objects
set(t1,'Matrix',Rz)
drawnow

Answers (1)

Karan Singh
Karan Singh on 20 Feb 2025
As the error states that you are trying to modify a graphics object that has already been deleted or is invalid. One common cause with your approach is that when you create your objects (for example, when you try to change their parent with "hgtransform"), the objects may be removed from the axes by subsequent plotting calls. A suggested solution can be to “hold on” your axes before you plot the objects or set their parent properties. Something like-
ax = axes(...);
hold(ax, 'on'); % prevents axes clearing
% ... create your objects
Karan

Community Treasure Hunt

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

Start Hunting!