Moving 3D patch object along a trajectory

68 views (last 30 days)
Hello
I created a 3D object with the patch function. I want to move this object along a trajectory, where the coordinates are functions of time. I want to move the object along the trajectory and show the plot as an animation of the object's motion by generating the path coordinates with a for loop, with time being the variable that changes with each iteration. I want to move the object by adding the coordinates of its original vertices to those of the path, dependent on time. I am not sure how to do this though. Any help would be appreciated. Thank you.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Oct 2019
There are three ways of doing this.
  1. Create a patch and record its handle. At each time step, update the patch XData and YData and ZData properties.
  2. Create a patch and record its handle. At each time step, update the Vertices properties
  3. Create hgtransform group and record its handle. Create a patch and set its parent to be the hgtransform group. At each time step, update the transform matrix of the hgtransform group.
The third option would always translate and rotate the patch as a group -- a rigid movement. The second option would encourage keeping the same face connections but would permit the vertices to move comparatively freely within that (e.g., a square could easily become a pentagram.) The first option permits more arbitrary movement of the vertices (but if you add new vertices along the way then you might need to update more properties.)
  1 Comment
Grant Cole
Grant Cole on 14 Oct 2019
Thank you very much for the answer. This was my attempt at an hgtransform, but I still don't have any movement in my object. Any thoughts? Thanks again.
load obj %matrix containing object vertices
ax=axes('Xlim',[-1000 1000],'Ylim',[-1000 1000],'Zlim',[-1000 1000]);
t_max=10;
sz=size(obj);
for i=1:4:sz(1,1)
f=[i i+1 i+2];
h(1)=patch('Faces',f,'Vertices',obj,'FaceColor','green');
end
H=hgtransform('Parent',ax);
set(h,'Parent',H)
set(gcf,'Renderer','opengl')
drawnow
% Arbitrary values used for trajectory
c=1000;
w=50;
f=30;
h=100;
v=300;
a=30;
% Coordinates of the trajectory
t=1:0.1:t_max
for i=1:numel(t)
x(i)=c*t(i)
y(i)=cos(2*pi*f*t(i))*(w-0.75*w*t(i)/t_max)
z(i)=h-v*t(i)+a*(cos(2*pi*(f/2)*t(i)))
end
% pitch and yaw angle acquired using derivatives of the trajectory
for t=1:0.1:t_max
dy=(w.*((6*pi*f*t-8*pi*f*t_max)*sin(2*pi*f*t)-3*cos(2*pi*f*t)))/(4*t_max)
dz=-pi*a*f*sin(pi*f*t)-v
dx=c
pitch_angle=atan2(dz,sqrt((dx)^2+(dy)^2))
yaw_angle=atan2(dy,dx)
end
for j=1:numel(x)
trans=makehgtform('translate',[x(j) y(j) z(j)]);
rot=makehgtform('yrotate',pitch_angle,'zrotate',yaw_angle);
set(H,'Matrix',trans*rot);
pause(0.2)
end
view([107,7]);
grid off;
axis equal

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!