



Reply From: 
David Krause 
I’ve solved it after studying this: Tilemap to Collision Geometry · Issue #5 · a327ex/blog · GitHub
extends Node2D
# https://github.com/SSYGEN/blog/issues/5
var edges = []
var grid = [
[0,0,0,0,0,0],
[0,1,1,1,1,1],
[0,1,1,1,1,1,1,1,1,1,1],
[0,1,1,1,1,1],
[0,1,1,1,1,1,1],
[0,1,1,1,1,1],
]
func getPoints(x, y, cellSize):
#1 2
#
#0 3
return [
Vector2(x * cellSize.x, y * cellSize.y + cellSize.y), # 0
Vector2(x * cellSize.x, y * cellSize.y), # 1
Vector2(x * cellSize.x + cellSize.x, y * cellSize.y), # 2
Vector2(x * cellSize.x + cellSize.x, y * cellSize.y + cellSize.y) # 3
]
func getLines(points):
return [
[points[0], points[1]],
[points[1], points[2]],
[points[2], points[3]],
[points[3], points[0]]
]
func createEdges(grid, cellSize = Vector2(48, 48)):
var edges = []
for y in range(grid.size()):
for x in range(grid[y].size()):
var tile = grid[y][x]
if tile == 1:
for line in getLines(getPoints(x, y, cellSize)):
edges.append(line)
return edges
func deleteEdges(edges):
# print(edges.size())
# print("EDGES 1:", edges)
var markForDeletion = []
for currentLineIdx in range(edges.size()):
var currentLine = edges[currentLineIdx]
var currentLineInverted = [currentLine[1], currentLine[0]]
for lineIdx in range(edges.size()):
var line = edges[lineIdx]
if lineIdx == currentLineIdx: continue # skip ourself
if currentLine == line or currentLineInverted == line:
markForDeletion.append(currentLine)
markForDeletion.append(currentLineInverted)
for line in markForDeletion:
var idx = edges.find(line)
if idx >= 0:
edges.remove(idx)
# print(edges.size())
return edges
func toShape(edges):
var result = []
var nextLine = edges[0]
for idx in range(edges.size()):
# # find the "next" point
for otherLine in edges:
if otherLine == nextLine: continue
if nextLine[1] == otherLine[0]:
print("the next line should be:", otherLine)
nextLine = otherLine
break
elif nextLine[1] == otherLine[1]:
print("next line is reversed:", otherLine)
nextLine = [otherLine[1], otherLine[0]]
for point in nextLine:
result.append(point)
return result
func _ready():
# Called when the node is added to the scene for the first time.
# Initialization here.
update()
func _draw():
var edges = createEdges(grid)
var cleanedEdges = deleteEdges(edges)
var shape = toShape(cleanedEdges)
var colors = PoolColorArray()
for p in shape:
colors.append(Color(1,1,1))
print(edges)
print(shape)
var toDraw = PoolVector2Array(shape)
print(toDraw)
draw_multiline(toDraw, Color(1,1,1) )
draw_polygon(toDraw, colors )