How can I rotate Rectangles in Libgdx?

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

This is what I did:

 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()); 
+6
source share
3 answers

The other answer is basically correct; however, I had some problems with positioning polygons using this method. Just a clarification:

LibGDX does not support rotating rectangles when using Intersector to detect conflicts. If you need rotating rectangles, you should use Polygon to detect conflicts.

Building a rectangular polygon:

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

Remember to set the source 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); 

In addition, somewhere in your code, you most likely want to update the position of the collision polygon in accordance with your sprite:

 polygon.setPosition(x, y); 

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

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

Collision Detection:

ConvexPolygons () Intersector Overlays:

 boolean collision = Intersector.overlapConvexPolygons(polygon1, polygon2) 

As mentioned in another answer, this method only works if:

  • using convex polygons whose rectangle
  • polygon execution for polygons, for example: you cannot mix rectangles and polygons
+12
source

Rotation

You can create a Polygon from a rectangle or from a sprite (supplying the vertices for the polygon constructor) and use its 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

Conflict checks can be performed using the Intersector class:

 Intersector.overlapConvexPolygons(polygon1, polygon2) 

Remember that this method only works if:

  • you use convex polygons whose rectangle
  • you perform polygon checks of polygons, for example: you cannot mix rectangles and polygons
+5
source

I think something like this might help, I can’t check 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. You may need to adjust the start of rotation to

you can use render ShapeRenderer to see how the result looks like this:

add for test in variable class

 private ShapeRenderer sRDebugRectangel = new ShapeRenderer(); 

add for test when updating or drawing

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

can see my answer to this question to use formatting, otherwise known as

Libgdx, how can I create a rectangle from coordinates?

+1
source

Source: https://habr.com/ru/post/988255/


All Articles