Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libgdx shaperenderer How to rotate rectangle around its center?

Tags:

java

libgdx

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.

like image 588
JustOneMan Avatar asked Dec 30 '12 14:12

JustOneMan


2 Answers

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.

like image 83
user1067800 Avatar answered Nov 09 '22 23:11

user1067800


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);
like image 44
rluks Avatar answered Nov 10 '22 01:11

rluks