Question: function archery _ game ( ) % Constants g = 9 . 8 1 ; % acceleration due to gravity ( m / s ^
function archerygame
Constants
g ; acceleration due to gravity ms
Set up figure
fig figurePosition;
Initialize archer position
archerx ;
archery ; Updated archer starting position
Create sliders for angle and power
angleslider uicontrolStyle 'slider', 'Min', 'Max', 'Value',
'Position', 'Callback', @updateplot;
powerslider uicontrolStyle 'slider', 'Min', 'Max', 'Value',
'Position', 'Callback', @updateplot;
Create fire button
firebutton uicontrolStyle 'pushbutton', 'String', 'Fire',
'Position', 'Callback', @firearrow;
Set up axes
ax axesParent fig, 'Position', ;
axisax; Adjusted yaxis to accommodate the archer and target
xlabelax 'Distance m;
ylabelax 'Height m;
holdaxon;
Plot archer
archerplot plotax archerx archeryb 'MarkerSize', ;
Initialize arrow trajectory
arrowplot plotax NaN, NaN, r 'LineWidth', ;
Play three rounds
for round :
Generate random target position
targetx randi;
targety ; Place target on ground level
targetradius ;
Plot target
targetplot viscirclesaxtargetx targety targetradius, 'EdgeColor', r;
Update plot based on slider values
updateplot;
Wait for user input
uiwaitfig;
Check if the archer hits the target
if checkhitarcherx archery targetx targety targetradius
dispYou hit the target!;
else
dispYou missed the target.;
end
Remove target plot
deletetargetplot;
end
Close figure
closefig;
Update plot based on slider values
function updateplot~ ~
Get angle and power from sliders
angle getangleslider, 'Value';
power getpowerslider, 'Value';
Calculate trajectory
time linspacepowersindangleg; Assuming g ms
xTrajectory archerx power cosdangle time;
yTrajectory archery power sindangle time g time.;
Update arrow trajectory
setarrowplot, 'XData', xTrajectory, 'YData', yTrajectory;
end
Fire the arrow
function firearrow~ ~
Get angle and power from sliders
angle getangleslider, 'Value';
power getpowerslider, 'Value';
Calculate trajectory
time linspacepowersindangleg; Assuming g ms
xTrajectory archerx power cosdangle time;
yTrajectory archery power sindangle time g time.;
Update arrow trajectory
setarrowplot, 'XData', xTrajectory, 'YData', yTrajectory;
Enable next round
uiresumefig;
end
Check if the archer hits the target
function hit checkhitarcherx archery targetx targety targetradius
Calculate distance between archer and target
distance sqrtarcherx targetxarchery targety;
Check if the distance is less than or equal to the target radius
hit distance targetradius;
end
end
Above is a code for an archery game. The problem is when I shoot the arrow at the target with the trajectory lined up it doesnt count as a hit. No matter how close or even on the target the arrow trajectory is it misses and I need it to properly tell the difference between a hit and a miss.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
