# Modified movement cost using A*

This topic is 1768 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all. I'm new to posting to the forum here but have been using the site as a reference source for answering questions for some time. Hopefully I can get some insight into this question I haven't been able to find a good answer to.

I am developing a realistic ww2 top down real time strategy. I am using A* for path finding but it's in it's simplest form. My game map consists of 10x10 "tiles" with a terrain type and height in meters. Eventually I will have 15-20 types of passable terrain and I want them to have an impact on speed along with going uphill or down hill. I know this will modify the cost from the normal straight/diagonal cost I'm using now.

My question is how much should I weight the cost without tipping the balance and losing the effect of the actual distance but enough to where it logically works to go the best path. Example around a large hill instead of up and over when moving fast. Now I'm not looking for an exact answer as I know it's all relative and a personal feel but I'd like to start in the right ballpark! :)

##### Share on other sites

My suggestion would be to do the math.

Say you go up a rather steep hill that impedes your normal speed by 50%. Then you should add 50% to the cost of that node vs the normal unimpeded terrain cost. Same for grass, sand, snow, or whatever other terrains or factors you have in the game. Make the cost relative to the speed. As long as you are consistent in your math, it should work out just fine.

Also, if different unit types move at different speeds across different terrains, then the cost of each node should be different based on the AI unit that is performing the navigation. For example, say vehicle A moves faster through grass, but slower through sand, while vehicle B moves faster through sand, and slower through grass. These units should have different cost values for these terrains since the effect is different.

In the end it all comes down to math. Calculate the change in speed across a terrain for a particular unit, and apply the same percentage of change to the cost value.

##### Share on other sites

The rule is that your estimate in your A* function should always be less or equal to the actual cost. So if your fastest terrain type is road, then your estimate function could be the straight line road cost. All other terrain types would be more expensive and so your A* should return the optimal path.

##### Share on other sites

Yes, as others have said, you need a consistent unit to calculate all of your metrics as.  For instance, in your case you may want to have time as your metric that you want to minimize.  In which case all of your path calculations should evaluate as time that way you can compare apples to apples.  Another example could be distance as your metric where you are trying to minimize distance.  I don't think this is what you want, though.  So basically, first figure out the metric you want to minimize and then have all of your formulas calculate that metric.

##### Share on other sites

OK that makes sense.  I was actually kind of thinking along those lines.  And just to clarify one issue.  The main points of movement are the center of the 10x10 tiles, so if I'm moving from lets say a grass tile to a sand tile, since I am moving 1/2 the time on grass and 1/2 the time on sand then I would weight each by 50% and then apply that to the normal movement cost?

##### Share on other sites

OK that makes sense.  I was actually kind of thinking along those lines.  And just to clarify one issue.  The main points of movement are the center of the 10x10 tiles, so if I'm moving from lets say a grass tile to a sand tile, since I am moving 1/2 the time on grass and 1/2 the time on sand then I would weight each by 50% and then apply that to the normal movement cost?

That's what I'm doing in my code and it works fine.

I believe that calculating the cost based purely on the second tile's cost (the destination tile of the pair) would probably be as valid since the next step is going to include another 50% of sand. Although there may be edge cases with multiple goal points to choose from where the simpler approach might be minutely wrong but it probably won't matter to anyone.

##### Share on other sites

Perfect well I'll be getting around the implementing this in a couple days and will check back in.

I think averaging the 2 tiles is best because, at least in my game, you can do diagonal movement, and if you just used the next tile, while the loss of accuracy would still be small, the cost of movement wouldn't balance out if you went diagonal and then straight (left, right, up, down) following the diagonal movement, and I think it would be big enough of a difference to throw off the accuracy of the path finder.

Edited by airborneinf82

##### Share on other sites

realistic terrain the heights are very important because they can slow/impede movement speed significantly (puting the object under fire in the open for longer - very nasty with WW2 firepower)

So the difficulty between the node should be relatve to the way the game mechanics work  (including uphill harder than downhill -- the edged costs being directional (dual values)

Some curve table based on the difference between adjacent nodes as a basic calculation method.

Depending on the complexity of the movement other factors like what kind of terrain it is (and if the SPECIFIC unit has any special advantage/disadvantage thru different terrain flavors)

You may wind up haveing multiple A* node maps for different unit types to reflect the different units abilities (insetad of having the edge costs recalculated EVERY  open node calculation.