How to rotate rectangle around its center? I found rotate function in ShapeRenderer:
void rotate(float axisX, float axisY, float axisZ, float angle);
but it rotates around 0,0 coordinate, and I want rotating shape around its center.
If you look at the documentation for ShapeRenderer the second example shows you how to set the center of the box at position {20, 12, 2} and rotate around the z-axis using translate. You need to do the same thing e.g.
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.
Use this method (official docs):
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. The x and y specify the lower left corner. The originX and originY specify the point about which to rotate the rectangle.
Use it like this: (x and y is 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With