+1 vote

I am attempting to move the vertices of a CollisionPolygon2d in code (GDScript).

The process of creating the polygon2d is a bit complicated. It involves reading tiles of a tileset and converting them to a polygon. The shape generated is the expected one but the given position of the vertices seems to me totally random and the documentation on that function is scarce (opaquetopolygons)

Say that the process for creating the CollisionPolygon2d returns the following square (but the solution should be general to be applied to any shape).

CollisionPolygon2d position is 0.0
CollisionPolygon2d polygon is:
Vertex 0 = (0,416)
Vertex1 = (32, 416)
Vertex2 = (32,384)
Vertex3 = (0,384)

And I would like to keep the same square shape, with origin at 0,0 but as follows:
Vertex 0 = (0,32)
Vertex1 = (32, 32)
Vertex2 = (32,0)
Vertex3 = (0,0)

Reading the documentation I have reached to a partial solution using Transform 2D and xform. But the solution is partial because it relies on picking up the "correct vertex" of the polygon (which is easy for a square but more tricky for more complex shapes).

var polygon_to_translate = collision.get_polygon()
collision.set_polygon( Transform2D(0, Vector2(0,384)).xform_inv(polygon_to_translate) )

Does anyone know a better solution?

Thank you.

Godot version 3.3
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 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.