简体   繁体   English

Allegro圆碰撞检测

[英]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. 我有一个函数,如果两个圆发生碰撞,该函数应该返回true,否则返回false,为帮助开发,我还在该函数中添加了一个部分以仅在未碰撞时绘制命中框。

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));
    }
}

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. 我假设camera.getScreenPosX/Y()将您的cx / cy / cr圈子转换为另一个空间,而不是player.hitbox.posx / y所在的空间。 I cannot be sure however, because implementation of player.hitbox.draw() is not given. 但是我不确定,因为没有给出player.hitbox.draw()实现。

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. 您的collision函数看起来不错,所以我去检查player.hitpox.posx / y和cx / cy是否在同一坐标空间中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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