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?

5 views (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.

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

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!