Thread: Determining what object is closest:

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question 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.
    Last edited by Queatrix; 08-16-2006 at 03:51 PM.

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Pythagoras theorem looks like a solution to me.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You're using Manhattan distance for your calculation. You need to use Euclidean distance.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    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.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    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.
    Last edited by Queatrix; 08-16-2006 at 04:54 PM.

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    >> 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
    Last edited by twomers; 08-16-2006 at 05:06 PM.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    My thinking exactly (was just gonna edit with that), but I stand by what I said (with the exception of a mistaken sign though)!

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    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.


  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

    Thumbs up

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM