Notifications
Clear all

[Closed] A* Pathfinding help

I’m not much of a scripter, but fancied trying to make a maxscript implimentation of the A* pathfinding method. Explained in simple terms here:
http://www.policyalmanac.org/games/aStarTutorial.htm

It seems it should be easy enough and I’ve made some progress, but I wondered if anyone else had tried something similar, as I’m not sure I’ve started going about it the right way.

I’m using boxes to form my grid, and storing them in a multidimensional array. This enables me to manage the objects based on their grid reference. I’m not entirely sure if this is even the best way to do this, so I’m cautious to go much further.

One box will be the start, one the goal, and through a flag (maybe something like a color change) there will be “wall” boxes.

I’m currently try to figure out how I can express that a diagonal move is more expensive than a horizontal or vertical one. Clearly both coordinates chage for a diagonal move, whereas only one does for a horzontal or vertical, so perhaps that is the key?

I’m just going to soldier on with this, but any input or thoughts would be great.

4 Replies

I’ve done A* (or similar) in Flash, but never MXS.
You could also use the distance function:

 
if distance [1,1] [2,2] > 1 then print "expensive!"

Yeah that would work, but I wanted to avoid using Max’s distance tool as everything is on a grid, and it seems overkill. I’m trying to do it all just by comparing coordinates…, maybe I’m making it too hard for myself…

If your cells are perfectly aligned, you could subtract the current point from the target point. If one of the components (x or y) is equal zero (or near zero), means that it’s a vertical/horizontal movement, else it’s a diagonal movement.

Another solution would be to calculate the squared distance. This way you avoid the square root operation:

fn distanceSq p1 p2 = (
	local p1Top2 = p2 - p1
	return (p1Top2.x^2 + p1Top2.y^2)
)

Interesting solotution… thanks