LibGDX - properly using Polygon class

妖精的绣舞 提交于 2019-12-05 03:05:34

问题


I have created Polygon object to wrap my airplane (size of airplane's TextureRegion is 256x74, but size of this one in a game is 70x20). So:

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

After that in my update function I update position of it:

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

Then I render polygon to know where it is:

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

At the end I create 2 airplanes and make them fly to each other and every iteration I try to detect collision like below:

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

I see that 2 rectangles move to each other and intersect, but overlapConvexPolygons function doesn't work! Why?


回答1:


I've solved this problem. I incorrectly specified vertices. I needed to get rectangular polygon, so I had to use following:

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

and do not forget set origin if you are going to rotate polygon object:

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

Now it works perfect!



来源:https://stackoverflow.com/questions/14359852/libgdx-properly-using-polygon-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!