开发者

circle - rectangle intersection

I have problem with circle-rectangle intersection.Though A number of discussion i found about it ,i could not get my answer.My problem is -

I have a rectangle lower portion(100-200,0-50) of my view/window(320 X 480).And a ball is moving here and there.And sometimes it collides with the rectangle and bounce back.And my problem is how will i know in which axis circle collide with the rectangle, in x-axis or y axis,means in which line(x=100 or x=200 or y==50) circle intersect with rectangle.

*Ball comes from outside of rectangle.开发者_如何学Go


To see if it hits one of the lines full on is easy: just check for a collision between the bounding box of the circle and each of the lines in turn.

The problem is if it hits a corner... then you have to intersect the circle with each line. This can be done, but requires a bit more mathematics. It will also bounce off at an unusual angle.


Say the ball's center is moving along a time trajectory that can be described as x = a t + b and y = c t + d -- any linear, uniform-speed motion can be described this way. Since you say that it's initially (say at t=0) outside the rectangle, we know that at that time x < 100 or x > 200, or y < 0 or y > 50 (one of the conditions of x, and one of the conditions of y, can both be true, but at least one must be -- if they were all false we'd be inside the rectangle).

So check "at what time and exactly where will that point intersect each of the four lines that make up the rectangle"; i.e., solve for t when x = 100 (which gives t = (100 - b) / a, and therefore y = c (100 - b) / a + d), x = 200, y = 0, y = 50. Discard the solutions where t < 0 (those were things that happened in the past), as well as ones where the other variable falls outside of the rectangle's boundaries (for example, for the t = 100 case I just mentioned, you can ignore the apparent solution if (100 - b) / a < 0, or c (100 - b) / a + v < 0, or c (100 - b) / a + v > 50). If none of the four is left, this means the ball (with a radius of 0...) will not hit the rectangle along its current trajectory (it may if and when it bounces and thus changes trajectory, but those will be separate computations). If one or more are left, the one with the minimum value of t is the one you want. Once you know where and when the center would hit the rectangle, taking account of the radius can be done separately, but won't change the issue of which rectangle side the ball hits.

The cases where the ball "glances" (hits the rectangle just because it does have a radius greater than zero) are harder, but one approach is, if the normal computation shows the ball "not hitting", repeat it after shifting the ball (by the amount of its radius) to both side of the trajectory-line it's following -- this will tell you if the ball IS in fact going to hit, and, if so, which side (assuming hits on corners can be counted as hits on one of the sides converging on that corner;-).


How about:

Let centre of circle have coordinates cx, cy, radius cr.

if cx > 100 - cr and cx <= 100 and cy <= 50
    collision with left upright
else if cy >= 50 and cy < 50 + cr and cx > 100 and cx < 200
    collision with top
else if cx < 200 + cr and cx >= 200 and cy <= 50
    collision with right upright
else if ( cx - 100 ) ** 2 + ( cy - 50 ) ** 2 < cr ** 2
    collision with top left corner
else if ( cx - 200 ) ** 2 + ( cy - 50 ) ** 2 < cr ** 2
    collision with top right corner
else
    no collision

Corner collisions will need special treatment to work out how bounce will work based on exact point of contact and direction of travel. This also leaves a large part of the screen where collisions will not be detected (inside the rectangle), which I'm sure you could add to the above algorithm.

Doing a quick search seems to indicate that this problem has been asked before...


decrease rectange by size of radius on each side and move circle as point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜