# ﻿Determining what object is closest:

• 08-16-2006
Queatrix
﻿Determining what object is closest:
I want my AI to go after one of two targets. The closest one.

At first is was going to ﻿determine that by getting the disance between my X pos and the target's X pos, and adding that to the disance between my Y pos and the target's y pos, but that doesn't do it, as you can see in my picture here. So how should I do it?

Thanks, August.
• 08-16-2006
OnionKnight
Pythagoras theorem looks like a solution to me.
• 08-16-2006
CornedBee
You're using Manhattan distance for your calculation. You need to use Euclidean distance.
• 08-16-2006
Dash_Riprock
Get the distance from your x to the target's x and square it. Now do the same for y, and then get the square root of the sum of the 2 values. This will get you the distance from you to the target.
• 08-16-2006
Queatrix
Thanks guys. The Pythagoras theorem worked fine.

EDIT: Dash_Riprock, I didn't see your post untill after I replyed. I did what you said except for squaring the 2 sums by them selves.
• 08-16-2006
twomers
>> I did what you said except for squaring the 2 sums by them selves.

Well, it's not Pythagoras' theorm otherwise ...

Try this -

x2 = object position (in a grid), x1 = your pos
y2 = object position (in a grid), y1 = your pos

Attachment 6731
• 08-16-2006
CornedBee
Note that if you're just after the closest object, there's no need to take the square root. You can just compare the squared distances.
• 08-16-2006
twomers
My thinking exactly (was just gonna edit with that), but I stand by what I said (with the exception of a mistaken sign though)!
• 08-16-2006
VirtualAce
Quote:

Note that if you're just after the closest object, there's no need to take the square root. You can just compare the squared distances.
Took the words right out of my mouth. Work in terms of the square of the distance.

Ya know it's really not fair when everyone can feast on the board all day long and when I get home at night there's nothing but crumbs here and there.

:)
• 08-16-2006
swoopy
Quote:

Originally Posted by Bubba
Took the words right out of my mouth. Work in terms of the square of the distance.

Ya know it's really not fair when everyone can feast on the board all day long and when I get home at night there's nothing but crumbs here and there.

:)

Now Bubba, we've been waiting all day for you to reply to this thread.;)