How do I replace an image on a graph and replace it with a new image, whilst keeping all other images in their place?
Mostra commenti meno recenti
I am making a basic game on matlab as part of an assignment and have decided to do a simple basketball simulation, where a user chooses the power and angle at which to throw the ball towards a net. The path of the ball is modelled in a for loop, in which the initial position of the ball is drawn, then the next position a small time later is calculated and drawn. The problem that I am having is that, because I have hold on, each of the previous images of the ball remains on the graph, instead of there being a continuous movement of the basketball. I was wondering how I could fix this? I have included an image below to help illustrate what I mean.

Risposta accettata
Più risposte (1)
Depending on how you are drawing the ball, you may be able to update it's position rather than drawing a new one. If not, you could delete the old one then draw a new one:
% set up the static image
hold on
ball_handle = draw_ball(init_pos);
for ind=1:100
delete(ball_handle)
% calculate new position
ball_handle = draw_ball(pos);
end
hold off
What function are you using to draw?
14 Commenti
Walter Roberson
il 16 Feb 2020
I agree with the recommendation to update the position of the ball rather than draw a new one.
Sq
il 16 Feb 2020
Sq
il 16 Feb 2020
Sq
il 16 Feb 2020
Sindar
il 16 Feb 2020
Can you include the actual line of code where you draw the ball?
Sq
il 16 Feb 2020
Walter Roberson
il 16 Feb 2020
Change
ball_handle = drawshapeNew(translatedBall,'m','f');
to
if k == 1
ball_handle = drawshapeNew(translatedBall,'m','f');
else
set(ball_handle, 'XData', translatedBall(1,:), 'YData', translatedBall(2,:));
end
Sindar
il 16 Feb 2020
Where and how were you deleting the old ball?
Sq
il 16 Feb 2020
Sq
il 16 Feb 2020
Sindar
il 16 Feb 2020
Instead of drawing a new ball, you update the location of the old
Sindar
il 16 Feb 2020
An unrelated note: fill can create unfilled polygons as well:
fill(x,y,c,'EdgeColor',c,'FaceAlpha',0);
Creating all shapes consistently with fill avoids issues later
Walter Roberson
il 16 Feb 2020
hold on
head = polygon(1.5,1000,0,9);
drawshapeNew(head,'b','f');
body = [0 0 ; 9 0];
drawshapeNew(body,'b','d');
leg1 = [0 3;0 -7];
leg2 = [0 0;0 -7];
drawshapeNew(leg1,'b','d');
drawshapeNew(leg2,'b','d');
foot1 = [3 5;-7 -7];
foot2 = [0 2; -7 -7];
arm1= [0 4;4 1];
arm2= [0 4;4 4];
drawshapeNew(foot1,'b','d');
drawshapeNew(foot2,'b','d');
drawshapeNew(arm1,'b','d');
drawshapeNew(arm2,'b','d');
basketball = polygon(1.5,10,4,2.5);
ball_handle = drawshapeNew(basketball,'m','f');
basketballNet = [140 140 130;-7 20 20];
drawshapeNew(basketballNet,'g','d');
axis([-10 150 -10 90])
axis square
prompt = 'How powerful do you want to throw the ball? enter a value between 0 and 100';
v1 = input(prompt);
if v1<0;
v1 = 50;
msgbox('value too low - a default value of 50 has been chosen')
end
if v1>100;
msgbox('value too high - a default value of 50 has been chosen')
v1 = 50;
end
v = v1/1.5
prompt1 = 'at what angle do you want to shoot the ball? choose between 0 and 90 degrees';
angle1 = input(prompt1)
if angle1<0;
angle1 = 45;
msgbox('value too low - a default value of 45 has been chosen')
end
if angle1>90;
msgbox('value too high - a default value of 45 has been chosen')
angle1 = 45;
end
angle = angle1*pi/180
xball = [4:0.5:140];
yball = 2.5 + (xball-4)*tan(angle) - (4.9/((v*cos(angle))^2))*((xball-4).^2);
pause(3)
n = length(xball);
for k = 1:(n-1);
xtranslate = xball(k+1) - xball(1);
ytranslate = yball(k+1) - yball(1);
translatedBall = translate(basketball,xtranslate,ytranslate);
if ytranslate + 2.5 < -10
oshioihrjhoijbz
end
if k == 1
ball_handle = drawshapeNew(translatedBall,'m','f');
else
set(ball_handle, 'XData', translatedBall(1,:), 'YData', translatedBall(2,:));
end
axis([-10 150 -10 90]);
axis square
drawnow
pause(0.01)
end
axis([-10 150 -10 90])
axis square
Sq
il 16 Feb 2020
Categorie
Scopri di più su Animation in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!