How can I rotate Rectangles in Libgdx?

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

提交回复
热议问题