4

我已经创建Polygon了包裹我的飞机的对象(飞机的尺寸TextureRegion是 256x74,但在游戏中这个尺寸是 70x20)。所以:

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});

之后在我的update函数中我更新它的位置:

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

然后我渲染多边形以知道它在哪里:

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功能不起作用!为什么?

4

1 回答 1

8

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

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);

现在它完美无缺!

于 2013-01-16T14:43:30.460 回答