
How to find if a projectile intersects a point in a line?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
This is my uni assignment. I have to make an angry birds like game. I have created most of it but im kind of stuck at how to tell if ive "hit the target". We were given a matrix equation type thing of how to work out if lines intersect but im not sure how to use that with my projectile?
Also is there a way to have a margin of error, like thats close enough.
I have points of intersection of the ground and the target set to xi, yi. for the margin of error can you do something like yt1 == yi +- 5?
Thanks for the help. I've included my full code below.
clc; clear all; close all;
x1 = 0;
y1 = 50;
x2 = 1000;
y2 = 50;
x3 = 725;
y3 = 49;
x4 = 725;
y4 = 51;
MatrixA = [x1 x2 x3; y1 y2 y3; 1 1 1];
MatrixB = [x1 x2 x4; y1 y2 y4; 1 1 1];
MatrixC = [x1 x3 x4; y1 y3 y4; 1 1 1];
MatrixD = [x2 x3 x4; y2 y3 y4; 1 1 1];
if det(MatrixA)*det(MatrixB) < 0 && det(MatrixC)*det(MatrixD) < 0
xi = ((x4-x3)*(x2*y1-x1*y2)+(x2-x1)*(x3*y4-x4*y3))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
yi = ((y4-y3)*(x2*y1-x1*y2)+(y2-y1)*(x3*y4-x4*y3))/((x2-x1)*(y4-y3)-(x4-x3)*(y2-y1));
intersection = [xi yi];
%fprintf('Co-ordinates of intersection = (%0.2f %0.2f)\n', xi, yi)
else
disp('False')
end
plot(xi,yi, '-p', 'MarkerSize', 20,...
'MarkerEdgeColor', 'blue',...
'MarkerFaceColor', 'red');
xlim([0,1000]);
ylim([0,500]);
hold on
ground_length = x1:x2;
ground = 0*ground_length + 50;
plot(ground, '-');
prompt1 = 'What is the firing angle? '; %asking for user to input the angle to fire at
theta1 = input(prompt1);
prompt2 = 'What is the firing speed in m/s? '; %asking for the user to input the speed of the projectile
v01 = input(prompt2);
x0 = 0; %starting point on x axis
y0 = 50; %starting point on y axis
g = 9.81; %gravity. used positive as it gives wonky time numbers if using negative
tup1 = (v01*sind(theta1))/g; %working out the time to max height where vertical velocity = 0
maxheight1 = (v01^2*(sind(theta1)^2))/(2*g) + y0; %working out max height to work out time to hit ground, added y0 as that is the starting height
tdown1 = sqrt(2*maxheight1/g); %time to hit the ground from max height
tmax1 = tup1 + tdown1; %total flight time
t1 = 1:tmax1;
vx1= v01*cosd(theta1); %velocity in x diretion
vy1 = v01*sind(theta1); %velocity in y direction
xt1 = x0 + vx1*t1; %points in time of the projectile
yt1 = y0 + vy1*t1-0.5*g*t1.^2; %points in time of the projectile
plot(xt1,yt1)
title('Level 1 Angry Space Catapult')
0 commentaires
Voir également
Catégories
En savoir plus sur General Applications dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!