java - How can I rotate Rectangles in Libgdx? -


i rotated sprite 90 degrees , want same rectangle able use them collision, rotate() method not available on rectangles.

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

the other answer correct; however, had issues positioning of polygons using method. clarification:

libgdx not support rotated rectangles when using intersector collision dectection. if need rotated rectangles, should use polygon collision detection instead.

building rectangular polygon:

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

don't forget set origin of polygon if going rotate it:

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

now can rotate collision polygon:

polygon.setrotation(degrees); 

also, somewhere in code, want update position of collision polygon match sprite:

polygon.setposition(x, y); 

we can 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 intersector:

boolean collision = intersector.overlapconvexpolygons(polygon1, polygon2) 

as mentioned in other answer, method works if:

  • using convex polygons, rectangle
  • performing polygon polygon checks, e.g.: cannot mix rectangles , polygons

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -