Java – how to determine if two circular sectors overlap each other

Each sector can be expressed as (x, y, R, a, d), where x, y is the position, R is the radius, D is the direction and a is the angle Given the information of these two circular sectors, how to determine whether they overlap each other?

Solution

I know a very fast way to discount the possibility because I used to use circle collisions

Find the distance between the two centers, and then if it is greater than the sum of the radii, there will be no collision For efficiency, don't use the square root, just work directly on the square value:

if (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) > (r1 + r2) * (r1 + r2):
    # No chance of collision.

Working for the circle will be more difficult

The method you choose depends on the accuracy you need If you are doing practical mathematics, you may need high precision But, for example, if you do it like a computer game, close enough may be enough

If so, I'll study turning arcs into a series of straight lines (the number may depend on the "expansion" of arcs - you may avoid the spread of several lines in one degree radian, but 180 degrees is not very good)

Linear collision detection is a more well-known method, although you have to deal with the fact that the number of comparisons may rise rapidly

If you don't want to use line segments, here's the process to follow It uses the circle collision algorithm to find the zero point of the whole circle, the collision of one or two points, and then check these points to see if they are between two arcs

First, run the above check to detect an unlikely collision If there is no collision between circles, the arc will not collide

Secondly, check whether the circle has a collision point If so:

(x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) == (r1 + r2) * (r1 + r2)

Of course, within the appropriate error range We should all know by now that more equal floating point numbers should use some kind of delta comparison

If so, you have one thing to check, and you can easily find it The line along the line from (x1, Y1) to (X2, Y2) is R1 units, or watch it move some fractions along the line:

(x1 + (x2-x1) * (r1+r2) / r1,y1 + (y2-y1) * (r1+r2) / r1)

Otherwise, there are two points to check. You can use the answer to a question like this one to determine these two points

Once you have some collision points, this is a much simpler method. Find out whether these points are on an arc. Remember, candidate points will need to be on two arcs to make them conflict with each other, not just one

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>