+1 vote

Hi everyone,

i'm trying to animate the "polygon" property of Polygon2D node.
At first I tried through tweening, but it seems the Tween method cannot work with "polygon" property and packedArrays in general (apparently this problem was already known -> #5501).
Then i tried with AnimationPlayer, and witnessed 2 kind of behaviors :

1 - If you add the AnimationPlayer node and you set the animation of the "polygon" property through tracks and keys with the GUI -> it works.

2 - but if you try to do all the previous things programmatically (GDScript) without adding manually the nodes and tracks/keys -> it half works : the polygon vertices go abruptly from first to final values, without any interpolation/smoothness (even with the track set to Animation.INTERPOLATIONLINEAR and Animation.UPDATECONTINUOUS).

To make sure my code was not wrong, i also tried to animate programmatically "color" and "position" properties of Polygon2D -> it worked perfectly.
Below the code i'm using if someone have an idea to how solve this problem :

extends Node2D

var poly
var polyW = 100
var polyH = 100
var polyPoints1:= [Vector2(0,0), Vector2(polyW,0), Vector2(polyW,polyH), Vector2(0,polyH)]
var polyPoints2:= [Vector2(0,0), Vector2(polyW,0), Vector2(polyW*3,polyH*3), Vector2(0,polyH)]
var animPlayer
var anim
var colorTrackName = 'Polygon2D:color'
var moveTrackName = 'Polygon2D:position'
var polyTrackName = 'Polygon2D:polygon'
var track_index0 = 0
var trackIndex1 = 1
var trackIndex2 = 2
var keyTime01 = 0.0
var keyTime02 = 0.5
var polyPosition = Vector2(0, 0)
var polyPositionNew = Vector2(500, 200)
const RED = Color.red
const YELLOW = Color.yellow

func _ready() -> void:
#   *********************************** ADD Polygon2D NODE ***********************************
    poly = Polygon2D.new()
    poly.name = "Polygon2D"
    poly.polygon = polyPoints1
    poly.offset = Vector2(10,10)
    poly.color = RED
    add_child(poly)
#   ******************************************* END ******************************************

#   ******************************** ADD AnimationPlayer NODE ********************************
    animPlayer = AnimationPlayer.new()
    add_child(animPlayer)
#   ******************************************* END ******************************************

#   ************************* ADD 'move' ANIMATION in AnimationPlayer ************************
    anim = Animation.new()
    animPlayer.add_animation("move", anim)
    var animList = animPlayer.get_animation_list()
#   ******************************************* END ******************************************

#   ********************* ADD 'Polygon2D:color' TRACK and KEYS in 'move' ANIMATION ********************
    anim.add_track(Animation.TYPE_VALUE)
    anim.track_set_path(track_index0, colorTrackName)
    anim.track_insert_key(track_index0, keyTime01, 0)
    anim.track_insert_key(track_index0, keyTime02, 1)
    anim.track_set_key_value(track_index0, 0, RED)
    anim.track_set_key_value(track_index0, 1, YELLOW)
#   ******************************************* END ******************************************
#
#   ******************* ADD 'Polygon2D:position' TRACK and KEYS in 'move' ANIMATION *******************
    anim.add_track(Animation.TYPE_VALUE)
    anim.track_set_path(trackIndex1, moveTrackName)
    anim.track_insert_key(trackIndex1, keyTime01, 0)
    anim.track_insert_key(trackIndex1, keyTime02, 1)
    anim.track_set_key_value(trackIndex1, 0, polyPosition)
    anim.track_set_key_value(trackIndex1, 1, polyPositionNew)
#   ******************************************* END ******************************************

#   ******************** ADD 'Polygon2D:polygon' TRACK and KEYS in 'move' ANIMATION *******************
    anim.add_track(Animation.TYPE_VALUE)
    anim.track_set_path(trackIndex2, polyTrackName)
    anim.track_insert_key(trackIndex2, keyTime01, 0)
    anim.track_insert_key(trackIndex2, keyTime02, 1)
    anim.track_set_key_value(trackIndex2, 0, polyPoints1)
    anim.track_set_key_value(trackIndex2, 1, polyPoints2)
    anim.track_set_interpolation_type(trackIndex2, Animation.INTERPOLATION_LINEAR)
    anim.value_track_set_update_mode(trackIndex2, Animation.UPDATE_CONTINUOUS)
#   ******************************************* END ******************************************

func _process(_delta: float) -> void:
    if Input.is_action_just_released("ui_substract") or Input.is_action_just_released("ui_add"):
        animPlayer.play("move")

P.S : Even though "color" and "position" properties worked perfectly without it, i nonetheless forced "polygon" track with Animation.INTERPOLATIONLINEAR and Animation.UPDATECONTINUOUS but the result is the same).

Hopfully someone could help me :)

Godot version 3.5.1
in Engine by (15 points)

Please log in or register to answer this question.

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 Frequently asked questions and 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 [email protected] with your username.