Jump to content
  • Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts



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 this post

Link to post
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

bool advance_along_path(float stepLength) {
  while (distance(sprite.currentLocation, path[currentTarget]) <= stepLength) {
    stepLength -= distance(sprite.currentLocation, path[currentTarget]);
    sprite.currentLocation = path[currentTarget];
    if (currentTarget == path.length())
      return true;
  sprite.currentLocation += normalize(path[currentTarget] - sprite.currentLocation) * stepLength;
  return false;
Edited by Álvaro

Share this post

Link to post
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 this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!