how can i move the object along a path?
20 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
clear all;
close all;
clc;
vA = [2.31; -0.55]; % initial velocity OF 1
dt = 0.1; % time step for animation.
%t = 0:dt:10 ;
t = linspace(0,2*pi) ;
% get points trajectory
%pA = zeros(2,length(t)+1) ;
pA(:,1) = [4 ;10] ;% initial position
yp = sin(pi*t) ;
for i = 2:length(t)+1 % t is time variable
% plot(pA(1),pA(2),'ro') % Plot circle
%pA(:,i) = pA(:,i-1) + vA*dt; % update position
pA(:,i) = yp(:,i-1) + vA*dt; % update position
%pB(:,i) = pB(:,i-1) + vB*dt; % update position
end
%plot
title('Motion animation');
%xlabel('x (m)');
%ylabel('y (m)');
h = plot(pA(1,2),pA(1,1),'cs') ;
%h = plot(pB(1,1),pB(1,2),'gp') ;
axis([min(pA(2,:)) max(pA(2,:)) min(pA(1,:)) max(pA(1,:)) ])
for i = 1:length(pA)
set(h,'XData',pA(1,i),'YData',pA(2,i))
%set(h,'XData',pB(1,i),'YData',pB(2,i))
pause(.5)
end
i have a code with a particle moving along undefined path.i tried editing the code for thr e particle to move alon sine wave graph.errors are occuring.could anyone help me to edit my code to move the particle along a sine wave with constant velocity?
0 Kommentare
Antworten (1)
Adam Danz
am 3 Sep. 2019
Bearbeitet: Adam Danz
am 6 Sep. 2019
"errors are occuring.could anyone help me to edit my code"
The code you provided runs without errors. However, the animated object leaves the axis limits. To fix that, the axis limits should be:
axis([min(pA(1,:)) max(pA(1,:)) min(pA(2,:)) max(pA(2,:)) ])
"could anyone help me to edit my code to move the particle along a sine wave with constant velocity?"
Here's a demo.
% Creat the trajectory
t=0:0.1:2*pi;
y=sin(t);
% Set up axes
axh = axes();
hold(axh,'on')
axis([0 2*pi -1 1]); %set axis limits ahead of time
xlabel('t');
ylabel('y');
% plot empty line objects (with NaN values)
line1 = plot(axh,t, nan(size(t)),'-','Color','r');
marker1 = plot(axh,nan,nan,'o','Color','r');
% loop through t and make each point appear
for k=1:numel(t)
%marker plots
marker1.XData = t(k);
marker1.YData = y(k);
%Line plots
line1.YData(1:k) = y(1:k);
drawnow();
% pause(0.05) %if you want to slow it down
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Animation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!