# libGDX - How to rotate with a specific duration

I want to rotate my player to 90° for a specific duration. I know scene2d provides this, but I don't want to use it for my players.

With how much to rotate every frame, so after x seconds the rotation is finished? The game can run on different FPS also.

• Why not use Actions ? – Sebastien Servouze Feb 11 '17 at 13:29

Do you want to do a basic rotation of an image or using an Animation of Textures? If it is just a rotation of an image, JoeBode1337's answer is a good one (with a while instead of the if). If you are looking for an Animation of Textures:

    rotateSheet = new Texture(Gdx.files.internal("playerRotation.png"));

TextureRegion[][] tmp = TextureRegion.split(rotateSheet, rotateSheet.getWidth()/nbrOfColumnsInRotateSheet, rotateSheet.getHeight()/nbrOfRowsInRotateSheet);

int nbrOfRotationPosition = nbrOfRowsInRotateSheet*nbrOfColumnsInRotateSheet;

rotateFrames = new TextureRegion[nbrOfRotationPosition];
int index = 0;
for (int i = 0; i < nbrOfRowsInRotateSheet; i++) {
for (int j = 0; j < nbrOfColumnsInRotateSheet; j++) {
rotateFrames[index++] = tmp[i][j];
}
}

rotateAnimation = new Animation((float)x/nbrOfRotationPosition, rotateFrames);
spriteBatch = new SpriteBatch();
stateTime = 0f;

spriteBatch.begin();
while((stateTime += Gdx.graphics.getDeltaTime()) < x){
currentFrame = rotateAnimation.getKeyFrame(stateTime, true);
spriteBatch.draw(currentFrame, player.posX, player.posY)
}
spriteBatch.end();


You can use the timing system described here (this uses plain Java): https://stackoverflow.com/questions/23932887/libgdx-how-to-calculate-elapsed-time

Basically, set startTime = System.currentTimeMillis() before doing the rotation (but don't do it at the start of your program) and each time in your loop you can check to see if the amount of seconds, x, has passed.

Just do something like this (where x represents the seconds the object has to rotate):

    if((System.currentTimeMillis() - startTime)/1000 <= x) {
//do rotation logic
}


The division by a thousand converts it to seconds (1000 milliseconds in 1 second).

Noticed that there wasn't an answer for this question so I am going to provide something simple that will work at any FPS as you suggested:

You could do something simple involving what angle you want to move to, that being 90°, and the duration you want to complete that movement in.

Taking the your designated time, say it's 3 seconds, you can convert that to frames using the given delta with Gdx.graphics.getDeltaTime() or the given delta in the render loop:

frameTime = timeInSeconds * (1 / Gdx.graphics.getDeltaTime());
// 3 * (60) = 120 ( which is how many frames over 3 seconds if your running at 60 fps)


Note, the getDeltaTime() and delta variable are 1 / fps, so you need to get the inverse of the delta to get the fps.

Then you can take your designated rotation amount, 90°, and find the amount it needs to rotate every frame:

degreesEveryLoop = 90 / frameTime;
// 90 / 120 = .75 (So if you wanted the player to move 90° over 3 seconds then you would need to move .75° every frame


Then you would just need to apply the amount to the player every frame