How can I rotate Rectangles in Libgdx?

前端 未结 3 1897
你的背包
你的背包 2021-02-06 11:58

I rotated my sprite 90 degrees and I want to do the same with my rectangle to be able to use them for collision, but the rotate() method is not available on rectang

相关标签:
3条回答
  • 2021-02-06 12:09

    The other answer is basically correct; however, I had some issues with the positioning of the polygons using that method. Just some clarification:

    LibGDX does not support rotated Rectangles when using the Intersector for collision dectection. If you need rotated rectangles, you should use the Polygon for collision detection instead.

    Building a Rectangular Polygon:

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

    Don't forget to set the origin of the Polygon if you are going to rotate it:

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

    Now you can rotate the collision polygon:

    polygon.setRotation(degrees);
    

    Also, somewhere in your code, you will likely want to update the position of the collision polygon to match your sprite:

    polygon.setPosition(x, y);
    

    We can even draw our polygon on screen (for debug purposes):

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

    Collision Detection:

    The overlapConvexPolygons() of the Intersector:

    boolean collision = Intersector.overlapConvexPolygons(polygon1, polygon2)
    

    As mentioned in the other answer, this method only works if:

    • using convex polygons, which the rectangle is
    • performing polygon to polygon checks, e.g.: you cannot mix rectangles and polygons
    0 讨论(0)
  • 2021-02-06 12:25

    Rotation

    You could create a Polygon from the rectangle or from the sprite (supplying the vertices in order for the polygon constructor) and use it's rotate(float degrees) method:

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

    Collision Detection

    Collision checks then could be done via the Intersector class:

    Intersector.overlapConvexPolygons(polygon1, polygon2)
    

    Keep in mind though, this method only works if:

    • you use convex polygons, which the rectangle is
    • you do polygon to polygon checks, e.g.: you cannot mix rectangles and polygons
    0 讨论(0)
  • 2021-02-06 12:32

    I think something like it can help, I can not test now,

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

    Note: may You need to adjust the origin of the rotation for both

    you can use a render ShapeRenderer to see if the result is as expected:

    add for test in variable class

    private ShapeRenderer sRDebugRectangel = new ShapeRenderer();
    

    add for test in update or draw

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

    can look at my answer to this question to use a shaperrender otherwise known as:

    Libgdx, how can I create a rectangle from coordinates?

    0 讨论(0)
提交回复
热议问题