Help!! Projectile motion plot
Show older comments
am currently having trouble with my script outputing the scores when the user has scored a point by hitting the board on the plot with a ball. Any help will be greatly appreciated.
(script)
h=figure;
score = 0;
clf(h);
AxesH = axes('NextPlot','add');
while (1)
play = menu('Would you like to play?: ', 'Yes', 'No');
switch play
case (1)
b = input('Please input angle value: ');
if (b >= 0) && (b <=90)
[x,y] = HDTask1function_f(b);
z = rand()*3.5;
j = z+0.5;
hndl2=plot([z j],[0 0],'LineWidth',5);
hold on
set(gca,'Xlim',[0 4])
set(gca,'Ylim',[0 4])
hndl=plot(x(1),y(1),'r.','MarkerSize',50);
for ii=2:length(x)
drawnow;
set(hndl,'XData', x(ii), 'YData', y(ii));
pause(0.0025);
if y(ii:end) < 0
y(ii:end) = 0;
pause(0.5);
if x(1,ii) > z && x(1,ii) < j
score = score +1;
fprintf('Your current score is: %4.2', num2str(score));
end
break
end
end
else
disp('Invalid input. Please input an angle in the range of [0 - 90]');
end
case(2)
disp('Thankyou for visiting')
break;
end
end
(function)
function [x,y]=HDTask1function_f(a)
x0=0;
y0=1.8;
v0=4;
g=9.8;
t=0:0.01:5;
x=x0+v0*cos(a*(pi./180)).*t;
y=y0+v0*sin(a*(pi./180)).*t-(g.*t.^2)/2;
end
8 Comments
Rik
on 22 Apr 2020
Your questions look really similar. You also insist on not using proper formatting. They look so similar that one of them even got flagged as spam. That should make you reconsider how you are approaching your homework.
Please respond in a comment, instead of posting a new question. You can put a follow up in the same thread, that will make it easier to see the progression in your question and will prevent people missing a follow up question.
Rik
on 22 Apr 2020
In response to your flag: I have formatted most of your questions for you, nor was I the one flagging one of your questions as spam, so fail to see how I would be bullying you.
I have posted several links to thread that should be helpful for you, but it seems you haven't read them yet. If anything you're bullying me and all other users of this forum.
Aaron Stronguin
on 22 Apr 2020
Ameer Hamza
on 22 Apr 2020
Aaron, you have posted five similar questions. Two of them already have answers, including one from Walter. Why don't you comment on those answers if you think their solution does not work.
Rik
on 22 Apr 2020
You're not making it easy for people to help you by posting very similar questions quickly one after the other. You aren't targeting me specifically (at least I have seen no indication of that), so if there is any target for any bullying it is the entire community.
I would suggest to stop this line of discussion and focus on how to improve your questions and how to find sattisfying answers to them. Just to get a second opinion I'm not going to remove the flags, but leave it to another user with sufficient reputation points to do so.
Aaron Stronguin
on 22 Apr 2020
Ameer Hamza
on 22 Apr 2020
Aaron, There is no point in flagging Rik's comment. I don't see how that will help you in solving your problem. I have removed these flags since you are clearly pointless.
Rik
on 22 Apr 2020
Now I'm convinced you are trolling me. How is spending time to help you a form of bullying? I'm done with you. I do this for fun. From now on you don't have to expect any form of help from me.
Answers (1)
Rik
on 22 Apr 2020
Despite your rudeness (i.e. contiuously flagging my comments) I decided to help you. So below you will find your code with some minor edits and with some comments for places where you need to do some edits.
By making it a function you give mlint the opportunity to give you helpful advice. Make sure all warnings are resolved.
function MyGame
h=figure;
score = 0;
%clf(h);
%the figure was just created, so you don't need to clear it
AxesH = axes('NextPlot','add','Parent',h);
% ^^^^^^^^^^^ add this to make it explicit
while (1)
play = menu('Would you like to play?: ', 'Yes', 'No');
switch play
case (1)
b = input('Please input angle value: ');
if (b >= 0) && (b <=90)
[x,y] = HDTask1function_f(b);
z = rand()*3.5;
j = z+0.5;
hndl2=plot([z j],[0 0],'LineWidth',5,'Parent',handle_to_parent_axis);
% ^^^^^ ^^^^^^^^^^^^ add a parent object here
% where are you using hndl2 for? you could use it to delete the target plate, but you don't
% shouldn't these three lines be outside of the loop (or in some logic making sure they only run once)? This way the target moves every time the game runs.
% hold on
% hold on is not needed, because you already have NextPlot set to add
set(gca,'Xlim',[0 4])
set(gca,'Ylim',[0 4])
% these two line are part of the setup and only need to run once
hndl=plot(x(1),y(1),'r.','MarkerSize',50);
for ii=2:length(x)
drawnow;
set(hndl,'XData', x(ii), 'YData', y(ii));
pause(0.0025);
if y(ii:end) < 0
y(ii:end) = 0;
pause(0.5);
if x(1,ii) > z && x(1,ii) < j
score = score +1;
fprintf('Your current score is: %4.2f', num2str(score));
% ^ this was missing (and why is this displayed with two decimals? if the score is an integer you could use %d instead)
end
break
end
end
else
disp('Invalid input. Please input an angle in the range of [0 - 90]');
end
case(2)
disp('Thankyou for visiting')
break;
end
end
end
function [x,y]=HDTask1function_f(a)
x0=0;
y0=1.8;
v0=4;
g=9.8;
t=0:0.01:5;
x=x0+v0*cos(a*(pi./180)).*t;
y=y0+v0*sin(a*(pi./180)).*t-(g.*t.^2)/2;
end
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!