Results 1 to 4 of 4

Thread: Get distance between two points for tile based game

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421

    Thumbs up Get distance between two points for tile based game

    Some people that are making tile based games might be interested in the distance formula. The formula returns how many tiles it takes to get from one point to another on a grid. Most of you probably know this already, but here it is anyway..

    VB Code:
    1. Private Function Distance(X1 As Long, Y1 As Long, X2 As Long, Y2 As Long) As Long
    2.  
    3.     Distance = (X2 - X1) + (Y2 - Y1)
    4.  
    5. End Function

    For instance, if you wanted to determine the distance between tiles (2,8) and (9,14), you would use the formula like this..

    VB Code:
    1. Distance 2, 8, 9, 14

    That would then return 13 which is how many tiles it takes to get from point (2, 8) to point (9, 14). So for those who care, there it is.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

  2. #2
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Uhm.. that's wrong isn't it?

    VB Code:
    1. Private Function Distance(X1 As Long, Y1 As Long, X2 As Long, Y2 As Long) As Long
    2.     Distance = Sqr( (X2 - X1)^2 + (Y2 - Y1)^2 )
    3. End Function

    Well this formula is the distance if you move straight on to point #2..

  3. #3
    Good Ol' Platypus Sastraxi's Avatar
    Join Date
    Jan 2000
    Location
    Ontario, Canada
    Posts
    5,134
    That wouldn't matter if he's doing a typical grid-confinement RPG. I say good work on the function! If you are using a free-roaming (you can be on more than one tile at a time) then you should use Fox's.
    All contents of the above post that aren't somebody elses are mine, not the property of some media corporation.
    (Just a heads-up)

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2001
    Posts
    421
    Yeah, mine didn't incorporate diagonal movement.
    [vbcode]
    ' comment
    Rem remark
    [/vbcode]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width