# Finding the nearest point to a shape.

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

## Recommended Posts

Hello, I've been trying to figure something out that seems rather simple, but I'm just not sure on the type of math I should be using. I'm trying to find the nearest point on the surface of a shape from the center of a sprite (blue ball). If anyone could point me in the right direction that'd be great, I have included an image below. I was thinking I'd need to use some kind of vector math, so any help is greatly appreciated.

##### Share on other sites

find the nearest point on the surface of a shape

More information is needed to help you out, as it depends on how you have the shape defined. Is it procedural? Defined by vertices? A drawn & filled shape?

##### Share on other sites
Use GJK. GJK works via "support functions" which can map mathematical shapes (curves) or vertex-based shapes. The result of GJK is the shortest distance between two points on the surface of the shapes, which necessarily gives you the points as well.

##### Share on other sites

find the nearest point on the surface of a shape

More information is needed to help you out, as it depends on how you have the shape defined. Is it procedural? Defined by vertices? A drawn & filled shape?

The shape would be drawn and filled. Would it be possible to use some kind of pixel level testing or could this potentially all be done through vector math?

##### Share on other sites

Would it be possible to use some kind of pixel level testing .. ?

Most likely.

.. could this potentially all be done through vector math?

Maybe.

You're new to the site (and welcome!) so you should learn how to post an effective question.

First thing would be to read about posting to the forums. Although that link is about the Beginner's forum, there's a lot of good stuff that will help you get better answers to your questions. E.g., take a look under Asking Questions, at the line that begins "Be as descriptive as you can.."

The reason I'm redirecting you to that page is because your post so far is: "I'm drawing a shape. What's the distance from here to there?" See the problem?

##### Share on other sites

You can do this easily by calculating the perpendicular distance to each line comprising the polygon, as well as checking each vertex.

The perpendicular distance be calculated like this http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html

##### Share on other sites

If all you need is to calculate the distance between one of the box corner to the sprite center, just use the distance formula.

Do this for each 4 points of the box and the shortest value will be the nearest one.

1. 1
2. 2
Rutin
20
3. 3
khawk
16
4. 4
A4L
14
5. 5

• 11
• 16
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633756
• Total Posts
3013707
×