【问题标题】:How can I rotate Rectangles in Libgdx?如何在 Libgdx 中旋转矩形?
【发布时间】:2015-05-31 06:42:45
【问题描述】:

我将我的精灵旋转了 90 度,我想对我的矩形做同样的事情,以便能够将它们用于碰撞,但 rotate() 方法不适用于矩形。

这就是我所做的:

treeSpr=new Sprite(new Texture(Gdx.files.internal("tree.png")));
        treeSpr.setPosition(250,700);
        treeSpr.rotate(90f); 

//Rectangle
 treeRect=new Rectangle(treeSpr.getX(),treeSpr.getHeight(),
                treeSpr.getWidth(),treeSpr.getHeight());

【问题讨论】:

  • 你试过使用 Box2D 吗?
  • 您好,您可以实现一个自己的 Rect 类或...我不知道您是如何进行旋转的。但是您可以使用旋转和平移矩阵,因此可以处理碰撞,就好像您的精灵仍处于 0 度并使用普通矩形
  • 不,我还没有尝试过Box2d,你能给我一个代码示例吗?抱歉,我是 Java 和游戏开发的新手
  • 你想对矩形做什么?另一个建议是使用可以轻松旋转和缩放的 Image 类型的 Actor。

标签: java android libgdx


【解决方案1】:

另一个答案基本正确;但是,我在使用该方法定位多边形时遇到了一些问题。只是一些澄清:

LibGDX 在使用 Intersector 进行碰撞检测时不支持旋转矩形。如果您需要旋转矩形,则应使用 Polygon 进行碰撞检测。

构建一个矩形多边形:

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

现在你可以旋转碰撞多边形了:

polygon.setRotation(degrees);

此外,在您的代码中,您可能希望更新碰撞多边形的位置以匹配您的精灵:

polygon.setPosition(x, y);

我们甚至可以在屏幕上绘制多边形(出于调试目的):

drawDebug(ShapeRenderer shapeRenderer) {
    shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
    shapeRenderer.polygon(polygon.getTransformedVertices());
    shapeRenderer.end();
}

碰撞检测:

Intersector的overlapConvexPolygons():

boolean collision = Intersector.overlapConvexPolygons(polygon1, polygon2)

如另一个答案中所述,此方法仅在以下情况下有效:

  • 使用凸多边形,矩形是
  • 执行多边形到多边形检查,例如:你不能混合矩形和 多边形

【讨论】:

  • 与其单独调用setPosition(x, y),不如在Rectangle的位置正确创建Polygon?构造函数的float[] vertices参数见[libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/…
  • 另外请记住,Polygon.setRotation(degrees) 方法的作用与Sprite.rotate(degrees) 方法不同。前者将设置旋转而不考虑当前旋转,而后者将旋转增加指定的度数。
【解决方案2】:

旋转

您可以从矩形或精灵创建一个Polygon(为多边形构造函数提供顶点)并使用它的rotate(float degrees) 方法:

treePoly = new Polygon(new float[] {
               treeRect.x, treeRect.y,
               treeRect.x, treeRect.y + treeRect.height,
               treeRect.x + treeRect.width, treeRect.y + treeRect.height,
               treeRect.x + treeRect.width, treeRect.y
           });

treePoly.rotate(45f);

碰撞检测

然后可以通过Intersector 类进行碰撞检查:

Intersector.overlapConvexPolygons(polygon1, polygon2)

但请记住,此方法仅在以下情况下有效:

  • 你使用convex polygons,矩形是这个
  • 您对多边形进行检查,例如:您不能混合使用矩形和多边形

【讨论】:

    【解决方案3】:

    我认为类似的东西可以提供帮助,我现在无法测试,

    //Rectangle
     treeRect=new Rectangle(treeSpr.getX(),
                            treeSpr.getY(),
                            treeSpr.getHeight(), //now is change width by height
                            treeSpr.getWidth());  //now is change height by width
    

    注意:您可能需要调整两者的旋转原点

    您可以使用渲染 ShapeRenderer 来查看结果是否符合预期:

    添加用于变量类中的测试

    private ShapeRenderer sRDebugRectangel = new ShapeRenderer();
    

    添加以进行更新或绘制测试

    sRDebugRectangel.begin(ShapeType.Filled);
    sRDebugRectangel.identity();
    
    sRDebugRectangel.rect(yourRectangle.getX(), 
                          yourRectangle.getY(),
                          yourRectangle.getWidth(),
                          yourRectangle.getHeight());
    
    sRDebugRectangel.end();
    

    可以查看我对这个问题的回答以使用 shaperrender,也称为:

    Libgdx, how can I create a rectangle from coordinates?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多