Polygons, can point order be reversed?

I’ve designed a few maps for a golf game and have drawn some polygons for the sections. In some cases (because I didn’t realize it at the time) I drew some polygons with points clockwise and some counterclockwise. Turns out rotation order is important for my graphics engine.

I’m wondering if there’s a shortcut or technique to reverse the vertex order of a polygon in Tiled? I’m thinking a little plugin script that adds a new button to the UI? Just wondering if there are any thoughts on this.

There’s no built-in option to reverse vertex order. You wouldn’t need to write a plugin (C++/binary compatibility required), you can do this with a script (JavaScript, the same script will work in any recent enough version).

Here’s such a script, it adds a “Reverse Vertex Order” action to the object right-click menu. The option will appear for all objects because I’m too lazy to hide it, but it won’t modify objects that aren’t Polygons. It will modify all selected Polygons, so you can reverse the order for many objects at once. It keeps the first point the same, and only reverses the order of the other points.
ReverseVertexOrder.js (1.1 KB)

2 Likes

That’s fantastic it works excellently! Thank you.

Even going forward, it’ll be nice to just draw the shape freely in any direction and then fix it afterword.

Thanks again :pray:

1 Like

My first thought would have been to just use the Array.reverse, like:

obj.polygon = obj.polygon.reverse()

But due to the first/last point not being duplicated in the polygon, it changes the location of the “first” point. I guess it will usually not matter, though it is a little unexpected to see that marked location change.

Anyway, great solution!

I guess if this action is relevant, this is also a vote for that old issue:

I guess if this action is relevant, this is also a vote for that old issue

Hmm, a polygon (open or closed) but with labeled points, and points arbitrarily orderable, possibly even with each point having custom properties. That sounds like it could be useful for 2d paths, like a train or railcar path where certain parts have custom speeds or custom data attached to them, but also a lot of work.

I can’t say much on the topic. I haven’t run into that yet (or are aware of what Tiled already has), but for me the vertex order reversing is a practical addition. I would vote for that to be added to the object’s right-click menu, or as an icon button on the top bar (next to mirror and rotate buttons), along with a disable/enable only when a polygon is selected.

It would be a lot of UI work, yes. Currently, for paths where each point needs its own properties, one can use Points instead of a Polyline, with each Point having an object reference to the next point on the path. It’s less convenient due to the need to set up those references, though.