Libgdx shaperenderer How to rotate a rectangle around its center?

How to rotate a rectangle around its center? I found the rotate function in ShapeRenderer:

void rotate(float axisX, float axisY, float axisZ, float angle); 

but it rotates around the coordinate 0,0, and I need a rotating shape around its center.

+7
source share
2 answers

If you look at the documentation for the ShapeRenderer, the second example shows how to set the center of the field at {20, 12, 2} and rotate the z axis around using translation. You need to do the same, for example.

 this.m_ShapeRenderer.begin(ShapeType.Rectangle); this.m_ShapeRenderer.setColor(1.f, 1.f, 1.f, 1.f); this.m_ShapeRenderer.identity(); this.m_ShapeRenderer.translate(20.f, 10.f, 0.f); this.m_ShapeRenderer.rotate(0.f, 0.f, 1.f, 45.f); this.m_ShapeRenderer.rect(x, y, 40.f, 20.f); this.m_ShapeRenderer.end(); 

Hope this helps.

+10
source

Use this method ( white papers ):

 public void rect(float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float degrees) 

Draws a rectangle in the x / y plane using ShapeRenderer.ShapeType.Line or ShapeRenderer.ShapeType.Filled. X and y indicate the lower left corner. Origin X and originY indicate the point at which you want to rotate the rectangle.

Use it like this: (x and y are the point in the center of the rectangle)

 renderer.rect(x-width/2, y-height/2, width/2, height/2, width, height, 1.0f, 1.0f, myRotation); 
+3
source

All Articles