필터 지우기
필터 지우기

how to put a function inside animation

조회 수: 4 (최근 30일)
Meriem Boukhaima
Meriem Boukhaima 2017년 1월 7일
댓글: Walter Roberson 2017년 1월 7일
Hi, I have to program a bouncing ball using a function I created before called drawball, now I can't implement this function inside my program, it only shows me a big ball, here are the scripts I used:
function drawdisc(x0,y0,r,c)
t=0:0.01:2*pi;
x=r*cos(t)+x0;
y=r*sin(t)+y0;
fill(x,y,c)
end
the bouncing ball script:
clc;
initpos=0; %Ball's initial vertical position
initvel=20; %Ball's initial vertical velocity
r_ball=1; %Ball's radius
gravity=10; %Gravity's acceleration
c_bounce=0.85; %Bouncing's coefficient of elasticity
dt=0.0125; %Animation timestep;
pos=r_ball; %Ball's current vertical position
vel=initvel;%Ball's current vertical velocity
axis([0 20 0 25])
while 1
pos=pos+(vel*dt); %Ball's current vertical position
vel=vel-(gravity*dt); %Ball's current vertical velocity
if pos<0
vel=-vel*c_bounce; %Balls' current vertical velocity
end
drawball(1,pos,1,'b')
pause(0.01)
end
your help is appreciated!!

채택된 답변

Walter Roberson
Walter Roberson 2017년 1월 7일
You define a function named "drawdisc" but you call upon "drawball" in your code.
You call upon drawball(1,pos,1,'b') in your code. It would make more sense to call upon drawball(1,pos,r_ball,'b')
If you the Image Processing Toolbox and a newer MATLAB you might want to consider using viscircles() instead of your own routine. You might also want to consider using scatter() with a marker size and with 'filled'. Or you might want to use plot() with 'o' as the marker and with a MarkerSize and MarkerFaceColor argument.
  댓글 수: 2
Meriem Boukhaima
Meriem Boukhaima 2017년 1월 7일
Thank you Walter for your response, Excuse me I made a mistake, it's supposed to be called drawball instead of drawdisc, but it's the same code, the thing that the exercise says I have to creat a function drawing a ball and then implement it in the animation, so I can't use any built in functions to draw a ball. so when I run the program I see the ball moving but the axes are not fixed, means I see only a big ball, and a moving y-axis.
Walter Roberson
Walter Roberson 2017년 1월 7일
You need to either "hold on" or set the axes xlim and ylim (which automatically sets xlimmode and ylimmode to manual). Make sure you do that after each time you draw the ball. (Better yet would be for your drawing routine to detect that the ball had already been drawn and to update its position instead of creating a new drawing, but even that requires that you had set xlim and ylim)

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

추가 답변 (0개)

카테고리

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