# Make sprite follow an array of points in a grid

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

## Recommended Posts

Hello

I need help with understanding how to approach the logic of this.

So i have an array of points which my sprite will follow at a fixed speed the array looks like this:

[

{"X":"2"   ,"Y":"2"},
{"X":"9"   ,"Y":"2"},
{"X":"10" ,"Y":"3"},
{"X":"1"   ,"Y":"3"}
]

My grids are 50 by 50 pixels and my sprite moves at 4 pixels per second.

Now I set up my sprite info so i have and i can calculate the line the sprite will take quite easily:

sprite.currentLocationX = path[0].X * 50;
sprite.currentLocationY = path[0].Y * 50;

sprite.dX = path[1].X * 50; //destination X

sprite.dY = path[1].Y * 50; //destination Y

My question is how do you calculate when the sprite has passed destination X:Y and so update the sprite's destination ?

I update the sprite's X:Y current location every frame using delta*speed.

I thought about using the idea of checking when X:Y is greater than destination X:Y but that merely assumes the sprite is moving north east only.

If it matters in terms of explaining the way to work it out, my sprites moving in 8 compass directions.

##### Share on other sites
I wrote the answer in pseudo-code. I can translate it to English if it is not clear enough.

// advance_along_path returns whether we have arrived at the end of the path

while (distance(sprite.currentLocation, path[currentTarget]) <= stepLength) {
stepLength -= distance(sprite.currentLocation, path[currentTarget]);
sprite.currentLocation = path[currentTarget];
currentTarget++;
if (currentTarget == path.length())
return true;
}

sprite.currentLocation += normalize(path[currentTarget] - sprite.currentLocation) * stepLength;
return false;
}

Edited by Álvaro

##### Share on other sites

Yeah might be easier to follow in english rather than code.. if thats okay ?

##### Share on other sites
OK, I'll try.

At any given time, you'll know what node in the path you are currently going towards. Let's call it the "current target"

In order to advance some length along the path, check if the current target is within that length. If that's the case, move to that position, reduce the length to advance by the amount you have just moved and start aiming for the next node in the path.

Since it could be that several points in the path are very close together, you should do the previous check in a loop, in case the amount you are advancing is enough to go over several nodes.

Finally, now that you know you won't get to the next target in this step, just advance in the direction of the goal.

Try re-reading the code with this description in mind, to see if you can understand what each part is supposed to do.

##### Share on other sites

Ah i understand - i got it working now thanks :)

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11