Rotate Image Clockwise using LibGDX

时间秒杀一切 提交于 2019-11-28 12:26:05
Rahat Ahmed

When you draw the Texture with your SpriteBatch, you can use one of the draw functions that includes rotation. This javadoc has all the draw functions: SpriteBatch

You can keep a variable for position and rotation, and increase the rotation and x component of the position each time you render to make it rotate while moving horizontally.

Libgdx gives you more then one way to do that: You can use Scene2D and add an Image to your Stage. Image is a subclass of Actor, so you can add Actions to it:

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);

In render you can then call stage.act(), which updates the position, rotation, scale... of all your Actors and then call stage.draw() which will call draw() for all your Actors. Image allready handles the draw() so you don't need to care about that anymore.

You can also do it without scene2d, by updating everything yourself:
You can store a int rotationSpeed in degrees/sec
You can store a int moveSpeed in units/sec (maybe pixel but i would suggest to use camera or viewport and use your own unit, which is equal on all devices)
Store the float angle, which is the current rotation of your Texture and store a Vector2 position, which contains the x and y position of your Texture.
If you want to move in x and y direction you can also store a Vector2 direction, which is a normalized Vector, giving the percent of movement in x and y direction, but thats a different story.

Then in your render(float delta) you update everything:

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);

For clockwise rotation simply use a negative rotationSpeed or replace the angle+= with angle-=.

Hope it helps.

Siddharth

Following is the implementation to rotate any sprite

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 --;

Initially set counter to

private float count =360.0f;

You can also use the Scene2D actions. I have an example here with asteroid-type thing falling down the screen and rotating.

http://www.netthreads.co.uk/2012/02/09/libgdx-scene2d-demo-with-scene-transitions/

Nitin Sharma

To rotate anticlockwise and horizontally.. create a textureRegion then

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

You can do it too like this:

on your create method

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

on your render(float delta)

sprite.setX( sprite.getX() + delta ).setRotation( sprite.getRotation() + delta ); 
Sudhir singh

Here is a simple to rotate an actor in libgdx. First you need to set the origin:

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

And then you can rotate clockwise and anticlockwise according to your need:

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

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));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!