Lerp rotation above 180 degrees

Attention Topic was automatically imported from the old Question2Answer platform.

I’m making a node point towards the mouse angle, and I’m using lerp to ease the rotation of the node. But when the mouse angle changes from a value above 179, the node rotates all the way around instead of just adding + 1 on the node’s angle. Is there a built in function to ease rotations without this happening?

I had the exact same issue, and could only solve it with some if statements testing for crossing -PI and +PI (using radians)
I basically ask: if the change in angle is more the 180 (PI) then go the other way around.

Two things I’ve noticed:
if you use radians, the engine doesn’t seem to mind if values go far above or below PI, it still rotates correctly. In which case you might start by setting all the rotations simply far above PI so they always stay positive. Then you can add or subtract the change relative to your mouse.

Good luck

Still struggling with this:

if the change in angle is more the 180 (PI) then go the other way
around.

How exactly are you telling it to go other way around?

Ankeris | 2019-10-24 22:09

Old question, but I’ll leave it here for anyone dealing with this problem.

I don’t know how to explain it in a formal way, with trigonometry and all that, but you can take a look here if want to understand it: Tau Day | No, really, pi is wrong: The Tau Manifesto by Michael Hartl

When you are in the inspector, you can see that any rotation “resets” when crossing the 0/180 degrees threshold. When you are rotating something by code and want to avoid weird behaviours due to this angles, you can use the `lerp_angle` function:

Hi there, I am still dealing with this problem lol…

I have a boss I want to aim at the player… follows him around etc… I have the same rotation reset where in a point it goes all the way the other way instead of +1 to the side…

here my code: the 1.57 is a correction coz my player sprite is rotated

``````func _aim_at_player():
if stage.has_node("Player"):
var player = stage.get_node("Player")
var aim_dir = (player.position - self.position).angle() - 1.57
var current = self.rotation
\$Tween.interpolate_property(self,"rotation",current,aim_dir,1,Tween.TRANS_LINEAR,Tween.EASE_OUT)
\$Tween.start()
else: pass
``````

Surtarso | 2021-03-06 23:03

Actually I just fixed it by removing the tween and replacing with:

``````self.rotation = lerp_angle(current,aim_dir,0.02)
``````

I’ll just leave this here for future reference I guess

Surtarso | 2021-03-06 23:22

Thank you so much!

Using lerp_angle() instead of lerp() fixed it in my case as well

gimgones | 2022-04-02 08:54