Irrelon / ige

The Isogenic Game Engine
523 stars 139 forks source link

Smooth out path-finding to be more natural #36

Closed goldfire closed 12 years ago

goldfire commented 12 years ago

Is there a way to smooth the path-finding out so that the player can move diagonally instead of being forced to walk out of their way to go to a certain tile?

For example, in the below image, the yellow path would be much preferred over the blue as that is what the player would be expecting to happen when they click there, unless there is something blocking their path in the middle tile.

Path-Finding Example

Irrelon commented 12 years ago

@goldfire In the path-finding example (9.5) the middle tile is "blocked" so the entity should not path through it. You can set the square and diagonal movement options of the path-finder via the options passed to the aStar() method as documented in the IgePathFinder.js file:

/**
 * Uses the A* algorithm to generate path data between two points.
 * @param {IgeCollisionMap2d} tileMap The tile map to use when generating the path.
 * @param {IgePoint} startPoint The point on the map to start path-finding from.
 * @param {IgePoint} endPoint The point on the map to try to path-find to.
 * @param {Function} comparisonCallback The callback function that will decide if each tile that is being considered for use in the path is allowed or not based on the tile map's data stored for that tile which is passed to this method as the first parameter. Must return a boolean value.
 * @param {Boolean} allowSquare Whether to allow neighboring tiles along a square axis. Defaults to true if undefined.
 * @param {Boolean} allowDiagonal Whether to allow neighboring tiles along a diagonal axis. Defaults to false if undefined.
 * @return {Array} An array of objects each containing an x, y co-ordinate that describes the path from the starting point to the end point in order.
 */
aStar: function (tileMap, startPoint, endPoint, comparisonCallback, allowSquare, allowDiagonal) {
goldfire commented 12 years ago

Bah, I feel like a moron. Not sure how I missed that. Thanks, works beautifully!

Irrelon commented 12 years ago

@goldfire Hehe, don't worry this is all still "hidden away" documentation at the moment!