How to rotate a sprite to an angle linearly (Not lerping)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Vignesh S

How do I rotate a sprite to an angle linearly like if its to rotate from 0deg to 90deg I want to rotate linearly at a speed of 10 deg/sec

What do you mean by not lerping? Do you mean by using add_torque?

Magso | 2020-07-04 22:18

So you want to do a linear rotation that does not involve linear interpolation?

Ertain | 2020-07-05 00:22

@Magso Its just a simple sprite not a Rigidbody2d so add_torque would not be present.
Also add_torque wont stop once it has reached the target angle in my case the global_mouse_position.

@Ertain yes I wanted to do a linear rotation like 90deg/sec or 75deg/sec towards the target angle and stop at target angle.

Vignesh S | 2020-07-05 06:52

:bust_in_silhouette: Reply From: Vignesh S

I finally got the solution for this problem. Took more than a day.

export var rotation_speed := 90.0
export var offset_angle := 0.0

func _ready():
	rotation_speed = deg2rad(rotation_speed)
	offset_angle = deg2rad(offset_angle)

func _process(delta):
	var target_angle = get_global_mouse_position().angle_to_point(position) + offset_angle
	if (rotation != target_angle):
		var angle_dif = -short_angle_dist(rotation, target_angle)
		if (abs(angle_dif) < 0.05f):
			rotation = target_angle
			rotation = rotation - rotation_speed * delta * sign(angle_dif)

func short_angle_dist(from, to):
	var max_angle = TAU
	var difference = fmod(to - from, max_angle)
	return fmod(2 * difference, max_angle) - difference