0 votes

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

adderVec2 = adder.getpolygon()
levelVec2 = levelBase.get
polygon()

func _input(event):
    if event.is_action_pressed("leftclick"):
        adder.set_position(get_global_mouse_position())
        mergeResult = Geometry.merge_polygons_2d(adderVec2,levelVec2)
        levelBase.set_polygon(mergeResult)
        print(levelBase.get_polygon())

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?

asked Aug 28, 2019 in Engine by spiderbyte87 (33 points)

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

1 Answer

+1 vote

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.

answered Aug 28, 2019 by Xrayez (1,236 points)
edited Nov 29, 2019 by Xrayez

I was wondering if transformpoints2d 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!!

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

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.

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.

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

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

I get Invalid call. Nonexistent function 'transformpoints2d' in base '_Geometry'.

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.