• Godot Help
  • Way to make smooth rotation in asteroids style movement

Hi to the best peoples of the world
Want find way to make rotation like this
I maked smooth slow down but can't figure how make smooth rotations
Very glad to a little help , with example if it's possible +_- ,

That's code of player ship movement

func _physics_process(delta):
if !alive: return

var input_vector := Vector2(0, Input.get_axis("move_up", "move_down"))

velocity += input_vector.rotated(rotation) 	
velocity = velocity.limit_length(max_speed)

if Input.is_action_pressed("rotate_right"):
	rotate(deg_to_rad(rotation_speed*delta))

if Input.is_action_pressed("rotate_left"):
	rotate(deg_to_rad(-rotation_speed*delta))

#smooth slow down
if input_vector.y == 0:
	velocity.x = lerpf(velocity.x, 0, 0.012)
	velocity.y= lerpf(velocity.y, 0, 0.012)

move_and_slide()

7 days later

To achieve smooth rotations for your player ship, you can use interpolation to gradually change the rotation speed. Here's an updated version of your code with smooth rotations:

extends KinematicBody2D

var max_speed = 200
var rotation_speed = 200
var velocity = Vector2()

func _physics_process(delta):
if !alive:
return

var input_vector = Vector2(0, Input.get_action_strength("move_up") - Input.get_action_strength("move_down"))

velocity += input_vector.rotated(rotation) * max_speed * delta
velocity = velocity.clamped(max_speed)

if Input.is_action_pressed("rotate_right"):
    rotate(deg2rad(rotation_speed * delta))

if Input.is_action_pressed("rotate_left"):
    rotate(deg2rad(-rotation_speed * delta))

if input_vector.y == 0:
    var target_rotation = 0
    if velocity.length_squared() > 0:
        target_rotation = velocity.angle()
    rotation = lerp_angle(rotation, target_rotation, 0.1) # Adjust the third parameter for smoothness

move_and_slide(velocity)

This code introduces lerp_angle() to smoothly interpolate between the current rotation and the target rotation when there's no input for rotation. Adjust the third parameter of lerp_angle() to control the smoothness of the rotation. Lower values will result in smoother but slower rotations, while higher values will make the rotations faster but less smooth. Experiment with different values to find the right balance for your game. Hope this helps