How to create an unfilled Polygon2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jgodfrey

As a learning project, I’m recreating the old Atari classic Asteroids in Godot. That has always been my “Hello World” app when I start playing with a new game engine… ;^)

Originally, I just created typical transparent PNG files with white outlines for the typical Asteroids assets (ship, rocks, enemy, …).

But, I have the vector coordinates for all of those assets and noticed that Godot has a simple, Polygon2D node that I can use to consume the coordinates in place of a Sprite node.

A few quick changes and I nearly have what I want, using my original vector coordinates and Polygon2D nodes in place of my sprites. Except…

I don’t see a way to simply draw the polygon outlines. They’re always solid filled. Ideally, the Polygon2D would just have a boolean “filled” property and maybe a “strokeWidth” property, but that doesn’t seem to be the case. With that in mind, I assume if I continue down the vector path, I’ll need to draw my own polygons in-game?

Advice appreciated.

If I need to draw my own polygons, a push to the appropriate classes / methods would be great.

And, FWIW, I’m really enjoying Godot.

Thanks.

:bust_in_silhouette: Reply From: Zylann

There is Line2D to draw lines with many options, but it would still require some scripting to make it render a closed polygon (and more if the polygon is hollow). I think at the moment the best option is to draw them yourself. Nobody seems to have requested that feature on Polygon2D, so maybe it’s time?

Thanks for the input. I’ll take a look at Line2D.

Also, I just made a feature request here:

Option for unfilled Polygon2D node · Issue #30667 · godotengine/godot · GitHub

jgodfrey | 2019-07-17 23:51

@Zylann,

Looking at Line2D, I think it’s capable of exactly what I want/need. It seems I can simulate a closed, outline-only polygon by just adding one more point to the end of the coordinate array that repeats the first point’s position.

Additionally, I can set the color and the line width. Really, that’s all I think I need at the moment.

With that in mind, I guess I don’t understand some of the potential issues you eluded to above. I’m probably missing the points you were trying to make.

I will say that the final in-game appearance of the Line2D drawn items don’t seem to be quite as “clean” as the sprites they replaced. I’m not yet sure why that is. Perhaps something to do with anti-aliasing, which Line2D doesn’t seem to support (?).

jgodfrey | 2019-07-18 14:26

It seems I can simulate a closed, outline-only polygon by just adding one more point to the end of the coordinate array that repeats the first point’s position.

There is actually more to do for this to be accurate. If you create a fake point just to close the line, you will produce overdraw. This node claims no overdraw at all, which allows to make it transparent without artifacts. You should be fine if you don’t use transparency or textures.

What do you mean by “clean”? If you mean anti-alias, I’m not sure if it’s even supported in 2D, at least for polygons. Line2D uses polygons to generate its visual, not “lines”. That allows it to have round caps, texture and configurable thickness.
To draw actual OpenGL 1px-thick lines, you could use draw_line() inside a _draw() function.

Zylann | 2019-07-18 18:45

There is actually more to do for this to be accurate. If you create a fake point just to close the line, you will produce overdraw. This node claims no overdraw at all, which allows to make it transparent without artifacts. You should be fine if you don’t use transparency or textures.

Understood. Yeah, since I’m not using any transparency, this isn’t an issue for me, but I see how it could be for other cases.

What do you mean by “clean”?

Like I said above, I’m not really sure what I mean by that yet, but it definitely doesn’t look quite as nice after replacing the sprites with Line2D. I think it may be AA related, but I need to run the two variations in parallel to get a better feel. I will say the Line2D version has more motion-flicker than the sprite-based version, but it’s more than that. I’ll post some pics if I find anything more definitive.

If you mean anti-alias, I’m not sure if it’s even supported in 2D, at least for polygon

I’ve seen a few discussions on this. If that’s the case, why does Polygon2D support an Antialiased property? And, related, why does Line2D not have such a property?

To draw actual OpenGL 1px-thick lines, you could use draw_line() inside a _draw() function.

Thanks - good to know. Though, I may just switch back to sprite-based representations for both simplicity and quality.

jgodfrey | 2019-07-18 20:01

I’ve seen a few discussions on this. If that’s the case, why does Polygon2D support an Antialiased property? And, related, why does Line2D not have such a property?

I tried this property and it didnt have any effect ^^" In fact, when looking through the source code, it’s not even used by anything. Maybe it used to work in an old version but none of the redone renderers supported that.
There is no such parameter on Line2D maybe for the same reason, but maybe nobody thought of adding it either.

Zylann | 2019-07-18 20:14

I tried this property and it didnt have any effect

Ah, interesting. Perhaps it has a moderate placebo effect.

TIcks the AA option…
“Aah, yeah, that’s much better…”
Never looks back…

While the pursuit of vector-based rendering has been interesting academically, I’ll probably go back to the sprite-based representations. Really, there’s no reason not to. I just found it interesting to reuse the original vector coords I had in some other, long-ago incarnation of this game…

Thanks for the continued input.

jgodfrey | 2019-07-18 20:24