•      Sign In
• Create Account

## Best way to check tile-distance (building placement)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1suliman  Members

1266
Like
0Likes
Like

Posted 13 February 2014 - 08:13 AM

Hi

When placing buildings in my game (think dune 2 or warcraft 2) they must be placed with no more than 2 empty tiles spacing from an existing building. What is the best way to perform such a check? Buildings are X*Y tiles in size (and X and Y may not be the same for some building types).

Thanks!
Erik

Edited by suliman, 13 February 2014 - 08:16 AM.

### #2haegarr  Members

7185
Like
0Likes
Like

Posted 13 February 2014 - 08:37 AM

Hmm … "best way" is ever a fruitless question, because what is the measure for "best"?

Let a tile index be [x ; y]. Let the tiles covered by building n be from [xl,n ; yb,n] to [ xr,n; yt,n ], e.g. read it as from left / bottom corner to right / up corner. Then the separate distances between 2 buildings 1 and 2 for the both dimensions are

dx = min( | xl,1 - xr,2 | , | xr,1 - xl,2 | )

dy = min( | yb,1 - yt,2 | , | yt,1 - yb,2 | )

Both of the separate distances need to be 1 for directly adjacent buildings. A value of 2 means that 1 free tile is in-between them. Hence, if no more than 2 free tiles is allowed, both separate distances must be less than 4. (Notice that you may need to use a slightly more complex condition if diagonal distances need to be considered.)

Above solution requires AFAIS that the building do not overlap, or else false positives may occur if one building is really big compared to the other, so checking whether the tiles under the placeable building are free should be done already simply by checking each tile in  [xl,2 ; yb,2] to [ xr,2; yt,2 ] for coverage.

You can restrict the search over all already placed buildings (if needed) by e.g. using a spatial partitioning scheme.

Edited by haegarr, 13 February 2014 - 08:39 AM.

### #3suliman  Members

1266
Like
0Likes
Like

Posted 13 February 2014 - 09:18 AM

Best in this case in easy to understand/code and not too slow (its c++)

Yes I already check for overlap. How would you do that check actually? Loop though each tile of the proposed building-position and check how far other buildings are?

### #4samoth  Members

8927
Like
0Likes
Like

Posted 13 February 2014 - 09:40 AM

It depends a lot on what your game looks like.

If the map is "reasonably sized" (few thousand tiles in each direction), the easiest way may be to draw into a bitmap of "occupied" tiles whenever a house is placed. Placing a new house then just makes it necessary to do a few samples into neighboring bitmap pixels. If they're zero, there is no house.

Another solution would be to do a hierarchical collision detection as suggested by haegarr. For example:

• First, a spatial partitioning scheme like an octree or a regular grid (hashed if you will). Depending on the world size, a hashed/virtualized regular grid will likely be best.
• Next, a radius-radius collision. For that, calculate for each house type (offline preprocessing!) the squared radius of from the house center to the most distant occupied tile, plus 3. If squared distance of the to-be-placed house and another one isn't less than the sum of these two radii, there is no point in pursuing that pair any further.
• Last, if the radius test passed, do an exact tile-by-tile collision for this pair.

### #5haegarr  Members

7185
Like
0Likes
Like

Posted 13 February 2014 - 09:41 AM

Yes I already check for overlap. How would you do that check actually? Loop though each tile of the proposed building-position and check how far other buildings are?

Do you mean the overlap check? It depends on how the ground data is available. However, looping over all tiles of the desired coverage is not needed if the bordering tiles are compared.

* Is there an array where each tile occurrence is memorized, then setting a flag for coverage is a simple extension with which direct indexed checking can be done.

* If you have a kind of sparse array for coverage then a separated indexing is to be done.

* If you have no explicit coverage but the building instances store the covered tile indices (as is used in my post above for distance calculations), then an overlap test would look like

xl,2 <= xr,1 && xr,2 >= xl,1 && yb,2 <= yt,1 && yt,2 >= yb,1 then overlapping

Whether this is worth being accelerated depends on the amount of building. As said, some kind of spatial partitioning may be an option.

### #6suliman  Members

1266
Like
0Likes
Like

Posted 13 February 2014 - 09:47 AM

ok thanks guys, i think i got it now:)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.