Jump to content

Move sprite along A* path.


Arcanorum
 Share

Recommended Posts

Can someone direct me to any resources/examples that explain how I can make a sprite (or whatever) move along the path created from using an A* path finding algorithm please?

I have looked at a few implementations of A* and got the path 'finding' part working fine, but when it comes to getting something to follow the path I can't figure it out.

 

I can't find any well explained/commented sources of info that don't tell me things I already know or can work out for myself.

Link to comment
Share on other sites

 In one of our recent games I have picture of map with levels. The levels are places on map. Places are distinct points. But to move avatar nicely from one point to another, I connected them with Catmull-Rom spline and there are 3 waypoints between two level points. When I want to move player's avater along path defined with these waypoints (which is similar to your A* waypoints) I create chained tween like this:

        private moveAvatar(aTargetLocation: number): void {            // nothing to move            if (aTargetLocation <= this.mAvatarLocation)                return;            // create chained tween to move point by point to target            var avatarTween = this.add.tween(this.mAvatar);            while (aTargetLocation > this.mAvatarLocation) {                ++this.mAvatarLocation;                var newAvatarLocation = this.mMapPoints[this.mAvatarLocation];                avatarTween.to({ x: newAvatarLocation.mX, y: newAvatarLocation.mY },                    250, Phaser.Easing.Linear.None);            }            avatarTween.onComplete.add(() => {                //console.log('Move complete');            });            avatarTween.start();        }

 There is also other possibility, especially if you want more tight control over your sprite: set next point from A* path as target and move towards it in update method. Also check if you are in target location (best with checking if you are already in some small circle around target point). If so, set next point as a target. One important thing with this approach - in past I used it in one of our games and had headaches, because sprite had collision checks with background on and it often got stuck in some corner. Very often the reason was small rounding error. I tried lot of ways how to bypass it and finally I found the most obvious solution: your A* already defined correct way without collisions - there is no need to check collisions with background (unless it is changing dynamically) during move.

 

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...