LibGDX – 正确使用Polygon类

我创建了Polygon对象来包裹我的飞机(飞机的TextureRegion大小是256×74,但是这个游戏的大小是70×20)。 所以:

 TextureRegion[] texRegsAirplane = TextureRegion.split(textureAirplane, 256, 74); Rectangle bounds = new Rectangle(0, 0, 70, 20); Polygon polygon = new Polygon(new float[]{0,0,bounds.width,0,bounds.width,bounds.height,0,bounds.height,0,0}); 

在我的updatefunction之后,我update它的位置:

 public void update(float delta){ Vector2 v = getPosition(); v.add(velocity); polygon.setPosition(vx, vy); } 

然后我渲染多边形以了解它的位置:

 public void render(SpriteBatch spriteBatch, float pixelPerUnitX, float pixelPerUnitY){ spriteBatch.draw(testTexture,polygon.getX()*pixelPerUnitX, polygon.getY()*pixelPerUnitY, polygon.getBoundingRectangle().width*pixelPerUnitX,polygon.getBoundingRectangle().height*pixelPerUnitY); } 

最后,我创建了2架飞机,让它们相互飞行,每次迭代我都会尝试检测碰撞,如下所示:

 public void detectCollision(){ for(Airplane airplane1 : Airplanes){ for(Airplane airplane2 : Airplanes){ if(Intersector.overlapConvexPolygons(airplane1.getPolygon(), airplane2.getPolygon())){ //COLLISION DON'T HAPPEN!!! } } } 

我看到2个矩形相互移动并相交,但overlapConvexPolygons函数不起作用! 为什么?

我已经解决了这个问题。 我错误地指定了顶点。 我需要得到矩形多边形,所以我不得不使用以下内容:

 polygon = new Polygon(new float[]{0,0,bounds.width,0,bounds.width,bounds.height,0,bounds.height}); 

如果要旋转多边形对象,请不要忘记设置原点:

 polygon.setOrigin(bounds.width/2, bounds.height/2); 

现在它完美无缺!