what is the best way to determine which is the closest point to hit the target in 2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By HarbingerSh

what is the best way to determine which is the closest point to hit the target in 2D?

here you can see the agent want to know that is is the best point to hit the Body and not twards the center point

What are the constraints on this? Will the character always be “contained” by these colliders?

HyperlinkYourHeart | 2023-06-12 22:03

:bust_in_silhouette: Reply From: jgodfrey

Maybe someone will correct me, but I don’t think there’s any built-in method to do this. I think you’ll need to do a bit of work yourself.

Are your targets always rectangles (as in your image)?

If so, you could probably use Godot’s geometry functions do the underlying math for you. Maybe something based on Geometry2D.get_closest_point_to_segment() (Docs here)

You could pass in each of the 4 segments making up the target rectangle. That’ll give you the closest point on each of the 4 segments. The one closest to your original point is the one you’re after…