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

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?

in Engine by (36 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.

by (1,369 points)
edited by

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.
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.