how to detect collision
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
how can i detect whether a point in 2D will collide with a circle when going to another.suppose i have to go from x1,y1 to x2,y2 and there is a circle with radius r centered at cx,cy. how can i check whether the straight line path from x1,y1 will touch the circle
0 Commenti
Risposte (2)
Ameer Hamza
il 24 Mag 2018
Based on the condition given here, you can write following statement to check whether the line touch the circle or not
C = [2 5];
r = 3;
X1 = [1 0];
X2 = [5 4];
coff = polyfit([X1(1), X2(1)], [X1(2), X2(2)], 1);
doesTouch = abs(coff(1)*C(1) + coff(2) - C(2))/norm([1 coff(1)]) < r;
0 Commenti
Nicola Bombace
il 24 Mag 2018
Modificato: Nicola Bombace
il 24 Mag 2018
You could create a line between the points (x1,y1) and (x2,y2) in the form y = ax + b with polyfit and work out the slope and the intercept of the path. Then use the function linecirc and check the output. If the output is Nan, the path does not intersect the circle.
% input: x1 y1--> point 1
% x2 y2 --> point 2
% cx,cy,r --> coordinates center and radius of the circle
coefficients = polyfit([x1, x2], [y1, y2], 1);
a = coefficients (1);
b = coefficients (2);
[xout,yout] = linecirc(a,b,cx,cy,r);
if any(isnan(xout))
disp('The line does not intercept the circle')
else
disp('The line intercepts the circle')
end
0 Commenti
Vedere anche
Categorie
Scopri di più su Image Processing Toolbox in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!