• ### What is your GameDev Story?

Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Distance between a point and a polygon :(

This topic is 5397 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Is there a way that would find the distance between a point and a polygon? I''m trying to use a binary tree to cut down on collision detection between the player and the playing area, but I need to find the closest point to a polygon to draw the lines to cut up the 2D playing area. I''ve looked around, but I can''t seem to find any help.

##### Share on other sites
I suppose you could do a quick pass to check which of the points of a polygon is closest to the point (quick as in without the final sqrt), then apply the sqrt to the closest point to find the distance...

##### Share on other sites
How accurate do you need it to be?

If speed is your concern and a margin of error is ok, you could just check the distance to one of the vertices of the polygon. The center of the polygon will give slightly better results, but unless you have it stored it is also a little slower.

Unfortunatly, the larger your polygons get the more accuracy problems this will have.

For a better results check all the vertex points and use the smallest, as falkone suggested, just be aware that it still isn''t perfectly accurate.

Drakonite

Shoot Pixels Not People

##### Share on other sites
The most accurate solution would be to find the distance between the point and each line segment comprising the polygon, and take the minimum distance.

"Sneftel is correct, if rather vulgar." --Flarelocke

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634096
• Total Posts
3015504
×