Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ &引用;勾选“;如果两个不同的几何形状对象碰撞_C++_C_Geometry - Fatal编程技术网

C++ &引用;勾选“;如果两个不同的几何形状对象碰撞

C++ &引用;勾选“;如果两个不同的几何形状对象碰撞,c++,c,geometry,C++,C,Geometry,我只是想“检查”两个不同几何形状的物体是否碰撞。一个是圆,第二个是正方形。我在谷歌上搜索了一个简单的例子并从中学习,因为我不知道怎么做。我找到了这个例子,尝试了一下,但什么都没发生。代码中可能有一个bug。请用C或C++语言帮助我改正。< /P> bool Collision(int circleX, int circleY, int radius, int squareX, int squareY, int width, int height) { int distance = 0;

我只是想“检查”两个不同几何形状的物体是否碰撞。一个是圆,第二个是正方形。我在谷歌上搜索了一个简单的例子并从中学习,因为我不知道怎么做。我找到了这个例子,尝试了一下,但什么都没发生。代码中可能有一个bug。请用C或C++语言帮助我改正。< /P>
bool Collision(int circleX, int circleY, int radius, int squareX, int squareY, int width, int height)
{
    int distance = 0;

    if(circleX < squareX) 
        distance += pow(circleX - boxX,2); //x-axis
    else if (circleX > squareX + width)
        distance += pow(circleX - squareX - width, 2);

    if(circleY < squareY) 
        distance += pow(circleY - squareY,2); //y-axis
    else if (circleY > squareY + height) 
        distance += pow(circleY - squareY - height, 2);

    if( distance <= pow(radius, 2))
        return true; 
    else    
        return false;
}
bool冲突(整数圈、整数圈、整数半径、整数平方、整数平方、整数宽度、整数高度)
{
整数距离=0;
if(圈<平方x)
距离+=pow(圆框x,2);//x轴
else if(圆圈>正方形+宽度)
距离+=功率(圆-平方-宽度,2);
if(圆<平方)
距离+=pow(圆-平方,2);//y轴
否则如果(圆形>方形+高度)
距离+=功率(圆-平方-高度,2);

如果(距离首先,让我们了解如何表示圆和正方形

按照惯例,
正方形
表示如下:

  squarexX, squareY + height  ------------ squareX+ width, squareY +height
  |                                               |
  |                                               |
   squareX, squareY          ------------- squareX + width, squareY
由圆的坐标
(circleX,circleY)
半径
值表示。记住这一点,您甚至可以编写自己的函数。请注意,圆没有宽度或高度

因此,在他们的代码中:

 if(circleX < squareX) distance += pow(circleX - boxX,2); //x-axis
我们的正方形坐标如下(包括中心)

我们可以看到,(60172)位于边(120252)->(120180)的左侧,(120180)和(60,72)之间的距离大于39。(120252)和(60,72)之间的距离也大于39,没有重叠。为了更好地理解它,给定圆心(60172)半径39,从圆心可以到达的x和y的范围是从(60-39,60+39)=(21,99)和(172-39,172+39)=(133211)。如果可视化,这应该与正方形没有重叠

如果您首先将原点转换为(156216),则在新坐标系中,圆的中心有(-96,-44)。它位于第三象限。如果您运行testForCollision函数,您将看到没有重叠

dx = min(-96,36) = -96
dx1 = max(dx, -36) = -36
dy = min(-44, 36) = -44
dy1 = max(dy,-36) = -36

distance = (-36 - (-96))^2 + (-36 - (-44))^2 = 60^2 + 8^2 > 39^2, so no overlap
有3种情况:

  • 形状重叠,因此一个形状的边缘与另一个形状的边缘交叉
  • 圆圈完全位于正方形内(例如,想象一个巨大的矩形,其中某处有一个小圆圈)
  • 完全位于圆内的矩形(例如,想象一个巨大的圆中有一个小矩形)
首先确定圆心和矩形中心之间的距离,我们称之为距离D

然后找到一个最大的圆,它足够小,可以放在矩形内。这个圆的直径等于矩形的高度或矩形的宽度(以较小者为准)。如果新圆的半径加上原始圆的半径小于距离D,则肯定存在碰撞

然后找一个足够大的最小圆来容纳整个矩形。这个圆的半径可以用毕达哥拉斯(从矩形中心到矩形任何一个角的距离)来求.如果新圆的半径加上原始圆的半径大于距离D,则不会发生任何碰撞

如果这两个测试都没有给出答案,那么两个形状都不完全在另一个形状内,但它们可能重叠或不重叠;因此,您必须确定一个形状的边是否与另一个形状的边相交。为此,请延伸矩形的所有边,使其无限长(使用线段来描述直线);并尝试通过在圆的公式中“插入”x或y来计算这些无限长直线与圆周长相交的位置(注意:您将找不到交点、1个交点或2个交点).如果有两个交叉点,则必须测试其中一个是否位于边缘

例如,如果矩形的上边缘在
y=2
处,则将
y=2
插入到圆的形式中。如果发现这条线在
x=3
和x=6`处相交,则测试3和6是否都在矩形的左边缘和右边缘之间-如果它们是cir的边缘cle与矩形的上边缘相交,发生碰撞


如果进行“边相交”在没有检测到碰撞的情况下测试矩形的所有4条边,那么就没有要检测的碰撞。

碰撞函数中的boxX是什么?您的实现使用的变量不是您声明的函数的一部分。而且标记很差。boxX是从哪里来的?我不知道,因为网站上已经发布了这一点2009年,没有关于boxX的解释,我试着将boxX改为squareX,但它不起作用。我会写一个答案告诉你如何做圆-圆碰撞,这就是你要做的事情。没有,我已经做了圆-圆碰撞,但想要圆-方/矩形碰撞。你能根据什么来做函数吗你说,请让我马上选择你的anwser,它会帮助其他人也明白你的意思,但我已经将boxX改为squareX,它没有正常工作,除此之外,pow pow(circleX-squareX,2);必须是pow(circleX-squareX,2.0),但如果我改变它,它会说:从'double'转换为'int',可能会丢失data@annaSjolvikaz看到我更新的代码了吗?你例子中的代码不太正确。你说的对,但是圆形或正方形的宽度和高度是多少?
//this function computes the distance between center of circle
//to closest point on one of the edges of square, that edge is on the
//same side as the circle
#include <algorithm>
bool testForCollision(int circleX, int circleY, int width, int height, int radius)
{   

    int dx = std::min(circleX, (int)(width  * 0.5));
    int dx1 = std::max(dx, (int)(-width *0.5));

    int dy = std::min(circleY, (int)(height * 0.5));
    int dy1 = std::max(dy, (int)(-height * 0.5));

    return (dx1 - circleX) * (dx1 - circleX) 
           + (dy1 - circleY) * (dy1 - circleY) <= radius * radius;
}


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

    //get center of square
    int center_of_square_x = squareX + width/2;
    int center_of_square_y = squareY + height/2;

    //if square is already at origin, test directly
    if ( center_of_square_x == 0 && center_of_square_y ==0)
    {
        return testForCollision(circleX, circleY, width, height, radius);
    }
    else
    {
        //shift center of square to origin and update coordinates of circle
        //only consider part of the situation, more to add about shifting
        circleX = circleX - center_of_square_x;
        circleY = circleY - center_of_square_y;
        return testForCollision(circleX, circleY, width, height, radius);
    }
}
circleX = 60 circleY = 172 radius = 39
squareX = 120 squareY = 180 width = 72 height = 72 
120, 252  -------- 192, 252
 |                   |
 |--------156,216----|
 |                   |
120,180   ---------192,180
dx = min(-96,36) = -96
dx1 = max(dx, -36) = -36
dy = min(-44, 36) = -44
dy1 = max(dy,-36) = -36

distance = (-36 - (-96))^2 + (-36 - (-44))^2 = 60^2 + 8^2 > 39^2, so no overlap