How can I rotate Rectangles in Libgdx?

前端 未结 3 1898
你的背包
你的背包 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: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?

提交回复
热议问题