C++ 不同的物体碰撞

C++ 不同的物体碰撞,c++,c,geometry,C++,C,Geometry,我只想检查两个物体是否碰撞,在这种情况下,它是一个圆,第二个是正方形。我使用的代码工作得很好,但它只检查正方形的右侧和下/按钮是否发生碰撞,请帮助我更正它,以便我能够检查所有侧面的碰撞 问题是,如果正方形与圆碰撞,我只想检查正方形的所有边,但它只检查两边,函数如下: bool Collision(int circleX, int circleY, int radius, int squareX, int squareY, int width, int

我只想检查两个物体是否碰撞,在这种情况下,它是一个圆,第二个是正方形。我使用的代码工作得很好,但它只检查正方形的右侧下/按钮是否发生碰撞,请帮助我更正它,以便我能够检查所有侧面的碰撞

问题是,如果正方形与圆碰撞,我只想检查正方形的所有边,但它只检查两边,函数如下:

 bool Collision(int circleX, int circleY, int radius, 
                   int squareX, int squareY, int width, int height)
    {

        double distance = 0;

        //get circle of square
        double center_square_x = (double)(squareX + squareX + width)/2;
        double center_square_y = (double)(squareY + squareY + height)/2;

        //check for whether circle fully located inside square
        if (circleX >= squareX && circleX <= squareX + width
            && circleY >= squareY && circleY <= squareY + height)
            return true;

        distance =  pow (circleX - center_square_x,2.0) 
                  + pow(circleY - center_square_y,2.0);

        if( distance <= pow(radius, 2.0))
           return true; 
       else    
           return false;
     }
bool碰撞(int-circleX,int-circleY,int-radius,
整数平方x,整数平方,整数宽度,整数高度)
{
双倍距离=0;
//得到正方形的圆
双中心x正方形x=(双)(正方形x+正方形x+宽度)/2;
双中心_平方_y=(双)(平方+平方+高度)/2;
//检查圆是否完全位于正方形内
如果(circleX>=squareX&&circleX=squareY&&circleY试试这个

bool Collision(int circleX, int circleY, int radius, 
               int squareX, int squareY, int width, int height)
{

    double distance = 0;

    //get circle of square (the center of the rectangle or square is
    // (squareX + width)/2 and (squareY+height)/2

    double center_square_x = (double)(squareX + width)/2;
    double center_square_y = (double)(squareY + height)/2;

    // check for each segment the circle position

    // check if the circle is between the bottom and upper square segments
    if (circleY >= squareY && circleY <= squareY + height) 
           // check for left segment
           if (circleX >= squareX && circleX < centerX )
           return true;
           // check for right segment
       else if (circleX <= squareX+width && circleX > centerX)
           return true;
     // check if the circle is between left and right square segments
    else if (circleX >= squareX && circleX <= squareX + width)
         // check for upper segment
         if (circleY >= squareY && circleY < centerY)
           return true;
         // check for bottom segment
        else if (circleY <= squareY + height && circleY > centerY)
           return true;
bool碰撞(int-circleX,int-circleY,int-radius,
整数平方x,整数平方,整数宽度,整数高度)
{
双倍距离=0;
//获取正方形的圆(矩形或正方形的中心为
//(正方形+宽度)/2和(正方形+高度)/2
双中心x正方形x=(双)(正方形x+宽度)/2;
双中心×正方形×y=(双)(正方形+高度)/2;
//检查每个段的圆位置
//检查圆是否位于底部和上部方形段之间
如果(圆圈>=squareY&&circleY=squareX&&circleX=squareX&&circleX=squareY&&circleY

我不知道您试图通过距离计算实现什么,但它无法实现。因为当您在检查碰撞后返回true时,函数将退出其范围。

如果可以,请添加更多描述。。当然,只是用
距离更新了它。
仅测试rec的中心是否tangle在圆中。您需要测试矩形的任何部分是否在圆中。您可以通过查找矩形中距离圆中心最近的点来完成此操作。因为您已经测试过圆的中心在矩形之外,所以最近的点将位于矩形的一条边上,因此您可以需要找到点和线段之间的最短距离(好的,4条线段)@Steve Jessop你能根据你所说的重新编写代码吗?这样我才能更好地理解它,它也会帮助其他人。我将选择你的anwser作为奖励。在这种情况下,代码确实会检查四个面并返回真值,但有一个小问题。它不知道圆是否与正方形接触,如果圆圈在同一侧,但不管圆圈是否远离它……请帮我修复它。如果圆圈远离正方形,为什么要检查是否有碰撞?不,你没有明白我的意思,我只是想知道“如果”它们是共线的,但是上面你发布的代码没有检查这一点,而是检查两个对象是否在同一侧,不管多远……哦,我明白问题所在了……好的,我在深夜写的……对不起:)…现在发生的是,如果圆x的位置不在正方形x的位置,但它们有相同的y位置,它会返回真值,对吗?是的,看看这个:如果圆:O在这里,| |正方形在那里,只要两者都在同一侧,它仍然返回真值