Public Group

# Following a line

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

## Recommended Posts

In 2D sprite programming, can anyone give me an idea or a snip on how to make my character sprite follow a particular randomly (well actually a large array of line sprites) pre-generated line which is also a sprite? I thought pathfinding would be the solution but it offers a different solution that I wanted. thanks...

##### Share on other sites
Well in order to follow the path the easiest way is to have the coordinates for each line in the path. You then want to place the sprite at the beginning of the path or the coordinate of the first point on the path.

I'm not sure what programming language you are using so my example code is C++

directionX = points[nextPointIndex]->GetX() - sprite->GetX();directionY = points[nextPointIndex]->GetX() - sprite->GetX();distance = sqrt(dirctionX * directionX + directionY * directionY);if (distance < spriteSpeed)  // if the distance is less than the spriteSpeed then the sprite will pass the point next update so it is just placed at the next location{  sprite->SetX( points[nextPointIndex]->GetX());  sprite->SetX( points[nextPointIndex]->GetX());  nextPointIndex++;  if (nextPointIndex == pathLength)  {    // We reached the end of the path    // Do whatever you want it to do once that has happened  }}else{  directionX /= distance;  directionY /= distance;  velocityX = directionX * spriteSpeed;  velocityY = directionY * spriteSpeed;}

I'm not sure why it gets removed but there should be a plus sign between dirctionX * directionX and directionY * directionY

##### Share on other sites
Oh...I forgot to mention that the line isn't just a straight line...it has edges and curves before going to it's other end. Hmmmm...like a road map direction map or something like that.

yes I'm using C++

##### Share on other sites
You'll need to choose a spline representation appropriate to the source data, then periodically update the sprite's position according to the coordinates calculated by evaluating the spline at the incrementing parameter.

There are various spline formats, each with their own pros, cons and particulars. Tell us more about the way-point data and we'll suggest something suitable.

##### Share on other sites
Well, if you wanted to just keep the path sprite, and not worry about matching lines or spline curves to it, then you probably could use a path finding algorithm. Just weight the pixels of the path sprite based on color. If your sprite has a white background with the line that is black, then weight black as zero cost, white as heavy cost, and grays as an intermediate weight, based on darkness of the gray. This would only work well if your line isn't a very thick line, however. Otherwise, the resulting path might not fit the center of the line very well, but would rather hug the edges of it. Also, you'd probably want to run the algorithm on your sprites once and store the results, rather than rerun the pathfinding algorithm every single time you needed the path.

You'd also have to deal with the issue of jerky movement, since at best, you'd only have eight directions of available movement, due to the square nature of pixel sprites. It's probably doable, but it'd be extra work.[/edit]

1. 1
Rutin
45
2. 2
3. 3
4. 4
5. 5

• 10
• 28
• 20
• 9
• 20
• ### Forum Statistics

• Total Topics
633407
• Total Posts
3011700
• ### Who's Online (See full list)

There are no registered users currently online

×