Hey,
Basically for a floor generation algorithm, I’m allowing users to place points in any order they would want, which is not a problem, I’ve commissioned someone to help with the more math-heavy concepts and they wrote a triangulation and rectilinear decomposition algorithm, it was just my job to actually implement it which is going fine thus far.
The problem I’m having is that, when determining the winding order of this polygon, it gets marked as the opposite of what it actually is, so a counterclockwise polygon is marked as clockwise and clockwise marked as counterclockwise. This isn’t the issue, I can just return the inverse so the winding order is correct. What I need is someone to explain why it returns the inverse.
For reference, I’m using this solution for finding the winding order. In essence, you need to do (x1 - x2) * (y1 + y2) for one and the next vertex of the polygon, then add each of the results. If the final sum is greater than 0, clockwise, if less than 0, counterclockwise. Implementing this seems to work fine, however the result appears to always be the inverse of what it needs to be, why is this?
Here’s my implementation:
local function getWindingOrder(...: Vector3)
local packed = {...}
local sumNow = 0
for i = 1,#packed do
local x1,y1,x2,y2 =
packed[i].X,
packed[i].Z, -- we take z because the plot can be summarized into a 2D plane by using z instead of y
packed[i + 1] and packed[i + 1].X or packed[1].X, -- in case it's the final iteration we need to loop back to the beginning
packed[i + 1] and packed[i + 1].Z or packed[1].Z
local x, y = x2 - x1, y2 + y1
sumNow += x * y
end
print(sumNow > 0 and 'Clockwise' or 'Counter clockwise') -- this is where the issue arises. if I place points in clockwise it'll get marked as ccw as well as ccw marked as cw
end
For example, these points are placed in counter clockwise but the output says “clockwise” (ignore the part that’s generated):

If placed in the opposite order, output says “counter clockwise”:

I saw in the replies of the solution that if you are working with a plane whose Y (in our case Z bc translating 3D to 2D) axis is reversed, the opposite is true; >0 is ccw, <0 is cw. I think this might be the case but I wasn’t able to find anything on the CFrame documentation. Could anyone confirm/deny/offer an explanation