Examples of correct inputs for draw_polygon

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Judd Gledhill
:warning: Old Version Published before Godot 3 was released.

Hello,

I am hoping to use Node2D.draw_polygon() to draw in a nice overlay for a section of my screen. I am calculating an outline for an area, and feeding that set of points ([Vector2]) to the draw_polygon method. I have verified that the method gets the data correctly and that it looks like a complete shape, but I am getting a “Bad Polygon!” error from the engine.

Here is my code:

#ROOM.GD
extends Node2D

var _outline_polygon = Vector2Array()
var _colors = ColorArray([Color(0,1,0,.5)])
var _drawing = false

func _ready():
	add_to_group("rooms")
	set_process(true)
	pass

func set_polygon(poly_path):
	#generate the polygon that represents this room
	_outline_polygon = poly_path
	print(get_name(), ": ", _outline_polygon)
#	_drawing = true

func _process(delta):
	update()
	
func _draw():
	if _drawing:
		draw_polygon(_outline_polygon, _colors)

Here is an example of the content of the _outline_polygon variable:

room_8: [(256, 448), (320, 448), (320, 448), (384, 448), (384, 448),
(384, 512), (384, 512), (384, 576), (384, 576), (320, 576), (320,
576), (256, 576), (256, 576), (256, 512), (256, 512), (256, 448)]

Here is the error I am getting - sorry about the image link, it is a screen shot. But basically says “Bad Polygon!” and seems to be coming from something called: visual_server_raster.cpp in the back end.

Any ideas? What should I be passing into the function to get a nice polygon out on the other side?

:bust_in_silhouette: Reply From: Zylann

Your polygon has duplicate points.
After experiencing a bit, I found draw_polygon will work only if those conditions are fulfilled:

  • There must be no duplicate points
  • The area must not be zero
  • The polygon must not fold onto itself
  • The number of points you provide must be the same as the number of colors (even if all colors are the same). The same rule applies to UVs if provided.