-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature request] Rotation tweens should move in the direction of the shortest distance #2494
Comments
I wonder if anyone can assist with a hack or workaround. I'd really like to make use of the tween system instead of having to manually tween all my rotations (with a simple +/- angle in updates), which looks horrible without easing and all the other neat features the tween system has. Is there an easy way for me to implement this into core? I've scoured the tween code and couldn't find where something like adding/subtracting the properties of the tween target takes place. I'm running 2.4.7 btw. |
Just a reminder. I would really really really love to have this. :) I am currently doing manually tweens on sprites to get the rotation going the shortest distance and it is extremely problematic, especially over a network (now I need to communicate the direction of the rotation over the network). I miss simply tweening rotations/angles. It was so easy and the product was beautiful! |
You can do this yourself, even if you need to hide it behind some abstraction. Since we know angles can wrap and be negative, we can do ourselves a favour and use some math to solve this problem...
is the same as
Except the latter takes the longer route. :) Replace 3.14 w/ Pi and the number multiplied by 1.5 with your "target". You can use some basic math to figure out which is shorter. If the absolute distance is better to turn in one direction, use the positive sign. Otherwise, use the negative sign. Does this help? You can hide this behind a Using your method above BTW, you can simply created a |
Oh wow, thanks! I will try your solutions and report back. |
Great. Let me know if you have issues, I tested it and it works fine. |
I'm having difficulty understanding how to implement your solution. Suppose I have a sprite with rotation at 0. I want to tween its rotation to 1.5:
What am I doing wrong here? I really just don't understand it. Compare the absolute distance of what? Use positive/negative signs on what, the rotation target? I guess it would make much more sense to me if I saw a complete example. Thanks! |
i might be thinking wrong but probably your errors coming from here :
` Please try to add else statments and give it a try |
If the rotation is smaller than 0 then the rotation is already negative; therefore, the sign doesn't need to change. This is all based on my interpretation of hilts-vaughan's logic, but I don't understand the math. |
Let's deal with the math then and see the result: var diffTurnNegative = Math.abs(gameObject.rotation - turnNegative); var diffTurnNegative = +1.57; (Math.abs(0 - (-1.57)); which gives you a positive 1.57) Look at if statemants, you will see that you are going into else statment for these results, At the and your moveTween variable give the rotation value of 1.5. Hope you will figure out rest :) |
I've tried writing it like this:
No luck. |
Hi, I'll write a Phaser sandbox example. :) I think I explained the "multiply" poorly. See, you do not need to multiply by these values. I only did that because it allowed me to think in terms of "full circles" 0.5 = quarter circle etc... see the Phaser sandbox example below for a corrected algorithm. |
From one developer to another. <3 I've only tested it from "rotation 0" but it should work fine.. I think? The if might need tweaking if that's the case, but should be O.K. Let me know. There is probably an easier way but it is late and I am tired. Good luck. :) Pay it forward (http://phaser.io/sandbox/edit/zyzPzRcM) Don't understand the math and want to? Send me an e-mail, we can talk. |
Oh, and of course, hide this ugliness in another class somewhere. |
…given angles. Angles are in the range -180 to 180, which is what `Sprite.angle` uses. So you can happily feed this method two sprite angles, and get the shortest angle back between them (#2494)
Here's my version :)
Here's a full example (you can find the assets in the Phaser Examples repo)
|
Wooooooooooot! I just tested it now and works like a charm. Here is my example with Richard's method (note that I simply copied his function because it only exists in the dev repo now):
Thanks everyone and I'm so glad to get this issue worked out. It's been plaguing me for a while now! |
For anyone running into this now that hasn't discovered what I should've found much sooner.. Phaser 3 has the function: |
Suppose I have a tank and I want to turn it to a specific angle using a rotation tween. The problem with the tween system now is that it only adds or subtracts a value to achieve the target value, but that simply logic doesn't work when you are dealing with angles. The function below will tell you wether adding to the rotation is the shortest direction to turn in to achieve a target angle. Given a current and target angle, when returning true, the tween manager should add, when returning false, it should subtract. That way you don't have the tank do an almost 360 spin to turn less than 10 degrees! Hopefully this code can be implemented in core as I think it is intuitive the tweening should work this way.
The text was updated successfully, but these errors were encountered: