Trajectory animation using coordinate points

I have set of x, y and z coordinates of two objects and I want to create animation of the object's 3D trajectory/motion using these coordinates. Consider the first coordinate in the set as the initial position. Can anyone help me with this.

 Accepted Answer

Animation it is just changing of images like in cartoons
plot(x,y,z)
hold on
for i = 1:length(x)
h = plot(x(i),y(i),z(i),'^r'); % draw something on the trajectory
pause(0.2) % wait a minute
delete(h) % delete it
end
hold off

More Answers (1)

David K.
David K. on 30 Aug 2019
Edited: David K. on 3 Sep 2019
I would do it like this:
% x1,x2,y1,y2,z1,z2 are your coordinate vectors
tstep = .1; % the amount of time between each value in the vectors
figure(1)
for n = 1:length(x1)
plot3(x1(1:n),y1(1:n),z1(1:n),'+-',x2(1:n),y2(1:n),z2(1:n),'+-')
title((n-1)*tstep); % label the time of each step
pause(.01)
end
If the default view of the plot is bad mess around with the view function to try and get a better one.
This will make an animation with both, to make the other two animations simply repeat it and leave out the xyz coordinates of the one you do not want to look at.
edit: fixed so it should actually work now

6 Comments

Where is pause()?
Tomer
Tomer on 31 Aug 2019
Edited: Tomer on 31 Aug 2019
I have tried the above lines but, it does not show any animation movie. I see only a 3D plot. I have the positions like this for Object #1:
x=[638.016117211618 624.691932895787 609.646603336688 593.114927471080 576.907825283452 561.619466401488 546.716404618476 531.647420233970 516.624166072581 501.593437207985 486.365854050800];
y=[143.129764054680 143.932175139856 145.128136117631 146.685057434428 148.205450798430 149.538266400937 150.798709485508 152.113912294220 153.430642102923 154.752576062911 156.115509903965];
z=[333.930739404813 333.463020996104 332.842247871420 332.564180603350 333.488318263397 336.019914862000 338.681315466773 339.985481745858 340.280053945105 340.291332963938 340.350712454917];
The first point in x,y,z is the starting point/location and the last point in x,y,z is the end point/location. The time between each transition is 0.02 sec.
I have something similar for Object #2. I want to create three animations (two individual) and one combined for these two objects.
Tomer
Tomer on 31 Aug 2019
Edited: Tomer on 31 Aug 2019
I mean, I want to make a 3D animation movie of the object (fish) traveling through these points.
Can you please show something? Did you try something?
Tomer
Tomer on 1 Sep 2019
Edited: Tomer on 1 Sep 2019
See, the attachment
tstep = .1; % the amount of time between each value in the vectors
figure(1)
for n = 1:length(x)
plot3(x,y,z,'+-')
title((n-1)*tstep); % label the time of each step
pause(.01)
end
I could see only the plot but, not the animation video.
Oh woops the plot3 should actually be this, i've also updated my original answer with it.
plot3(x(1:n),y(1:n),z(1:n),'+-')
Then, the axis will auto update as the values are added. If you do not want the line change '+-' to '+'
You also may need to add this to it to hold the axis positions.
xlim([lowX upX]);ylim([lowY upY]);zlim([lowZ upZ]);

Sign in to comment.

Categories

Asked:

on 30 Aug 2019

Edited:

on 3 Sep 2019

Community Treasure Hunt

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

Start Hunting!