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?