[英]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.