How do I calculate grid distance?

0 favourites
  • 9 posts
From the Asset Store
Grid - snap to grid
$1 USD
80% off
Snap to visible grid - perfect solution for any game genre
  • Oof, it's been a while since I've asked for help twice in one day xD

    So I'm working on a treasure hunt game, it's sort of a reverse minesweeper where you have limited shovels to dig and find treasure with. The grid of tiles is made using sprites. I have some special items to dig up, but otherwise the tile just spawns text that says how many squares away the chest is, and also it changes to red if it's adjacent to a kill tile.

    The issue is, sometimes it's not entirely accurate? The numbers will be off by 1 or 2, and sometimes I'll have a full 3x3 grid of red tiles but no skulls around.

    So I'm assuming it has something to do with my distance formula. Currently I'm using:

    str((floor(distance(Tile.X,Tile.Y,Treasure.X,Treasure.Y)/32)))

    I had the floor set to int before, but either one has slightly inaccurate results.

    Thanks so much!

    Tagged:

  • Floor and int work the same in construct - they both floor.

    I would guess you’d need 2 equations. 1 for how many’s diagonals you would have to take, then add how many straight squares.

    If you’re 8 up and 3 to the right you would solve for 3 diagonal steps then 5 straight.

  • Distance along diagonals is further than horizontal/vertical. Not sure how you could tackle this ideally...

    Probably you could use something like a* to find the shortest path through the grid.

  • Another idea is to use the Pathfinder as it will give you the shortest path and also the Path Tile Count, this will not hurt your performance since you will need to just do it once or very few amounts of times.

  • One more suggestion similar to Tarek2's idea - I find the EasyStar behaviour is phenomenal for tile work, distance, etc. If you apply the behaviour to your tileset, you can use it to find a path between the origin and target tiles, and the calculate its length using the "PathLength" property. I think it uses the a* that WackyToaster mentioned, too.

    construct.net/en/forum/construct-2/addons-29/behavior-easystar-js-96215

  • After looking at the math you just find the longer distance between vertical and horizontal. Whatever the shorter distance is, gets used up with diagonal steps.

    Find difference of X of current and X of chest then compare if its greater or less than Y of current Y of chest. If greater int((x/32)+0.5) or round(x/32)

  • Oh yeah, actually both easystar and the build in behavior use a* and should work as long as you can get the tile count.

  • winstreak

    Nice analysis.

    From that idea the distance on the grid with diagonals would be

    int(max(abs(treasure.x-tile.x),abs(treasure.y-tile.y))/32)

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • R0J0hound winstreak

    Thanks so much! As far as I can tell, this equation is working! You guys rock <3

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)