Help with gravity animation!

6 views (last 30 days)
abbyeit
abbyeit on 1 Oct 2021
Commented: Image Analyst on 3 Oct 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!

Accepted Answer

Image Analyst
Image Analyst on 1 Oct 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 Comments
abbyeit
abbyeit on 3 Oct 2021
Edited: abbyeit on 3 Oct 2021
WOW! Thank you so much!
I have been playing around with this code for the past 2 days you helped me with, learning alot!
I've tried simplifying to a point where it was simple enough for me to understand, but while the code still worked.
I have tried making the ball go out at a randomised angle, and then created another animation in the same for-loop where another ball also plays at the same time, it works!
This is the code I have come up with so far:
g = 9.82;
v0 = 35;
alpha = rand(1)*180; %randomized angle for ball 1
beta = rand(1)*180; %randomized angle for ball 2
t = linspace(0,15,150); %time
y1 = v0*t*sind(alpha)-0.5*g*t.^2; %variables/equation for ball 1
x1 = v0*t*cosd(alpha);
y2 = v0*t*sind(beta)-0.5*g*t.^2; %variables/equation for ball 2
x2 = v0*t*cosd(beta);
for k = 1 : length(t)
plot(x1(1:k),y1(1:k),'r--','LineWidth',2); %plotting ball 1
hold on;
plot(x2(1:k),y2(1:k),'g--','LineWidth',2); %plotting ball 2
hold on;
aPlot = plot(x1(k),y1(k),'b.','LineWidth',5,'MarkerSize', 50);
bPlot = plot(x2(k),y2(k),'k.','LineWidth',5,'MarkerSize', 50);
pause(0.015);
delete(aPlot); % Clears last marker ball 1
delete(bPlot); % Clears last marker ball 2
axis([-150 150 -45 45]);
end
clear; clc;
The goal with this code is to eventually be able to make the balls attract/gravitize to one another after being thrown out and then becoming "one" ball/planet, sort of like "BigBang", rather than the balls just falling down. I was thinking that maybe it would work with Newton's Law of Gravity formula, but I am not sure. Would you know how to code/approach such an animation?
You CERTAINLY have helped me tremendously already and definitely you should not feel obligated to help me further.
Again, thank you so much for the code!
Image Analyst
Image Analyst on 3 Oct 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!