how to detect collision

조회 수: 18 (최근 30일)
Borison ningthoujam
Borison ningthoujam 2018년 5월 24일
편집: Nicola Bombace 2018년 5월 24일
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
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
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

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by