简体   繁体   中英

LibGDX: How do I change a shapes X Y co-ordinates after rotating it in LibGDX?

I'm making a game through LibGDX and working with Java in Eclipse. I'm using the ShapeRenderer class to create a normal rectangle and rotate it continuously. I am using the logic given from the LibGDX docs to rotate the rectangle:

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/glutils/ShapeRenderer.html#identity--

Drawing the rectangle is done by:

shapeRenderer.rect(x, y, width, height);

However, the example given in docs to rotate the rectangle requires the following code:

shapeRenderer.rect(-width / 2, -height / 2, width, height);

You can see the code to rotate the shape requires the "x" and "y" parameters to have the width and height of the rectangle, this means that I lose control of where I want to position the rectangle whilst it rotates (granted the rectangle does rotate exactly how I want it to just not where I want it to). The rectangle's POSITION because of this code is now proportional to its SIZE. Is there any way around this because I need to give the rectangle a given size and a different x, y co-ordinate. Thank you in advance.

You use "translate" to place the rectangle where you want it after the rotation. From the docs to draw a rectangle at 20,12,2 that rotates on the z axis you would do the following

shapeRenderer.begin(ShapeType.Line);
shapeRenderer.identity();
shapeRenderer.translate(20, 12, 2);
shapeRenderer.rotate(0, 0, 1, 90);
shapeRenderer.rect(-width / 2, -height / 2, width, height);
shapeRenderer.end();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM