簡體   English   中英

Libgdx:如何在Scene2D的組元素周圍繪制矩形

[英]Libgdx: how to draw rectangle around group elements of Scene2D

我想知道如何檢查組內元素與游戲中其他元素之間的碰撞,換句話說,我想知道如何在組內元素周圍繪制矩形,導致每次嘗試時,矩形都是總是在錯誤的位置,我嘗試使用stageToLocalCoordinates,但是結果總是混亂不清 (有時我將矩形放置在正確的位置,但是當我移動組時,矩形似乎具有“鏡像效果” {朝相反方向移動} )

使用ShapeRenderer ..

在要返回矩形的組的所有元素中創建函數。

public Rectangle getBounds()
{
return new Rectangle(x,y,width,height);
}

現在,在帶有舞台的類中,在繪制元素(演員)之前先繪制矩形。

shapeRenderer.setProjectionMatrix(stage.getCamera().combined);
shapeRenderer.begin(ShapeType.Filled);
shapeRenderer.setColor(Color.BLUE); // put any color you want
// for each actor of your stage do this
 shapeRenderer.rect(actor.getBounds().x,actor.getBounds().y,actor.getBounds().width,actor.getBounds().height);

編輯:

要將矩形轉換為多邊形,您可以使用我之前制作的此方法

public static float[] rectangleToVertices(float x, float y, float width,
        float height) {
    float[] result = new float[8];
    result[0] = x;
    result[1] = y;

    result[2] = x + width;
    result[3] = y;

    result[4] = x + width;
    result[5] = y + height;
    result[6] = x;
    result[7] = y + height;

    return result;
}

Polygon poly=new Polygon(rectangleToVertices(.....));

將多邊形放入游戲畫面類中。

在渲染方法中設置多邊形位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM