how to detect collision

12 次查看(过去 30 天)
Borison ningthoujam
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 中查找有关 Axis Labels 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by