Jump to content
  • Advertisement
Sign in to follow this  
Revin122

Following a line

This topic is 3948 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

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


Link to post
Share on other sites
Advertisement
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 this post


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


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

Admiral

Share this post


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

[edit]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]

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!