Grid based movement

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Grid based movement

Post 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
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Grid based movement

Post 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.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Grid based movement

Post by hallsofvallhalla »

and you do this how?.....

:)
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Grid based movement

Post 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.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Grid based movement

Post 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.
User avatar
Xaos
Posts: 946
Joined: Wed Jan 11, 2012 4:01 am

Re: Grid based movement

Post 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.
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Grid based movement

Post 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.
The indelible lord of tl;dr
leruman
Posts: 29
Joined: Fri May 25, 2012 6:06 pm

Re: Grid based movement

Post by leruman »

Can characters go diagonally to next tile, or just cross movement to 4 tiles?
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Grid based movement

Post 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.
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Grid based movement

Post 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.
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”