March 28, 2024, 05:17:51 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Proximity in a grid

Started by whitenite1, April 25, 2011, 10:29:47 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

whitenite1

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

BJ

April 25, 2011, 11:28:02 AM #1 Last Edit: April 25, 2011, 11:31:17 AM by JBirk
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.

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