# Tween Rotation / Shortest Distance?

## Recommended Posts

Hi @enpu / Panda People

Just wondering how I would go about tween the rotation of a sprite back to 0, by the shortest distance.

I'm pretty sure that right now, the tween always takes the same direction - even if its the longest way around.

##### Share on other sites

What you want to do is to rotate your sprite to 0 or Math.PI * 2 (which is whole turn, 360 degrees), depending on which one is closest to the current rotation.

First you should convert your current rotation to value between 0 - Math.PI * 2. You can do that by using modulus operator

``sprite.rotation = sprite.rotation % (Math.PI * 2);``

Then just check if the rotation is more than Math.PI (which is half turn, 180 degrees). If it is, rotate to Math.PI * 2, if not then rotate to 0.

``````sprite.rotation = sprite.rotation % (Math.PI * 2);

var target = 0;
if (sprite.rotation > Math.PI) target = Math.PI * 2;

rotation: target
}, 500).start();``````

##### Share on other sites

@enpu - Ahhh I get it so Math.PI * 2 is different than 0.

I was under the impression that when it reaches 360 degrees it resets to 0 again.

Thanks for this!!

##### Share on other sites

I've noticed that when I rotate a sprite ...

this.playerShip.sprite.rotation += this.shipSpeed * Math.PI;

the rotation number keeps incrementing, instead of resetting.

So, two clockwise circles will be 720 degrees.

Do I have to reset manually?

##### Share on other sites

You don't have to reset anything. That's why i first converted the rotation value in the example above, so no matter how high the value is

``sprite.rotation = sprite.rotation % (Math.PI * 2);``

After this, it will be between 0 - Math.PI * 2.

##### Share on other sites

@enpu - Nice, I understand now ## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.