Rotate an image clockwise using LibGDX

How can we rotate the image clockwise using LibGDX? what am I looking for when the image is loaded, suppose a star, I need to rotate it from the beginning of the screen to the end of the screen horizontally, with a star rotating, how can I do this in libgdx?

+5
source share
8 answers

When drawing textures using SpriteBatch, you can use one of the drawing functions, which includes rotation. This javadoc has all the drawing features: SpriteBatch

You can save the variable for position and rotation, as well as increase the rotation and the x-component of the position at each rendering, so that it rotates when moving horizontally.

+10

batch.draw(sprite,(Gdx.graphics.getWidth() - sprite.getRegionWidth()) / 2.0f,(Gdx.graphics.getHeight() - sprite.getRegionHeight()) / 2.0f,sprite.getRegionWidth()/2.0f,sprite.getRegionHeight()/2.0f, sprite.getRegionWidth(), sprite.getRegionHeight(), 1f, 1f,count, false);

if(count < 0.0f)
count = 360.0f;
else
count --;

private float count =360.0f;
+3

Libgdx , : Scene2D Image Stage. Image Actor, Action:

Image myImage = new Image(myTexture);
myImage.addAction(Actions.parallel(Actions.moveTo(endX, endY, duration), Actions.rotateBy(degrees, duration)));
myImage.setPosition(startX, startY);
myImage.setOrigin(sizeX/2, sizeY/2);
stage.add(myImage);

render stage.act(), , , ... Actor, stage.draw(), draw() Actor s. Image allready draw(), .

Scene2D, :
int rotationSpeed /
int moveSpeed / (, , , )
float angle, Texture Vector2 position, x y Texture.
x y, Vector2 direction, Vector, x y, .

render(float delta) :

angle+=delta*rotationSpeed;
angl%=360;      // Limits the angle to be <= 360
while (angle < 0)  // Unfortunally the "modulo" in java gives negative result for negativ values.
    angle+=360;
position.x+=direction.x*moveSpeed*delta;
position.y+=direction.y*movSpeed*delta;
spriteBatch.draw(yourTextureRegion, position.x, position.y, sizeX/2, sizeY/2, sizeX, sizeY, scaleX, scaleY, angle);

rotationSpeed angle+= angle-=.

, .

+3

.

Sprite sprite = new Sprite(textureRegion, 0, 0, 128, 128);
sprite.setPosition(++mX, 0);
angle++;
sprite.setRotation(angle);
sprite.draw(batcher);
+1

:

sprite.setOrigin(sprite.getWitdh() /2f, sprite.getHeight() /2f);
sprite.setPosition( 0, 200 ); //200 it a example

()

sprite.setX( sprite.getX() + delta ).setRotation( sprite.getRotation() + delta ); 
0

libgdx. :

img.setOrigin(getWidth/2,getHeight/2);

And then you can rotate clockwise and counterclockwise according to your needs:

img.rotate(2f); or img.rotate(-2f);
0
source

So, the following sample worked for me (infinite rotation)

Method 1: (recommended)

loadingActor.addAction(Actions.repeat(RepeatAction.FOREVER, Actions.rotateBy(360, 1)));

Method 2:

Image loadingActor = new Image(AssetsController.getInstance().getLoading());
loadingActor.setOrigin(Align.center);
final SequenceAction infiniteRotate = Actions.sequence();
infiniteRotate.addAction(Actions.rotateTo(0 , 0f) );
infiniteRotate.addAction(Actions.rotateTo(360 , 1f) );
loadingActor.addAction(Actions.forever(infiniteRotate));
0
source

All Articles