碰撞检测的多边形位置不会更新?还是碰撞?

时间:2017-08-29 16:49:03

标签: java libgdx

我使用多边形作为碰撞检测方法。我有一块石头,朝某个方向移动。

当我运行应用程序时,多边形渲染成一个完美的矩形,岩石就在它开始的时候。

然而,它并没有移动 摇滚。

public Polygon box;
private int width;
public int height;
public float [] vertices;

public Rock (float x, float y, int width, int height) {
    this.width = width;
    this.height = height;
    position = new Vector2(x, y);
    velocity = new Vector2(20, 0);

    vertices = new float[] {position.x, position.y, position.x+width, position.y, position.x+width, position.y+height,
            position.x, position.y+height};
    box = new Polygon ();        
    box.setOrigin(width/2, height/2);

}

public void RockMove (float delta) {
    position.add(velocity.cpy().scl(delta));
    box.setVertices(vertices);
    box.setPosition(position.x, position.y);

}

sprite sprite批次

public void render (float delta) {
    batch.begin();
    batch.enableBlending();
    batch.draw(rock, rock.GetX(), rock.GetY(), rock.GetWidth(), rock.GetHeight());
    batch.disableBlending();
    batch.end();

}

这是渲染类的一些代码

shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
    shapeRenderer.setColor(Color.RED);
shapeRenderer.polygon(Rock.vertices);

shapeRenderer.end();

同样,多边形完美显示,但不会移动。我没有任何错误。

编辑:我在Rock Move方法中再次定义了顶点,现在它们随着岩石一起移动。

public void RockMove (float delta) {
    position.add(velocity.cpy().scl(delta));
    box.setVertices(vertices);
    box.setPosition(position.x, position.y);

vertices = new float[] {position.x, position.y, position.x+width, 
position.y, position.x+width, position.y+height,
            position.x, position.y+height};

}

但由于某种原因,碰撞检测无法正常工作。

1 个答案:

答案 0 :(得分:1)

不要在更新中再次定义顶点,而应使用多边形的transformedVertices。

Polygon polygon=new Polygon();
float transformed[] = polygon.getTransformedVertices();

TransformedVertices是应用缩放,旋转和位置转换后多边形的顶点。

相关问题