【问题标题】:how to create a basic polygon?如何创建基本多边形?
【发布时间】:2015-07-01 18:39:43
【问题描述】:

我想使用 overlapse() 创建一个与矩形碰撞的多边形。我唯一的问题是我不知道如何创建一个多边形,我的意思是只有一个多边形。每次我试图在互联网上找到它时,我都会找到有史以来最复杂的代码。所以如果你能告诉我如何创建一个多边形并解释如何声明它的形状,那将不胜感激。

多边形看起来像这样:

注意:它应该是对称的,但我不确定我画的是否对称......

【问题讨论】:

  • 你认为“多边形”是什么意思?
  • 任何不圆的形状,如果你这样问,我想我会发现我完全是哑巴?
  • 好吧,你没有错。但是在编程中,我们通常处理三角形......我不知道 libgdx 在碰撞检测方面提供了什么,但是对于您提供的形状,您将需要非常复杂的数学来检测碰撞。
  • 其实我只是可以声明一个多边形和一个矩形然后做 if(Rect.overlapse(Poly)){
  • 真的吗?这实际上很整洁,我可能需要看看那个库:P

标签: libgdx collision-detection overlap polygons


【解决方案1】:

根据多边形类上的documentation,您可以简单地使用它的构造函数来定义一个新的多边形。示例:

//we want to create a simple square shaped polygon
//we create an array to hold every vertex we need, there are 4 vertices
//this constructor uses 1 float array as parameter, where every two element define the position of 1 vertex
polygon = new Polygon(new float[]{ //so we pass a new float array to the constructor
                      0,0, //this is the x,y of the first vertex
                      width,0, //the second vertex
                      width,height, //the third
                      0,height}); //and the last

这是一个简单的示例,但您应该能够弄清楚如何扩展形状。我再举一个例子来添加一个额外的顶点来创建一个“房子形状”:

polygon = new Polygon(new float[]{ //so we pass a new float array to the constructor
                      0,0, //this is the x,y of the first vertex
                      width,0, //the second vertex
                      width,height, //the third
                      (width*0.5),(height*1.5), //this one is now extra, it adds one vertex between the last and the one before it
                      0,height}); //and the last

如果您需要更多信息,请告诉我。

【讨论】:

  • 很抱歉回复晚了,但非常感谢。我明白了,但有没有办法测试多边形?看看是不是我想要的形状?
  • 我的意思是,如果你可以暂时将其可视化
猜你喜欢
  • 2015-08-30
  • 1970-01-01
  • 2015-08-02
  • 2021-11-08
  • 2015-03-22
  • 1970-01-01
  • 2021-01-16
  • 2019-10-14
  • 2019-08-10
相关资源
最近更新 更多