MATLAB Answers

How do I replace an image on a graph and replace it with a new image, whilst keeping all other images in their place?

1 view (last 30 days)
Sq
Sq on 16 Feb 2020
Commented: Sq on 16 Feb 2020
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.

  0 Comments

Sign in to comment.

Accepted Answer

Sindar
Sindar on 16 Feb 2020
ball_handle = fill(x,y,c); %or secondhand through drawshapeNew
...
ball_handle.Vertices = ball_handle.Vertices + [xtranslate ytranslate];

  1 Comment

Sindar
Sindar on 16 Feb 2020
didn't refresh often enough; see Walter Roberson's comment for direct implementation in your code. Also, there may be different effects to changing with XData/YData vs Vertices; I don't know. Get/Set vs dot-indexing should be equivalant on current releases, but only get/set works for older ones

Sign in to comment.

More Answers (1)

Sindar
Sindar on 16 Feb 2020
Edited: Sindar on 16 Feb 2020
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 Comments

Sindar
Sindar on 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
Walter Roberson on 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

Sign in to comment.

Sign in to answer this question.

Tags

Products