I've been working on a soft-body physics engine in between other stuff for quite a while now. I've come to a rather interesting problem, how do I fill the polygon? This polygon (defined by two arrays of coordinates) is guaranteed to be simple but not necessarily convex. For now just knowing how to fill it a solid colour will do.
The obvious thing would be to just do a triangle-fan using all the points, this fails though because it may be concave.
Another thing I had to do was calculate the area of the polygon, to do this I used a formula I came across in my research:
While convincing myself how this formula worked I had an idea of how to draw the polygon. I could draw an inverted triangle-fan as areas which were included an even number of times would not be part of the polygon. Here's the code I came up with:
for (j = 0; j < number; j += 1)
And here's proof that it works: (No my soft-body doesn't look quite like that, it's just a random concave polygon I made to test the script.)
So, problem solved ... Well not quite yet.
This is fine if my background's a solid colour, but it won't always be. If the background is textured, the polygon will also be textured (not the desired outcome).
To ensure that the polygon is always a solid colour, I could each step:
- Create a surface the right size.
- Clear this surface black, alpha 0.
- Draw the inverted polygon to the surface.
- Draw that surface with the colour I want.
- Destroy the surface.
I have not yet implemented this but it sounds awfully expensive to be creating and destroying a surface every step. Not to mention the inconvenience of having to do some of it in the end-step event as you cannot change render targets during the draw event.
So any ideas? Am I going about this the right way or am I missing something?