LibGDX - 正确使用 Polygon 类

2024-05-06

我创造了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!!!
            }
    }
}

我看到两个矩形彼此移动并相交,但是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);

现在它完美运行了!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

LibGDX - 正确使用 Polygon 类 的相关文章

随机推荐