View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Make sprite follow an array of points in a grid

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1thefollower  Members

Posted 17 January 2014 - 09:08 PM

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.

### #2Álvaro  Members

Posted 18 January 2014 - 05:07 AM

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, 18 January 2014 - 05:08 AM.

### #3thefollower  Members

Posted 18 January 2014 - 09:51 PM

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

### #4Álvaro  Members

Posted 19 January 2014 - 12:16 AM

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.

### #5thefollower  Members

Posted 21 January 2014 - 08:44 PM

Ah i understand - i got it working now thanks

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.