Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotate Image Clockwise using LibGDX

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

like image 887
Alex Mathew Avatar asked Feb 25 '12 14:02

Alex Mathew


4 Answers

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.

like image 71
Rahat Ahmed Avatar answered Oct 15 '22 17:10

Rahat Ahmed


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.

like image 25
Robert P Avatar answered Oct 15 '22 15:10

Robert P


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;
like image 3
Siddharth Avatar answered Oct 15 '22 15:10

Siddharth


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/

like image 1
alrutherford Avatar answered Oct 15 '22 16:10

alrutherford