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

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

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

1 Commento

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

Accedi per commentare.

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

I agree with the recommendation to update the position of the ball rather than draw a new one.
I am using the polygon generator to draw a circle, then using the fill ‘function’ to fill in the ball
How would I go about updating the position of the bell rather than deleting and drawing a new one?
I tried the method of deleteing the ball handle each time at the start of the for loop, but for some reason the images of previous locations of the ball stil remain. could it be that it is just deleting the data in the variable but not the actual image?
Can you include the actual line of code where you draw the ball?
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
ball_handle = drawshapeNew(translatedBall,'m','f');
axis([-10 150 -10 90]);
axis square
drawnow
pause(0.01)
end
axis([-10 150 -10 90])
axis square
The above is the main script of my program and the function that I call upon to draw the objects is:
function drawing = drawshapeNew(shape,c,q)
n = length(shape);
if q == 'd' %draws outline of shape
for k = 1:n;
x(k) = shape(1,k);
y(k) = shape(2,k);
drawing = plot(x,y,c);
end
end
if q == 'f' % fills shape instead of drawing outline
for k = 1:n;
x(k) = shape(1,k);
y(k) = shape(2,k);
drawing = fill(x,y,c);
end
end
end
I am new to Matlab so I understand that my code may not be the most efficient
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
Where and how were you deleting the old ball?
I included a line at the start of the for loop in the main script which read “delete(ball_handle)”, but I have removed it as it didn’t work
@Walter Robinson, appreciate the help, but I’m a bit confused on what that will do?
Instead of drawing a new ball, you update the location of the old
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
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
Thank you both for the help. I have just implemented the methods and it seems to be working now

Accedi per commentare.

Categorie

Prodotti

Tag

Richiesto:

Sq
il 16 Feb 2020

Commentato:

Sq
il 16 Feb 2020

Community Treasure Hunt

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

Start Hunting!

Translated by