how to detect collision
12 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Ameer Hamza
on 24 May 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 Comments
Nicola Bombace
on 24 May 2018
Edited: Nicola Bombace
on 24 May 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 Comments
See Also
Categories
Find more on Axis Labels 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!