Help with gravity animation!
9 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
abbyeit
il 1 Ott 2021
Commentato: Image Analyst
il 3 Ott 2021
Hi,
I have been using MATLAB for a couple weeks now and I would love to try to animate something to do with gravity.
I have tried to animate a line (e.g. canonball) thrown starting from x = 0 and y = 0, (0,0), with a beginning velocity and throwing angle.
I tried to animate the line starting from (0,0) until it hits the ground but when I save and press Run, the figure opens but nothing happens.
Can someone help me understand what I am doing wrong? I have no clue what I am doing.
function animation = gravity()
g = 9.82; %g-constant
v0 = 35; %starting velocity
alpha = 50; %angle
picNr = 0; %picture number starts at 0 (for animation)
y = @(t)v0*t*sind(alpha)-0.5*g*t.^2; %throwing motion y-axis
x = @(t)v0*t*cosd(alpha); %throwing motion x-axis (constant velocity)
T = v0*sind(alpha)/(0.5*g); %time it takes for the "ball" to hit the ground
for t = linspace(0,T,100)
picNr = picNr + 1;
plot(x(t),y(t),'b--','LineWidth',5,'MarkerSize',12);
axis([-5 150 -5 45]);
animation(picNr) = getframe;
end
Any help would be deeply appreciated!
0 Commenti
Risposta accettata
Image Analyst
il 1 Ott 2021
Try this:
g = 9.82; %g-constant
v0 = 35; %starting velocity
alpha = 50; %angle
T = v0*sind(alpha)/(0.5*g); %time it takes for the "ball" to hit the ground
t = linspace(0,T,100);
y = v0*t*sind(alpha)-0.5*g*t.^2; %throwing motion y-axis
x = v0*t*cosd(alpha); %throwing motion x-axis (constant velocity)
for k = 1 : length(t)
plot(x(1:k),y(1:k),'r--','LineWidth',2);
hold on;
hPlot = plot(x(k),y(k),'b.','LineWidth',5,'MarkerSize', 50);
axis([-5 150 -5 45]);
caption = sprintf('Frame %d of %d', k, length(t));
title(caption);
drawnow;
grid on;
pause(0.2);
delete(hPlot); % Clear last marker
end
grid on;
g = gcf;
g.WindowState = 'maximized'
2 Commenti
Image Analyst
il 3 Ott 2021
I'll let you do the thought and modifications necessary to add gravity between the projectiles - I'd just have to do the same thing so I'll let you do it since it's your problem.
Another demo I can offer is my projectile demo that computes just about everything you could want to know about a projectile (assumes no drag). It's attached.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Animation in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!