【问题标题】:Rectangular collision detection矩形碰撞检测
【发布时间】:2012-12-17 17:47:33
【问题描述】:
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;
}

【问题讨论】:

  • +ve 起来了吗?如果是这样,它看起来没问题。

标签: c++ algorithm collision-detection


【解决方案1】:

如果矩形被填充(即,如果其中一个在另一个内部,则将其视为碰撞)。

【讨论】:

  • 您的意思是,如果它们没有被填充并且其中一个在另一个内部,那么我的算法将不起作用?它适用于所有其他情况吗?
  • 据我所知,是的。另外,第一个问题是的。
  • @l19 - 您的矩形已填充。如果您的一个矩形在另一个矩形内,那么这将被视为碰撞。如果那是您想要的,那么一切都很好。如果该行为不是您想要的,您只需要更改某些内容。
【解决方案2】:

是的。您可以将其视为超平面分离定理的特例,这是该问题的一般版本。您将这些矩形投影到 X 和 Y 轴上,然后检查生成的线段之间是否有一些分离。

【讨论】:

    【解决方案3】:

    对我来说,写这个条件的更直观的方式是:

    ( max(r1.x, r2.x) < min(r1.x+r1.w, r2.x+r2.w) ) &&
    ( max(r1.y, r2.y) < min(r1.y+r1.h, r2.y+r2.h) )
    

    实际上这可以推广到任何维度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-12
      • 2016-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多