0 votes

Hey,
i want to increase the value of the rotation from the current rotation value to a certainly value slowly (so that it looks like an animation or so. Here is my code:

             extends KinematicBody2D


func _ready():
set_fixed_process(true)
set_process_input(true)


func _input(event):

   var rectangle = get_rot()

   if event.type == InputEvent.MOUSE_BUTTON && event.is_pressed():
    set_rot(rectangle + 1.57)
in Engine by (17 points)

2 Answers

0 votes

set_rot uses radians, you need a tiny value to make a small movement or use rotd in degrees, also you have to add to the previous rotation value.
rotate adds to the current rotation.

If rotating during (any) _process, you can use an integrator method to get smooth movement, in short, think on how fast should rotate in a second and multiply by the time between process.

All that means, inside process:

rotate(ammount_in_radians*delta)

or

set_rot(get_rot()+ammount_in_radians*delta)

or

set_rotd(get_rotd()+ammount_in_degrees*delta)

You may need to read about basic motion control in games to understand more what is all about (mostly basic math and physics applied to games).

by (7,888 points)
0 votes

You want to interpolate the angle. Vectors have an angle_to() method that does the work of having to write a utility function to calculate the difference between two angles.

From there you want to multiply in some amount between 0 and 1 to get yourself a slice of that angle difference. Then add that to the current rotation.

This is done in radians, so be mindful of that, as the other poster pointed out. If you prefer degrees, and don't have to worry about overhead, then there is the deg2rad() function that can be helpful.

If you're doing this as a continuous effect, then you will want to pick some turning speed, and maybe adjust it. If you're doing animation, then you can use tweening and easing algorithms to provide you with a "t" (time) value.

I recently wrote an example for myself to do this for some other application. Maybe you will find this helpful.

extends Node2D

var turning_speed = .04

var orientation = Vector2(0,1)
var line_distance = Vector2(0,0)

var sprite = Sprite.new()
var sprite_pos

func _ready():

    var screen_size = get_viewport().get_visible_rect().size

    sprite.set_texture(preload("res://icon.png"))
    sprite.set_pos(Vector2(screen_size.x / 2, screen_size.y / 2))
    sprite_pos = sprite.get_pos()
    add_child(sprite)

    set_process(true)

func _process(delta):

    var m = get_viewport().get_mouse_pos()

    m -= sprite_pos
    line_distance = m

    m = m.reflect(m)

    var new_angle = orientation.angle() + orientation.angle_to(m) * turning_speed

    orientation.x = sin(new_angle)
    orientation.y = cos(new_angle)

    sprite.set_rot(orientation.angle())
    update()

func _draw():
    draw_line(sprite_pos, sprite_pos + line_distance, Color(.01, .2, .6, .8), 2)
by (5,232 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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.