多边形到多边形碰撞检测问题

发布于 2024-09-06 06:07:33 字数 1388 浏览 3 评论 0原文

我在实现窄相位碰撞检测时遇到了一些问题。 Broadphase 运行完美。

我有一组多边形,它们的顶点有一个 stl::vector 数组,按顺时针顺序排列。每个周期,我都会检查它们是否发生碰撞。

我从这里借用了以下多边形测试中的点 并使用我的点数据结构更改了它:

int InsidePolygon(std::vector <Point> poly, Point p) {
  int i, j, c = 0;
  int nvert = poly.size();

  for (i = 0, j = nvert-1; i < nvert; j = i++) {

    if ( ((poly[i].y> p.y) != (poly[j].y> p.y)) && (p.x < (poly[j].x-poly[i].x) * (p.y-poly[i].y) / (poly[j].y-poly[i].y) + poly[i].x) )
       c = !c;

  }
  return c;
}

我已扩展它以包含 PolygonPolygon 函数,该函数将 1 个多边形的所有点与另一个多边形进行检查,然后将其反转以进行相反的检查。

int PolygonPolygon(std::vector <Point> polygon1, std::vector <Point> polygon2) {

    for(int i=0; i<polygon1.size();i++) {    
     if(InsidePolygon(polygon2, polygon1[i])) {
         return 1;
     }
    }
    for(int j=0; j<polygon2.size();j++) {       
     if(InsidePolygon(polygon1, polygon2[j])) {
         return 1;
     }
    }

  return 0;

}

奇怪的是,我的 PolygonPolygon 函数总是返回 1。所以我有几个问题:

  1. 我是否在某个地方搞砸了逻辑?我应该以不同的方式编写 PolygonPolygon 函数吗?

  2. 是否有更好的 Polygon 测试方法,多边形本身不能保证是凸的,这就是为什么我选择多边形中的点方法。我还希望确定最终哪个点发生碰撞,如果我能通过这一点。

  3. 我应该在 InsidePolygon 测试中以特定顺序呈现我的点吗?

I have been having a few issues implementing my narrow phase collision detection. Broadphase is working perfectly.

I have a group of polygons, that have a stl::vector array of points for their vertices in clockwise order. Every cycle, I check to see whether they're colliding.

I have borrowed the following Point in Polygon test from here and changed it using my Point data structures:

int InsidePolygon(std::vector <Point> poly, Point p) {
  int i, j, c = 0;
  int nvert = poly.size();

  for (i = 0, j = nvert-1; i < nvert; j = i++) {

    if ( ((poly[i].y> p.y) != (poly[j].y> p.y)) && (p.x < (poly[j].x-poly[i].x) * (p.y-poly[i].y) / (poly[j].y-poly[i].y) + poly[i].x) )
       c = !c;

  }
  return c;
}

I have extended that to include a PolygonPolygon function, which check all the points of 1 polygon against another and then reverse it to check the other way around.

int PolygonPolygon(std::vector <Point> polygon1, std::vector <Point> polygon2) {

    for(int i=0; i<polygon1.size();i++) {    
     if(InsidePolygon(polygon2, polygon1[i])) {
         return 1;
     }
    }
    for(int j=0; j<polygon2.size();j++) {       
     if(InsidePolygon(polygon1, polygon2[j])) {
         return 1;
     }
    }

  return 0;

}

The strange thing is that my PolygonPolygon function is always returning 1. So I have a few questions:

  1. Have I screwed up the logic somewhere? Should I write my PolygonPolygon function differently?

  2. Are there any better methods for a PolygonPolygon test, the polygons themselves are not guaranteed to be convex, which is why I went for the point in polygon method. I also hope to determine which point is colliding eventually, if I can get past this bit.

  3. Should I be presenting my points in a particular order for the InsidePolygon test?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨笨の傻瓜 2024-09-13 06:07:33

您可能需要考虑尝试在多边形之间画一条线作为替代碰撞检测方法。

[编辑] 哎呀,我错过了一个事实,即那里也有非凸多边形。也许“确定一个点是否位于多边形的内部”会更好吗?或者您可以先将非凸多边形分解为凸多边形。

此外,StackOverflow 上至少有一个类似的问题

You may want to consider trying to draw a line between polygons as an alternative collision detection method.

[edit] Oops, I missed the fact that you have non-convex polys in there too. Maybe "Determining if a point lies on the interior of a polygon" would be better? Either that or you could break your non-convex polygons up into convex polygons first.

Also, there's at least one similar question here on StackOverflow.

听不够的曲调 2024-09-13 06:07:33

谢谢你们的帮助!但我已经自己解决了。

将顶点平移到世界空间并旋转它们的重要性不应被忽视,特别是当它们发生碰撞时。

Thanks for your help guys! But i've managed to sort it out on my own.

The importance of translating your vertices to world space and rotating them should not be overlooked, especially if you're colliding them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文