필터 지우기
필터 지우기

Bouncing Ball Animation Getframe

조회 수: 7 (최근 30일)
Gabrielle Bartolome
Gabrielle Bartolome 2020년 10월 31일
댓글: Alan Stevens 2020년 11월 1일
I don't understand what I am doing wrong. I have to create an animation of a ball bouncing. But I have to repeat a for loop for each bounce. It also plays the animation twice. Is there a simpler way of doing this? Also, is there a way to speed up the code so that the animation does not play so slowly? I was thinking that I could write a function that would call it to repeat the animation but I am not sure.
%%Animation
clc, clf, clear
g = 9.81;
theta0 = 45*pi/180;
v0=5;
t(1) = 0; x = 0; y = 0;
plot(x,y, 'o', 'MarkerFaceColor','b', 'MarkerSize', 8)
axis([0 8 0 .8]);
dt = 1/128;
M(1) = getframe
for j = 2:100
t(j) = t(j - 1) + dt;
x = v0*cos(theta0)*t(j);
y = v0*sin(theta0) * t(j) - 0.5*g*t(j)^2;
plot(x,y, 'o', 'MarkerFaceColor', 'b','markerSize', 8);
axis([0 8 0 .8]);
M(j)= getframe;
end
M(2) = getframe
t(100) = 0;
v0 = v0*.8;
for j = 101:198
t(j) = t(j - 1) + dt;
x = v0*cos(theta0)*t(j)+2.5;
y = v0*sin(theta0) * t(j) - 0.5*g*t(j)^2;
plot(x,y, 'o', 'MarkerFaceColor', 'b','markerSize', 8);
axis([0 8 0 .8]);
M(j)= getframe;
end
M(3) = getframe
v0 = v0*.8;
t(198) = 0;
for j = 199:300
t(j) = t(j - 1) + dt;
x = v0*cos(theta0)*t(j)+4;
y = v0*sin(theta0) * t(j) - 0.5*g*t(j)^2;
plot(x,y, 'o', 'MarkerFaceColor', 'b','markerSize', 8);
axis([0 8 0 .8]);
M(j)= getframe;
end
M(4) = getframe
v0 = v0*.8;
t(300) = 0;
for j = 199:300
t(j) = t(j - 1) + dt;
x = v0*cos(theta0)*t(j)+4;
y = v0*sin(theta0) * t(j) - 0.5*g*t(j)^2;
plot(x,y, 'o', 'MarkerFaceColor', 'b','markerSize', 8);
axis([0 8 0 .8]);
M(j)= getframe;
end
movie(M, 1, 1000)

채택된 답변

Alan Stevens
Alan Stevens 2020년 11월 1일
편집: Alan Stevens 2020년 11월 1일
Like this?
g = 9.81;
theta0 = 45*pi/180;
v0=5;
dt = 1/128;
ntimes = 400;
tend = ntimes*dt;
t = linspace(0,tend,ntimes);
vx = v0*cos(theta0);
vy = v0*sin(theta0);
x = 0; y = 0;
xmax = vx*tend;
plot(x,y, 'o', 'MarkerFaceColor','b', 'MarkerSize', 8)
axis([0 xmax 0 .8]);
M(1) = getframe;
treset = 0;
for i = 1:ntimes
x = vx*t(i);
tm = t(i) - treset;
y = vy * tm - 0.5*g*tm^2;
plot(x,y, 'o', 'MarkerFaceColor', 'b','markerSize', 8);
axis([0 xmax 0 .8]);
M(i)= getframe;
if y<0
y = 0;
vy = 0.8*vy;
treset = t(i);
end
end
close
pause(1)
movie(M, 1, 50)
  댓글 수: 4
Gabrielle Bartolome
Gabrielle Bartolome 2020년 11월 1일
What does tend represent? Is that when y = 0? or when it hits the ground?
Alan Stevens
Alan Stevens 2020년 11월 1일
It's just the total time. Just change the number of steps to change the total time.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 11월 1일
See my attached demo for creating an animation from figures. You can set the frame rate for the VideoWriter.

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by