Ninjadoodle Posted March 27, 2018 Share Posted March 27, 2018 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. Quote Link to comment Share on other sites More sharing options...
enpu Posted March 27, 2018 Share Posted March 27, 2018 @Ninjadoodle 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; game.Tween.add(sprite, { rotation: target }, 500).start(); Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted March 27, 2018 Author Share Posted March 27, 2018 @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!! Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted March 27, 2018 Author Share Posted March 27, 2018 @enpu 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? Quote Link to comment Share on other sites More sharing options...
enpu Posted March 27, 2018 Share Posted March 27, 2018 @Ninjadoodle 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. Ninjadoodle 1 Quote Link to comment Share on other sites More sharing options...
Ninjadoodle Posted March 27, 2018 Author Share Posted March 27, 2018 @enpu - Nice, I understand now Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.