0 votes

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

asked Jul 4 in Engine by Vignesh S (75 points)

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

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

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

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

1 Answer

0 votes
Best answer

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
        else:
            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
answered Jul 5 by Vignesh S (75 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.