0 votes

I try to draw a colored polygon usingCanvasItem. drawcoloredpolygon.
But i get the error message:

E 0:00:01.129   canvas_item_add_polygon: Invalid polygon data, triangulation failed.
  <C++-Fehler>  Condition "indices.empty()" is true.
  <C++-Quellcode>servers/visual/visual_server_canvas.cpp:770 @ canvas_item_add_polygon()
  <Stacktrace>  Water.gd:38 @ _draw()

This is my code:

var pool_points = PoolVector2Array()
pool_points.append(Vector2(10,0))
pool_points.append(Vector2(-10,0))
pool_points.append(Vector2(10,10))
pool_points.append(Vector2(-10,10))
draw_colored_polygon(pool_points, Color("#29adff"))
in Engine by (335 points)

1 Answer

+1 vote
Best answer

Thats because the order of the points you gave doesnt correspond to a shape that cannot be easily interpreted. Try this other order:

var pool_points = PoolVector2Array()
pool_points.append(Vector2(10,0))
pool_points.append(Vector2(-10,0))
pool_points.append(Vector2(-10,10))
pool_points.append(Vector2(10,10))
draw_colored_polygon(pool_points, Color(1,1,1,1))

If you follow your original order of points, and close the polygon with the starting point, you get overlapping paths, like here

by (3,503 points)
selected by

is there a function for checking if an triangulation is possible?
Because im generating a wave effect and it seems that sometimes it is not possible to triangulate.

Yes, you could try with Geometry.triangulate_polygon function:

var pool_points = PoolVector2Array()
pool_points.append(Vector2(10,0))
pool_points.append(Vector2(-10,0))
pool_points.append(Vector2(10,10))
pool_points.append(Vector2(-10,10))

if Geometry.triangulate_polygon(pool_points).empty():
    print("failed")
else:
    print("triangulated")
    draw_colored_polygon(pool_points, Color(1,1,1,1))

is there any easy way to fix the points that dont triangulate? Or at least find which points are the problem?

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 Frequently asked questions and 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 [email protected] with your username.