(3.2) is merge_polygons_2d() functioning or am I just doing it wrong?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By spiderbyte87

Just playing around with it to see if I can do a union on two polygon2d nodes.

adderVec2 = adder.get_polygon()
levelVec2 = levelBase.get_polygon()

func _input(event):
	if event.is_action_pressed("leftclick"):
		mergeResult = Geometry.merge_polygons_2d(adderVec2,levelVec2)

With this code the output is [(0,0)]
If I print out the result of the mergeResultprior to passing it to levelBase, I get an array that’s different from the two input polygons but isn’t a union.

I’m lost and thinking I may be playing with something that just isn’t ready for prime time. Is it functional and I’m just not understanding it?

Feel free to report an issue if you think you’ve stumbled upon a bug, with reproduction project.

Xrayez | 2019-08-28 10:54

:bust_in_silhouette: Reply From: Xrayez

The actual coordinates that define a polygon are actually local, the position of Polygon2D doesn’t automatically shift all the coordinates defined by the polygon for it to be represented in global coordinates, you can use an overridden xform method for translating an array of points.

Edit: transform_points_2d is removed since 3.2.beta in favor of xform, see this PR.

if event.is_action_pressed("leftclick"):
    var trans = Transform2D(0.0, get_global_mouse_position())
    var polyB = trans.xform(adder.get_polygon())
    mergeResult = Geometry.merge_polygons_2d(levelVec2, polyB)

Similarly the levelVec2 polygon needs to be transformed perhaps.

Also, merge_polygons_2d will return an empty array if neither polygon overlap.

I was wondering if transform_points_2d was the key to this. I’ll check it out a little later and let you know if it works. Thank you so much and thank you for your contributions to Godot I never thought I’d get a response from the person actually making this happen! You rock!!

spiderbyte87 | 2019-08-28 14:49

Thanks! You can download a test project in this pull request where transform_points_2d is actually used, just for reference.

Xrayez | 2019-08-28 14:56

I took a look at your example and I’m still confused.

Basically I’m trying to move polygon A to the mouse position on click, merge it with polygon B, then modify the vertices on the polygon B to match the merge.

spiderbyte87 | 2019-08-28 19:48

Not sure what exactly you’re confused with.

func polygon_at_mouse_position(p_polygon):
    var offset = Transform2D(0.0, get_global_mouse_position())
    var polygon = Geometry.transform_points_2d(p_polygon, offset)
    return polygon

This function should essentially “move” any polygon to mouse position (returns modified vertices themselves). Ideally you shouldn’t change the position of Polygon2D nodes while doing this as well.

Xrayez | 2019-08-28 20:19

I’ll give that a try. Thank you for your help and patience.

spiderbyte87 | 2019-08-28 22:44

The Geometry.transform_points_2d() doesn’t seem to be merged (or functional) as of 3.2beta2.

I get Invalid call. Nonexistent function ‘transform_points_2d’ in base ‘_Geometry’.

gururise | 2019-11-29 06:27