Search code examples
geometrygame-physics

Given a polygon and a point in 2D, how can one find the feature (vertex or edge) of the polygon closest to the point?


A naive approach is to find, for each edge in the polygon, the point on that edge closest to the given point, and then take the one that's closest. Is there a faster algorithm? My goal is to implement a 2D Super Mario Galaxy-style platformer.

Apparently this can be done with Voronoi regions, as in this video: http://www.youtube.com/watch?v=Ldh2YKobuWo

However, I can't find any Voronoi algorithms that deal with edges as well as points. Ideas?


Solution

  • If the polygon is convex, then the overhead of the voronoi calculation far exceeds that of the naive approach.

    If this is run many times, and each time the point changes slightly, you only need to check 3 segments (think about it: as you move around, assuming many checks, then the closest edge will only change to an adjacent edge)