Reading the post again, it looks like you are checking to see if a point is within a polygon - but that check alone is not good enough because it allows for tunneling. It also doesn't tell you how far the player can move before colliding with the wall corner. |
That's pretty much it. To prevent tunneling, I'm taking a larger polygon that contains player polygon both before and after moving. You do lack a bit of information after detecting a collision, so you have to do what you are already doing, except that now you only have to check zero or several points, instead of N. Like I said, gain is insignificant as there is no point in having absurd numbers of lines.
Which would be a false assumption for some of my test cases (and likely some practical cases in my game). |
Don't 1 pixel wide walls look ugly?
Edit: I just realized that this problem is what linear programming does. I think a better optimization can come from looking at it like this. I'll give it a try.
Edit: Here's a suggestion.
Construct a polygon with my make_poly. Then for each line L from MAP, do this:
For each point P in polygon, calculate P is_on_the_left_side_of L. If some results are true and some are false, there could be an intersection (there would be if that line was infinite). Assuming that you filter lines by distance and your map is not full of random spikes, only a few lines will go past this point. 7*5*N operations performed so far, instead of 2*3*13*N.
Now do the same thing for the two lines that go through ends of L and are perpendicular to it, except that now you're only looking for at least one true value. Of course, orientations must be correct. Although now you only know that there is an intersection, not where it is. It might be better to do what you are already doing. The difference is that almost all lines will have already been filtered out.