how to detect collision
古いコメントを表示
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
回答 (2 件)
Ameer Hamza
2018 年 5 月 24 日
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;
Nicola Bombace
2018 年 5 月 24 日
編集済み: Nicola Bombace
2018 年 5 月 24 日
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
カテゴリ
ヘルプ センター および File Exchange で Automated Driving Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!