user4383562
user4383562

Reputation:

Allegro Circle Collision Detection

I have a function that is supposed to return true if two circles are colliding and false otherwise, and to help while developing I have also added a part within the function to draw the hitbox only when they're not colliding.

My issue is even when they are colliding it will continue to draw the hitbox, and say they're not colliding, indicating that the function isn't working properly.

int colliding(int x, int y, int r, int x1, int y1, int r1)
{
    //compare the distance to combined radii
    int dx = x1 - x;
    int dy = y1 - y;
    int radii = r + r1;
    if ((dx * dx) + (dy * dy) < radii * radii)
    {
        return true;
    }
    else
    {
        player.hitbox.draw();
        return false;
    }
}

int main()
{
    while (true)
    {
        player.draw();

        int cx = 300;
        int cy = 300;
        int cr = 50;

        al_draw_filled_circle(camera.getScreenPosX(cx), camera.getScreenPosY(cy), cr, al_map_rgb(0, 0, 0));
        colliding(player.hitbox.posX, player.hitbox.posY, player.hitbox.radius, cx, cy, cr);


        al_flip_display();
        al_clear_to_color(al_map_rgb(255, 255, 255));
    }
}

Upvotes: 0

Views: 326

Answers (1)

Thomas Hilbert
Thomas Hilbert

Reputation: 3629

I would assume that camera.getScreenPosX/Y() transforms your cx/cy/cr circle into another space than the one where player.hitbox.posx/y are. I cannot be sure however, because implementation of player.hitbox.draw() is not given.

Your collision function seems fine, so I'd go and check whether player.hitpox.posx/y and cx/cy are in the same coordinate space.

Upvotes: 0

Related Questions