0 votes

I wanted to shrink a polygon, but because it's shape is like a skewed square,

enter image description here

I though of editing each of the 4 points to their half Y position, making them meet at the middle. For that I calculated half their scalar vectors for each point, and I have the following code so far:

extends Node2D

onready var NormalPolygon = $Polygon2D.polygon
onready var ShrunkPolygon = []

func _ready():
    for Point in NormalPolygon: #Returns Vector2 of each point.
        var HalfwayPoint = Point / 2 #Here later will be just the Point.Y/2
        ShrunkPolygon.append(HalfwayPoint)

    var i = 0
    while i < 3: #Don't know if the points starts at 0 or 1, because normally arrays starts at 0 index.

        Shrink(i)
        i += 1

func Shrink(i):
    $Tween.interpolate_property($Polygon2D,"polygon",$Polygon2D.polygon[i],ShrunkPolygon[i],2,Tween.TRANS_LINEAR,Tween.EASE_IN_OUT)
    $Tween.start()

In theory it should work, but the problem is that $Polygon2D.polygon returns a copy of the points, not a reference.

How can I edit the Polygon2D points?

I even though of using just the size, but the square looks like is getting squashed rather than shrinked.

in Engine by (332 points)

Do you want to know how to set the points of a Polygon, or how to animate them with a Tween?
Because it's easy to just set the position of points, but I don't know if it's even possible to animate them with a tween.

Yea, the Tween idea is to animate it smoothly... I have a shader material masked with the polygon2d texture.

1 Answer

+1 vote

So the workaround is to do it with the AnimationPlayer, we can keyframe the Polygon2D PoolVector2Array(), and it will deform through the frames with the continuous motion, but I don't know why we can't do it through gdscript using a tween.

by (332 points)

Just as a reference for anyone looking for additional info:

https://godotengine.org/qa/55810/set-polygoncollider2ds-polygon-variable-in-gdscript

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.

Categories