Page 1 of 1

Grid based movement

Posted: Sun Feb 23, 2014 1:03 am
by hallsofvallhalla
Need some help on this one. Anyone have a idea or links to how to calculate grid based movement. So in other words if I have 4 move how do I draw the squares to show that. Like in the picture below. Mine will be top down not iso but I need the fastest method to determine movable squares.


Image

Image

Re: Grid based movement

Posted: Sun Feb 23, 2014 3:02 am
by Jackolantern
There is probably a faster way, but I think you would have to iterate through the possibilities, and probably toggle a multidimensional array of Booleans. Basically the way grid movement works is that you have:

- If they take all of their movements up, down, left, or right, so you have to highlight 4 rays projected out from the user's location.
- Next, they could take all but one of their moves up, down, left or right, and then take the last move to the left or right relative of their facing, so that is a total of 2 more cells for each direction.
- Next, they could take all but 2 of their moves UDLR, but since there is more than 2 cells left at the end of the initial movement, you have to highlight both a right and a left cell, and then the 3 remaining adjacent cells to each of those right and left cells to account for the last move.

...and you essentially keep doing this until you reach 0 (FOR loop with a x-- counter), and account for each choice they could take for each direction. Maybe there would be a point depending on how many moves a player has where all the cells they could move to would already be selected once you reach down to a low enough number during the loop.

Re: Grid based movement

Posted: Sun Feb 23, 2014 5:53 am
by hallsofvallhalla
and you do this how?.....

:)

Re: Grid based movement

Posted: Sun Feb 23, 2014 5:59 am
by a_bertrand
I would really investigate into A*, as what if you have something blocking you in the middle? Check the A* algorithm, then make it go as far as X cell from your current position and show all possible points.

Re: Grid based movement

Posted: Sun Feb 23, 2014 4:28 pm
by hallsofvallhalla
I have a collision function on the red tile that gets laid and the terrain so that solves any squares they cannot move to. I also have a mouse click that solves to move to.
I am thinking more of a math algorithm that builds the diamond above. I know it is a simple math solution but for some reason it is not coming to me. Remember I am not looking for path finding to the squares but how to build the diamond based on movement points.

Re: Grid based movement

Posted: Sun Feb 23, 2014 4:38 pm
by Xaos
I may be missing the point, but why not just use a multi-dimensional array and then display the squares for each location 'activated' in the array? It could be a big array to account for all kinds of shapes and sizes and then modified for each game/match/fight.

Re: Grid based movement

Posted: Sun Feb 23, 2014 5:12 pm
by Jackolantern
a_bertrand wrote:I would really investigate into A*, as what if you have something blocking you in the middle? Check the A* algorithm, then make it go as far as X cell from your current position and show all possible points.
I am not sure if A* is right for this. A* is a pathfinding algorithm, and as such, requires both a start and a goal.
Xaos wrote:I may be missing the point, but why not just use a multi-dimensional array and then display the squares for each location 'activated' in the array? It could be a big array to account for all kinds of shapes and sizes and then modified for each game/match/fight.
The issue is actually activating or deactivating those squares. If you want to highlight only the cells that the player can move to in x moves, it becomes more complicated.
hallsofvallhalla wrote:I have a collision function on the red tile that gets laid and the terrain so that solves any squares they cannot move to. I also have a mouse click that solves to move to.
I am thinking more of a math algorithm that builds the diamond above. I know it is a simple math solution but for some reason it is not coming to me. Remember I am not looking for path finding to the squares but how to build the diamond based on movement points.
Is it always going to be in the shape of a diamond? Because that wouldn't be so bad.

Re: Grid based movement

Posted: Sun Feb 23, 2014 6:37 pm
by leruman
Can characters go diagonally to next tile, or just cross movement to 4 tiles?

Re: Grid based movement

Posted: Sun Feb 23, 2014 6:40 pm
by hallsofvallhalla
no diagonal so that is what creates the diamond shape. Yes it will always be diamond but may not end up that way due to non passable squares but my collision takes care of that.

I basically have for loops looking at the column and row of the player then moving out breaking each entity which are the red square but cannot get it to work right.

Re: Grid based movement

Posted: Sun Feb 23, 2014 9:13 pm
by Jackolantern
I will have to think on a bit to make an algorithm that works well and is efficient. Maybe later today I can build a little something in JS.