# How to merge two polygons?

Attention Topic was automatically imported from the old Question2Answer platform.

I am trying to merge two polygons to use it as a polygon for polygon navigation instance.
(Becasue I need to change the navigation polygon instance in runtime)

My code doesn´t work and I think it´s because the method Geometry.merge_polygons_2d return an Array, so how I can get a PoolVector2Array from the merged polygons?

``````var polygonA = Polygon2D.new()
var verticesA = PoolVector2Array([Vector2(0, 0), Vector2(0, 50), Vector2(50, 50), Vector2(50, 0)])
polygonA.set_polygon(verticesA)
polygonA.global_position = Vector2(100,200)

var polygonB = Polygon2D.new()
var verticesB = PoolVector2Array([Vector2(0, 0), Vector2(0, 50), Vector2(50, 50), Vector2(50, 0)])
polygonB.set_polygon(verticesB)
polygonB.global_position = Vector2(100,250)

var mergedPolygon = Polygon2D.new()
mergedPolygon.set_polygon(Geometry.merge_polygons_2d(polygonA.polygon, polygonB.polygon))
mergedPolygon.global_position = Vector2(100,200)
``````

You should be able to just pass the array to the PoolVector2Array constructor just as you did before:

``````var verticesC = PoolVector2Array(Geometry.merge_polygons_2d(polygonA.polygon, polygonB.polygon))
``````

Is not working and is printing [(0, 0)]

Pelli | 2021-12-07 02:17

If problem lied in incorrect variable type You would get an error. I don’t know about this set_polygon function, is it new in Godot 3.4 ? Or custom one ? You can ensure, if resulting PoolVectorArray is assigned to shape2d.points.
If it is, and it still doesn’t work, print this PoolVectorArray. I was playing with Geometry in the past and I rememeber it required tedious translation into global_position.

But it may all be in vein, because if not changed in Godot 3.4, it was not really possible to change Navigation at running time. It was baked straight from editor after compillation.

Okay, I am not getting an error, if I add the child polygon A and B I can see them. But Adding mergedPolygon is not showing.

Is not custom, I am very nob with polygons, I searched the docs and the way it makes one is with set_polygon or polygon = ….
Polygon2D — Documentación de Godot Engine (4.x) en español
Anyway it’s working with A and B so the problem is the merge function

So you are telling me that I cannot make the player go to a position with pathfinding, with the areas to avoid (or go through) implemented at runtime?

Pelli | 2021-12-06 21:36

But what does it say in console when You print result of merging ?
As I said, I remember that merging in Geometry required translation of positions. If You merge 2 polygons made by points in global positions result will be empty array. You will have to center them first, merge, and retranslate back to their positions

Yes, Dynamic obstacles are not something Navigation2D used to handle. But there are much more pathfinding options. Try Astar, or add collision avoidance using raycasts/test_move. However I am not updated about 3.4, I can’t say if Nav2D was upgraded there.

Inces | 2021-12-06 22:08

First of all thanks for your time and your help.

``````print(Geometry.merge_polygons_2d(polygonA.polygon, polygonB.polygon))
``````

is printing: [[(50, 50), (0, 50), (0, 0), (50, 0)]]
Is like is not merging anything

I am going to explain what I need so maybe you can have an idea of what to do. I am making a builder game, so you have to put rooms in the world, when you put some rooms, I need to go, with a character, from a room to a room, but I need to have pathfinding or something related because the rooms can variate a lot, and maybe the character have to go to an upper level or a lower level, I was moving the character with tween and detecting walls (Areas2D), but now that I have levels the think is more difficult.

Imgur: The magic of the Internet

So for example I need to go from red to green or from yellow to green. But as is a builder game, maybe there is going to be al ot of paths to go from any level to green and I have to find the shorter.

here another example of 3 different paths to take (the player putted the rooms at runtime so I cannot use a tilemap)

Imgur: The magic of the Internet

Pelli | 2021-12-07 02:37

So this is exactly what I thought : Geometry finds polygons being in separate dimensions, unable to merge. I would tell You how to translate them into local space before merging, but after seeing what Your game is going to be, I can surely say - drop Navigation2D, get into Astar. Astar can be intimidating, but this type of pathfinding should be easy even with it. In Astar You make points in space and choose connections between them. Every time You will build a room, You will create a point, get surrounding Astar points and connect them with new room in your pattern. Astar functions return array of points associated with positions, so You will iterate through it with Tween to code final movement. Astar can even intake additional arguments, to make some paths less picked, to simulate them being slower or harder terrain ( like stairs or tranversable obstacles ).

Inces | 2021-12-07 06:34

Sounds amazing, thanks for you solution and your detailed description, I am implementing it.

Pelli | 2021-12-07 12:53