C++ 计算具有已知大小和位置的两个对象之间的碰撞

C++ 计算具有已知大小和位置的两个对象之间的碰撞,c++,graphics,collision,C++,Graphics,Collision,可能重复: 考虑到我有两个正方形,我知道它们的x和y位置,我也知道它们的大小,如果我想知道物体是否相互碰撞,那么使用什么公式呢 if( ((shapeA->getX() - shapeA->getSize()) > (player->getX() - player->getSize()) && (shapeA->getX() + shapeA->getSize()) < (player->getX() + pla

可能重复:

考虑到我有两个正方形,我知道它们的x和y位置,我也知道它们的大小,如果我想知道物体是否相互碰撞,那么使用什么公式呢

if(   ((shapeA->getX() - shapeA->getSize()) > (player->getX() - player->getSize())
    && (shapeA->getX() + shapeA->getSize()) < (player->getX() + player->getSize()))
    && (shapeA->getY() - shapeA->getSize()  > player->getY() - player->getSize()
    && (shapeA->getY() + shapeA->getSize()) < (player->getY() + player->getSize()))
                )
if(((shapeA->getX()-shapeA->getSize())>(player->getX()-player->getSize())
&&(shapeA->getX()+shapeA->getSize())<(player->getX()+player->getSize())
&&(shapeA->getY()-shapeA->getSize()>player->getY()-player->getSize()
&&(shapeA->getY()+shapeA->getSize())<(玩家->getY()+玩家->getSize())
)

这是可行的,但效果很奇怪(并非一直如此)。我肯定遗漏了什么

假设getX/Y给出了正方形的左下角

shapeMinX = shapeA; shapeMaxX = shapeB;
if (shapeA()->getX() > shapeB()->getX())
  swap (shapeMinX, shapeMaxX);
shapeMinY = shapeA; shapeMaxY = shapeB;
if (shapeA()->getY() > shapeB()->getY())
  swap (shapeMinY, shapeMaxY);

collision = (shapeMinX->getX()+shapeMinX->size() >= shapeMaxX()->getX) || (shapeMinX->getY()+shapeMinY->size() >= shapeMaxY()->getY);

很容易检查一个矩形是否与另一个矩形相交或接触。请看下图:

如您所见,如果([x,x+a]和[x,x+a])与([y,y+b]和[y,y+b])之间的交点都不是空的,则两个矩形相交

struct Rectangle{
    bool intersects(const Rectangle&);

    unsigned int a; //!< width of the rectangle
    unsigned int b; //!< height of the rectangle
    unsigned int x; //!< x position
    unsigned int y; //!< y position
};

bool Rectangle::intersects(const Rectangle& oRectangle){        
    return  (x < oRectangle.x + oRectangle.a) && // [x,x+a], [X,X+A] intersection
            (oRectangle.x < x + a)            && // [x,x+a], [X,X+A] intersection
            (y < oRectangle.y + oRectangle.b) && // [y,y+b], [Y,Y+B] intersection
            (oRectangle.y < y + b);              // [y,y+b], [Y,Y+B] intersection
}
struct矩形{
布尔相交(常数矩形&);
无符号int a;//!<矩形的宽度
无符号int b;//!<矩形的高度
无符号整数x;//!
所以你的代码应该是

if(((shapeA->getX() + shapeA->getSize()) > (player->getX()) // x intersection
    && (shapeA->getX() < (player->getX() + player->getSize())) // x intersection
    && (shapeA->getY() < player->getY() + player->getSize()  // y intersection
    && (shapeA->getY() + shapeA->getSize()) > player->getY())  // y intersection
)
if(((shapeA->getX()+shapeA->getSize())>(player->getX())//x交叉点
&&(shapeA->getX()<(player->getX()+player->getSize())//x交叉点
&&(shapeA->getY()getY()+player->getSize()//y交叉点
&&(shapeA->getY()+shapeA->getSize())>player->getY())/y交叉点
)

如果您做了错误的测试,请尝试以下方法:

int left_bound_A=  shapeA->getX()-shapeA->getSize();
int right_bound_A= shapeA->getX()+shapeA->getSize();
int top_bound_A= shapeA->getY()-shapeA->getSize();
int bottom_bound_A= shapeA->getY()+shapeA->getSize();

int left_bound_B=  shapeB->getX()-shapeB->getSize();
int right_bound_B= shapeB->getX()+shapeB->getSize();
int top_bound_B= shapeB->getY()-shapeB->getSize();
int bottom_bound_B= shapeB->getY()+shapeB->getSize();

if( left_bound_A < right_bound_B &&
    right_bound_A > left_bound_B &&
    top_bound_A > bottom_bound_B &&
    bottom_bound_A < top_bound_B ) colide(shapeA,shapeB);

是的,检查两个矩形的方法很简单。 作为一个建议,如果你想计算一个矩形列表中矩形之间所有可能的交集,通过增加它们边界的x来对它们进行预排序,然后利用这个关系开始比较。这个问题可能会有所帮助


可能会引起兴趣

您应该使用调试器(或只添加有用的打印语句)来识别所有相关变量的值,并找出为什么它不适用于失败的情况。找到正方形所有边的方程式,并检查两条边是否相交。此答案缺乏解释(+我怀疑你能用一个
做到这一点,但我可能弄错了,在你解释之前我不知道)。你的意思可能是
交换(shapeMinX,shapeMaxX)
而不是
开关(shapeMinX,shapeMaxX)
。为什么我没有得到一个弹出窗口,线程有新的答案,我需要重新加载?fuuuu…发生在我们所有人身上;)。
 Box colision= intersect( shapeA->getBoundBox(), shapeB->getBoundBox() );
 if( colision.have_positive_area() )
     colide(shapeA,shapeB,colision);