简体   繁体   中英

Circle Collider Not Detecting Point Within It

In a 2D game, my function is detecting whether a sprite is located within a circles bounds. But, it is failing to do so.

The code below simply loops through all the points that make up the outline of the circle (each point has an x and y coordinate). It then breaks the circle into 4 equally-sized quadrants (like a cartesian plane), and checks whether the sprite is within that quadrant (x/y coordinates start at (0,0) at the top left corner of the window and increase as you go down for y and right for x):

bool IsColliding()
{
for (int i = 0; i < TotalPointsThatMakeUpTheCircleOutline; i++)
{
    // Points lining the top left quadrant of circle
    if (Circle.Point[i].xCoordinate <= Circle.getCenterCoordinate().x &&
        Circle.Point[i].yCoordinate <= Circle.getCenterCoordinate().y)
    {
        // Checks if sprite is in-between circle outline point and center point
        if (sprite.xCoordinate >= Circle.Point[i].xCoordinate &&
            sprite.xCoordinate <= Circle.getCenterCoordinate().x &&
            sprite.yCoordinate >= Circle.Point[i].yCoordinate &&
            sprite.yCoordinate <= Circle.getCenterCoordinate().y)
        {
            return true;
        }
    }
    // Points lining the top right quadrant of circle
    ........
    // Points lining the bottom left quadrant of circle
    ........
    // Points lining the bottom right quadrant of circle
    ........

    return false;
}

}

The other 3 if statements are the same, just reversing appropriate comparison signs ("<" & ">"). Can anyone see what I am missing?

These "box" tests are actually detecting inside the blue area depicted below, which is a crude approximation of the circle and leaves "holes".

在此处输入图片说明

The test (Px - Cx)² + (Py - Cy)² ≤ R² is both fast and perfectly accurate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM