c++ - 矩形碰撞检测

标签 c++ algorithm collision-detection

class Rectangle{
public:
   float x, y, width, height;
   // (x,y) is the lower left corner of the rectangle
};

这个算法正确吗?

bool Rectangle::colidesWith(Rectangle other) {
   if (x+width < other.x) return false; // "other" is on the far right
   if (other.x+other.width < x) return false; //"other" is on the far left
   if (y+height < other.y) return false // "other" is up
   if (other.y+other.height < y) return false // "other" is down
   return true;
}

最佳答案

如果矩形被填充(即,如果其中一个在另一个矩形内,则算作碰撞)。

关于c++ - 矩形碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13919303/

相关文章:

c++ - VC++ 在编译完成后挂起并卡住计算机

algorithm - 最小化行程数或组最大可能订单

c++ - 使用SDL的像素颜色和每像素碰撞的Alpha值

java - 创建一个垂直横向卷轴,在数组列表和逻辑方面遇到问题(Java)

javascript - 如何检测不易多边形分割体的碰撞

c++ - 将颜色映射到间隔

c++ - 编译器如何确定何时对 RVO 是安全的?

c++ - 在 C++ 中访问姐妹对象的成员变量

快速点查找和视线遍历算法

c - MPI_Cart_shift 不返回邻居