Author Topic: Proximity in a grid  (Read 1826 times)

0 Members and 1 Guest are viewing this topic.

Offline whitenite1

  • Jr. Member
  • **
  • Posts: 54
Proximity in a grid
« on: April 25, 2011, 10:29:47 AM »
I need a little insight on how I can easily check if an object is within a certain range of another object in a 10x10 grid. In my game of 'The Wolf and the 5 Goats', I need to move a goat away from the wolf when a check is performed and finds a goat within the striking distance of the wolf. The wolf's distance changes after each move, so let's say the movement of the wolf is three squares. I need to check the squares around the goat to see if it's in range.

0000000000
0w00000000
000g000000   ( As shown here, the w is 3 moves away from g. 2 to the right, 1 down, or 1 down, 2 to the right )
0000000000
... to row 10
The goats location is kept in an array. p[1,1] is the row, and p[1,2] is the column. So the goat is at row, 3, column 4, and the wolf at row 2, column 2, which I keep track of in p[0,1] and p[0,2]. Any ideas?

Thanks...

whitenite1

Offline BJ

  • Newbie
  • *
  • Posts: 9
Re: Proximity in a grid
« Reply #1 on: April 25, 2011, 11:28:02 AM »
The following will tell you if two X/Y coordinates are within a specifiable range of one another.  Hopefully this will get you going in the right direction.

If you're testing if they are within 3 squares of each other, you'd set iRange to 2.

Code: [Select]
SUB InPerimeter(int X1, int Y1, int X2, int Y2, OPT int iRange = 1), int
If (Abs(X2 - X1) <= iRange) And (Abs(Y2 - Y1) <= iRange) Then Return 1 Else Return 0
ENDSUB
« Last Edit: April 25, 2011, 11:31:17 AM by JBirk »